Skip to content

Commit ff693f0

Browse files
committed
add getFrequencyFromNoteName(noteFullName)
- fix steps range input
1 parent e2eaa9b commit ff693f0

File tree

4 files changed

+77
-8
lines changed

4 files changed

+77
-8
lines changed

demo/src/components/PlayNote.vue

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@
7373
<br />
7474
<input
7575
type="range"
76-
v-model="steps"
76+
v-model.number="steps"
7777
:id="steps_text"
7878
:min="MIN_STEPS"
7979
:max="MAX_STEPS"
@@ -196,7 +196,7 @@ function updateLowestMetrics() {
196196
lowest_metrics.value = np.getLowestMetrics()
197197
}
198198
const MIN_FREQEUNCY = computed(() => lowest_metrics.value.frequency)
199-
const MIN_STEPS = computed(() => lowest_metrics.value.steps)
199+
const MIN_STEPS = computed(() => lowest_metrics.value.step)
200200
const MAX_FREQEUNCY = 20000
201201
const MAX_STEPS = np.getStepsFromFrequency(MAX_FREQEUNCY)
202202
@@ -211,9 +211,12 @@ watch(temperament, () => {
211211
212212
const note_name_text = 'Note name'
213213
const note_name = ref('A4')
214+
watch(note_name, () => {
215+
note_frequency.value = np.getFrequencyFromNoteName(note_name.value)
216+
})
214217
215218
const steps_text = 'Steps'
216-
const steps = ref(0)
219+
const steps = ref<number>(0)
217220
watch(steps, () => {
218221
note_frequency.value = np.getFrenquencyFromSteps(steps.value)
219222
note_name.value = np.getNoteNameFromSteps(steps.value)

dist/index.d.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ declare class notePlayer {
88
private CONCERT_PITCH_OCTAVE;
99
private temperament;
1010
private noteNames;
11+
private noteNameRegex;
1112
constructor();
1213
setOscillatorDefaultSettings(): void;
1314
setOscillatorType(type: OscillatorType): void;
@@ -20,10 +21,13 @@ declare class notePlayer {
2021
getFrenquencyFromSteps(steps: number): number;
2122
getStepsFromFrequency(frequency: number): number;
2223
getNoteNameFromSteps(steps: number): string;
24+
getLowestStep(): number;
25+
getLowestFrequency(): number;
2326
getLowestMetrics(): {
24-
steps: number;
27+
step: number;
2528
frequency: number;
2629
};
30+
getFrequencyFromNoteName(noteFullName: string): number;
2731
}
2832

2933
export { notePlayer as default };

dist/index.js

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ var notePlayer = class {
2525
"G#"
2626
];
2727
// Based on Chromatic scale (12-TET) only, TODO: auto detect notes based on temperament
28+
noteNameRegex = /^(A|B|C|D|E|F|G)(#?)(\d)$/;
2829
constructor() {
2930
this.audioCtx = new AudioContext();
3031
this.gainNode = this.audioCtx.createGain();
@@ -80,10 +81,37 @@ var notePlayer = class {
8081
let noteIndex = (steps >= 0 ? steps : Math.abs(this.noteNames.length + steps)) % this.temperament;
8182
return `${this.noteNames[noteIndex]}${octave}`;
8283
}
84+
getLowestStep() {
85+
const step = -this.temperament * this.CONCERT_PITCH_OCTAVE;
86+
return step;
87+
}
88+
getLowestFrequency() {
89+
const step = this.getLowestStep();
90+
const frequency = this.getFrenquencyFromSteps(step);
91+
return frequency;
92+
}
8393
getLowestMetrics() {
84-
const steps = -this.temperament * this.CONCERT_PITCH_OCTAVE;
94+
return { step: this.getLowestStep(), frequency: this.getLowestFrequency() };
95+
}
96+
getFrequencyFromNoteName(noteFullName) {
97+
console.log("Incoming noteFullName:", noteFullName);
98+
const match = noteFullName.match(this.noteNameRegex);
99+
console.log(noteFullName, match);
100+
if (!match) {
101+
throw new Error("Invalid note format");
102+
}
103+
const [, noteLetter, sharp, octaveStr] = match;
104+
const noteName = `${noteLetter}${sharp}`;
105+
const octave = Number(octaveStr);
106+
const noteIndex = this.noteNames.findIndex((note) => note === noteName);
107+
if (noteIndex === -1) {
108+
throw new Error("Invalid note");
109+
}
110+
const stepsFromOctave = this.temperament * octave;
111+
const stepsBase = noteIndex;
112+
const steps = this.getLowestStep() + stepsFromOctave + stepsBase;
85113
const frequency = this.getFrenquencyFromSteps(steps);
86-
return { steps, frequency };
114+
return frequency;
87115
}
88116
};
89117
export {

src/index.ts

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ export default class notePlayer {
2121
"G",
2222
"G#",
2323
]; // Based on Chromatic scale (12-TET) only, TODO: auto detect notes based on temperament
24+
private noteNameRegex = /^(A|B|C|D|E|F|G)(#?)(\d)$/;
2425

2526
constructor() {
2627
this.audioCtx = new AudioContext();
@@ -90,9 +91,42 @@ export default class notePlayer {
9091
return `${this.noteNames[noteIndex]}${octave}`;
9192
}
9293

94+
getLowestStep() {
95+
const step = -this.temperament * this.CONCERT_PITCH_OCTAVE;
96+
return step;
97+
}
98+
getLowestFrequency() {
99+
const step = this.getLowestStep();
100+
const frequency = this.getFrenquencyFromSteps(step);
101+
return frequency;
102+
}
93103
getLowestMetrics() {
94-
const steps = -this.temperament * this.CONCERT_PITCH_OCTAVE;
104+
return { step: this.getLowestStep(), frequency: this.getLowestFrequency() };
105+
}
106+
107+
getFrequencyFromNoteName(noteFullName: string) {
108+
console.log("Incoming noteFullName:", noteFullName);
109+
110+
const match = noteFullName.match(this.noteNameRegex);
111+
console.log(noteFullName, match);
112+
if (!match) {
113+
throw new Error("Invalid note format");
114+
}
115+
116+
const [, noteLetter, sharp, octaveStr] = match;
117+
118+
const noteName = `${noteLetter}${sharp}`;
119+
const octave = Number(octaveStr);
120+
121+
const noteIndex = this.noteNames.findIndex((note) => note === noteName);
122+
123+
if (noteIndex === -1) {
124+
throw new Error("Invalid note");
125+
}
126+
const stepsFromOctave = this.temperament * octave;
127+
const stepsBase = noteIndex;
128+
const steps = this.getLowestStep() + stepsFromOctave + stepsBase;
95129
const frequency = this.getFrenquencyFromSteps(steps);
96-
return { steps, frequency };
130+
return frequency;
97131
}
98132
}

0 commit comments

Comments
 (0)