diff --git a/README.md b/README.md index fd62e07..c66e0fc 100644 --- a/README.md +++ b/README.md @@ -23,6 +23,7 @@ Example usage import notePlayer from "noteplayer.js"; const np = new notePlayer(); +np.init(); np.setFrequency(440); // Sets frequency np.play(); // Plays note np.stop(); // Stops note @@ -31,6 +32,8 @@ np.stop(); // Stops note Functions ```js +init(): void; + setOscillatorDefaultSettings(): void; setOscillatorType(type: OscillatorType): void; diff --git a/demo/src/components/PlayNote.vue b/demo/src/components/PlayNote.vue index 5282cb5..ca608c6 100644 --- a/demo/src/components/PlayNote.vue +++ b/demo/src/components/PlayNote.vue @@ -150,8 +150,12 @@ const np = new notePlayer() const is_playing = ref(false) const play_button_text = computed(() => (!is_playing.value ? 'Play note' : 'Playing note...')) - +const np_initialized = ref(false) function playNote() { + if (!np_initialized.value) { + np.init() + np_initialized.value = true + } if (!is_playing.value) { is_playing.value = true np.play() diff --git a/dist/index.d.ts b/dist/index.d.ts index 1a45206..b0fa995 100644 --- a/dist/index.d.ts +++ b/dist/index.d.ts @@ -10,6 +10,7 @@ declare class notePlayer { private noteNames; private noteNameRegex; constructor(); + init(): void; setOscillatorDefaultSettings(): void; setOscillatorType(type: OscillatorType): void; setFrequency(frequency: number): void; diff --git a/dist/index.js b/dist/index.js index 2e32cba..661941a 100644 --- a/dist/index.js +++ b/dist/index.js @@ -32,6 +32,8 @@ var notePlayer = class { this.oscillator = this.audioCtx.createOscillator(); this.oscillator.connect(this.gainNode); this.setOscillatorDefaultSettings(); + } + init() { this.oscillator.start(); } setOscillatorDefaultSettings() { diff --git a/package-lock.json b/package-lock.json index 82feb3a..68766c8 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "noteplayer.js", - "version": "0.0.61", + "version": "0.0.64", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "noteplayer.js", - "version": "0.0.61", + "version": "0.0.64", "license": "ISC", "devDependencies": { "tsup": "^8.4.0", diff --git a/package.json b/package.json index 981e650..5e3d352 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "noteplayer.js", - "version": "0.0.63", + "version": "0.0.64", "description": "Play, tune, and transform notes and frequencies in a snap!", "main": "./dist/index.js", "types": "./dist/index.d.ts", diff --git a/src/index.ts b/src/index.ts index 4d9eb83..4e489a3 100644 --- a/src/index.ts +++ b/src/index.ts @@ -32,6 +32,9 @@ export default class notePlayer { this.oscillator.connect(this.gainNode); this.setOscillatorDefaultSettings(); + } + + init() { this.oscillator.start(); }