Skip to content

Commit 24376a5

Browse files
committed
release v0.0.3: make notePlayer a functional class
1 parent 82c6a33 commit 24376a5

File tree

5 files changed

+112
-29
lines changed

5 files changed

+112
-29
lines changed

demo/src/components/PlayNote.vue

Lines changed: 7 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -24,45 +24,34 @@
2424
</template>
2525

2626
<script setup lang="ts">
27-
import { getAudioContext } from 'noteplayer'
27+
import { notePlayer } from 'noteplayer'
2828
import { computed, ref, watch } from 'vue'
2929
3030
const note_frequency = ref(440)
3131
const note_frequency_text = 'Frequency'
3232
33+
const np = new notePlayer()
34+
3335
const is_playing = ref(false)
3436
const play_button_text = computed(() => (!is_playing.value ? 'Start note' : '🔊 Playing note...'))
3537
36-
// create web audio api context
37-
// const audioCtx = new AudioContext()
38-
39-
const audioCtx = getAudioContext()
40-
41-
// create Oscillator node
42-
const oscillator = audioCtx.createOscillator()
43-
oscillator.type = 'sine'
44-
45-
const gainNode = audioCtx.createGain()
46-
oscillator.connect(gainNode)
47-
oscillator.start()
48-
4938
function playNote() {
5039
if (!is_playing.value) {
5140
is_playing.value = true
52-
gainNode.connect(audioCtx.destination)
41+
np.play()
5342
} else {
54-
gainNode.disconnect(audioCtx.destination)
5543
is_playing.value = false
44+
np.stop()
5645
}
5746
}
5847
5948
watch(note_frequency, () => {
60-
oscillator.frequency.setValueAtTime(note_frequency.value, audioCtx.currentTime)
49+
np.setFrequency(note_frequency.value) // set frequency
6150
})
6251
6352
const volume = ref(50)
6453
const volume_text = 'Volume'
6554
watch(volume, () => {
66-
gainNode.gain.value = volume.value / 100
55+
np.setGain(volume.value / 100)
6756
})
6857
</script>

dist/index.d.ts

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,15 @@
1-
declare function getAudioContext(): AudioContext;
1+
declare class notePlayer {
2+
private audioCtx;
3+
private gainNode;
4+
private oscillator;
5+
private DEFAULT_FREQUENCY;
6+
constructor();
7+
private setOscillatorDefaultSettings;
8+
setOscillatorType(type: OscillatorType): void;
9+
setFrequency(frequency: number): void;
10+
setGain(gain: number): void;
11+
play(frequency?: number): void;
12+
stop(): void;
13+
}
214

3-
export { getAudioContext };
15+
export { notePlayer };

dist/index.js

Lines changed: 43 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,46 @@
11
// src/index.ts
2-
function getAudioContext() {
3-
const audioCtx = new AudioContext();
4-
return audioCtx;
5-
}
2+
var notePlayer = class {
3+
audioCtx;
4+
gainNode;
5+
oscillator;
6+
DEFAULT_FREQUENCY = 440;
7+
constructor() {
8+
this.audioCtx = new AudioContext();
9+
this.gainNode = this.audioCtx.createGain();
10+
this.oscillator = this.audioCtx.createOscillator();
11+
this.oscillator.connect(this.gainNode);
12+
this.setOscillatorDefaultSettings();
13+
this.oscillator.start();
14+
}
15+
setOscillatorDefaultSettings() {
16+
this.oscillator.frequency.setValueAtTime(
17+
this.DEFAULT_FREQUENCY,
18+
this.audioCtx.currentTime
19+
);
20+
this.oscillator.type = "sine";
21+
}
22+
setOscillatorType(type) {
23+
this.oscillator.type = type;
24+
}
25+
setFrequency(frequency) {
26+
this.oscillator.frequency.setValueAtTime(
27+
frequency,
28+
this.audioCtx.currentTime
29+
);
30+
}
31+
setGain(gain) {
32+
this.gainNode.gain.setValueAtTime(gain, this.audioCtx.currentTime);
33+
}
34+
play(frequency) {
35+
if (frequency) {
36+
this.setFrequency(frequency);
37+
}
38+
this.gainNode.connect(this.audioCtx.destination);
39+
}
40+
stop() {
41+
this.gainNode.disconnect(this.audioCtx.destination);
42+
}
43+
};
644
export {
7-
getAudioContext
45+
notePlayer
846
};

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "noteplayer",
3-
"version": "0.0.2",
3+
"version": "0.0.3",
44
"description": "",
55
"main": "./dist/ index.js",
66
"types": "./dist/index.d.ts",

src/index.ts

Lines changed: 47 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,48 @@
1-
export function getAudioContext() {
2-
const audioCtx = new AudioContext();
3-
return audioCtx;
1+
export class notePlayer {
2+
private audioCtx: AudioContext;
3+
private gainNode: GainNode;
4+
private oscillator: OscillatorNode;
5+
private DEFAULT_FREQUENCY = 440;
6+
constructor() {
7+
this.audioCtx = new AudioContext();
8+
9+
this.gainNode = this.audioCtx.createGain();
10+
11+
this.oscillator = this.audioCtx.createOscillator();
12+
this.oscillator.connect(this.gainNode);
13+
14+
this.setOscillatorDefaultSettings();
15+
this.oscillator.start();
16+
}
17+
18+
private setOscillatorDefaultSettings() {
19+
this.oscillator.frequency.setValueAtTime(
20+
this.DEFAULT_FREQUENCY,
21+
this.audioCtx.currentTime
22+
);
23+
this.oscillator.type = "sine";
24+
}
25+
26+
setOscillatorType(type: OscillatorType) {
27+
this.oscillator.type = type;
28+
}
29+
setFrequency(frequency: number) {
30+
this.oscillator.frequency.setValueAtTime(
31+
frequency,
32+
this.audioCtx.currentTime
33+
);
34+
}
35+
setGain(gain: number) {
36+
this.gainNode.gain.setValueAtTime(gain, this.audioCtx.currentTime);
37+
}
38+
39+
play(frequency?: number) {
40+
if (frequency) {
41+
this.setFrequency(frequency);
42+
}
43+
this.gainNode.connect(this.audioCtx.destination);
44+
}
45+
stop() {
46+
this.gainNode.disconnect(this.audioCtx.destination);
47+
}
448
}

0 commit comments

Comments
 (0)