Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added exports #378

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 6 additions & 8 deletions modules/singer/playground/pages/Voice.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Voice } from '@/core/voice';
import SynthUtils from '@/core/synthUtils';
import * as Tone from 'tone';
import { _state, noteValueToSeconds, _defaultSynth, _polySynth } from '@/singer';
import { state, noteValueToSeconds, defaultSynth, polySynth } from '@/singer';
import { setupSynthUtils } from '@/core/synthUtils';
import { injected } from '@/index';

Expand All @@ -18,26 +18,25 @@ await (async () => {
'audio.piano': getAsset('audio.piano')!,
'audio.snare': getAsset('audio.snare')!,
};

})();

function _getSynth(synthType: string) {
switch (synthType) {
case 'polysynth':
return _polySynth;
return polySynth;
}
return _defaultSynth;
return defaultSynth;
}

async function playSynth(synthType: string) {
await Tone.start();
const synth = _getSynth(synthType);
_state.notesPlayed = 0;
state.notesPlayed = 0;
console.log('playing c4 using', synthType);
const now = Tone.now();
let offset = noteValueToSeconds(_state.notesPlayed);
let offset = noteValueToSeconds(state.notesPlayed);
synth.triggerAttackRelease('c4', '4n', now + offset);
_state.notesPlayed += 4;
state.notesPlayed += 4;
}

async function voice() {
Expand All @@ -61,7 +60,6 @@ async function voice() {
// }
// }).toDestination();


// _state.notesPlayed = 0;
// const now = Tone.now();
// let offset = noteValueToSeconds(_state.notesPlayed);
Expand Down
39 changes: 20 additions & 19 deletions modules/singer/src/singer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ const currentpitch = new CurrentPitch(testKeySignature, new Temperament(), 1, 4)
const _stateObj = { ..._defaultSynthStateValues };

/** Proxy to the synth state parameters. */
const _state = new Proxy(_stateObj, {
export const state = new Proxy(_stateObj, {
set: (_, key, value) => {
if (key === 'beatsPerMinute') {
_stateObj.beatsPerMinute = value;
Expand All @@ -37,7 +37,8 @@ const _state = new Proxy(_stateObj, {
});

/** Default synth **/
const _defaultSynth = new Tone.Synth().toDestination();
export const defaultSynth = new Tone.Synth().toDestination();
export const polySynth = new Tone.PolySynth().toDestination();

// -- private functions ----------------------------------------------------------------------------

Expand All @@ -46,7 +47,7 @@ const _defaultSynth = new Tone.Synth().toDestination();
* @param noteValue - 1/4, 1/8 etc.
**/
export function noteValueToSeconds(noteValue: number): number {
return (60 / _state.beatsPerMinute) * (noteValue / _state.beat);
return (60 / state.beatsPerMinute) * (noteValue / state.beat);
}

// -- public functions -----------------------------------------------------------------------------
Expand Down Expand Up @@ -78,20 +79,20 @@ export class ElementTestSynth extends ElementStatement {
if ((params['mode'] as number) === 1) {
for (let modalPitch = 1; modalPitch <= testKeySignature.modeLength + 1; modalPitch++) {
const now = Tone.now();
const offset = noteValueToSeconds(_state.notesPlayed);
const offset = noteValueToSeconds(state.notesPlayed);
const noteTup = testKeySignature.modalPitchToLetter(modalPitch - 1);
let octave = currentpitch.octave;
const fullNote = noteTup[0] + (octave + noteTup[1]);
_defaultSynth.triggerAttackRelease(fullNote, noteValue, now + offset);
defaultSynth.triggerAttackRelease(fullNote, noteValue, now + offset);
console.log('noteTup', noteTup, 'fullNote', fullNote);
octave += noteTup[1];
console.log('octave', octave);
_state.notesPlayed += 1 / 8;
state.notesPlayed += 1 / 8;
}
} else if ((params['mode'] as number) === 2) {
for (let i = 0; i < 8; i++) {
const now = Tone.now();
const offset = noteValueToSeconds(_state.notesPlayed);
const offset = noteValueToSeconds(state.notesPlayed);
let note = testKeySignature.modalPitchToLetter(
parseInt(
testKeySignature.genericNoteNameConvertToType(
Expand All @@ -101,9 +102,9 @@ export class ElementTestSynth extends ElementStatement {
) - 1,
);
const fullNote = note[0] + currentpitch.octave;
_defaultSynth.triggerAttackRelease(fullNote, noteValue, now + offset);
defaultSynth.triggerAttackRelease(fullNote, noteValue, now + offset);
console.log('fullNote:', fullNote, 'noteTup:', note);
_state.notesPlayed += 1 / 8;
state.notesPlayed += 1 / 8;
currentpitch.applyScalarTransposition(1);
}
}
Expand All @@ -127,12 +128,12 @@ export class ElementPlayNote extends ElementStatement {
const duration = params['duration'] as number;
const now = Tone.now();
const noteValue = duration + 'n'; //making the duration a string
const offset = noteValueToSeconds(_state.notesPlayed);
const offset = noteValueToSeconds(state.notesPlayed);
const noteTup = testKeySignature.modalPitchToLetter((params['pitch'] as number) - 1); //getting the note from a list in keySignature.ts
const fullNote = noteTup[0] + /*params['octave'] as number*/ (4 + noteTup[1]); //adding the octave (in this case 4) to the note
_defaultSynth.triggerAttackRelease(fullNote, noteValue, now + offset); //playing the note
defaultSynth.triggerAttackRelease(fullNote, noteValue, now + offset); //playing the note
console.log('noteTup', noteTup, 'fullNote', fullNote); //printing out the note
_state.notesPlayed += 1 / duration;
state.notesPlayed += 1 / duration;
}
}

Expand All @@ -143,7 +144,7 @@ export class PlayGenericNoteName extends ElementStatement {

onVisit(params: { [key: string]: TData }): void {
const now = Tone.now();
const offset = noteValueToSeconds(_state.notesPlayed);
const offset = noteValueToSeconds(state.notesPlayed);

const note = params['name'] as string;
const octave = currentpitch.octave;
Expand All @@ -154,8 +155,8 @@ export class PlayGenericNoteName extends ElementStatement {
); //converts generic note to letter note, ie. n0 -----> d
let fullNote = full[0] + (octave + full[1]);
console.log(fullNote);
_defaultSynth.triggerAttackRelease(fullNote, '4n', now + offset); //playing the note
_state.notesPlayed += 1 / 4;
defaultSynth.triggerAttackRelease(fullNote, '4n', now + offset); //playing the note
state.notesPlayed += 1 / 4;
}
}

Expand All @@ -169,7 +170,7 @@ export class PlayInterval extends ElementStatement {

for (let i = 0; i < 2; i++) {
const now = Tone.now();
let offset = noteValueToSeconds(_state.notesPlayed);
let offset = noteValueToSeconds(state.notesPlayed);

let note = testKeySignature.modalPitchToLetter(
parseInt(
Expand All @@ -181,9 +182,9 @@ export class PlayInterval extends ElementStatement {
);

console.log(note[0] + currentpitch.octave);
_defaultSynth.triggerAttackRelease(note[0] + currentpitch.octave, '8n', now + offset);
defaultSynth.triggerAttackRelease(note[0] + currentpitch.octave, '8n', now + offset);

_state.notesPlayed += 1 / 8;
state.notesPlayed += 1 / 8;
currentpitch.applyScalarTransposition(max);
}
currentpitch._genericName = 'n1'; //resets
Expand All @@ -203,6 +204,6 @@ export class ElementResetNotesPlayed extends ElementStatement {
* Resets notesPlayed counter to 0
*/
onVisit(): void {
_state.notesPlayed = 0;
state.notesPlayed = 0;
}
}