Skip to content

Commit

Permalink
normalize audio analysis + fix ret type
Browse files Browse the repository at this point in the history
  • Loading branch information
dragazo committed Sep 19, 2024
1 parent 96eb278 commit 4451a1d
Showing 1 changed file with 22 additions and 15 deletions.
37 changes: 22 additions & 15 deletions extensions/BeatBlox/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

function clamp(x, a, b) { return x < a ? a : x > b ? b : x; }
function lerp(x, a, b) { return (1 - x) * a + x * b; }

const EFFECT_INFO = {
'Volume': { type: 'Volume' , identity: 1.0, params: x => ({ 'intensity': clamp(x, 0, 1) }) },
'Delay': { type: 'Delay', identity: 0.0, params: x => ({ 'delay': clamp(x, 0, 1), 'attenuation': 0.5 }) },
Expand All @@ -15,6 +16,11 @@
'Fan': { type: 'Tremolo', identity: 0.0, params: x => ({ 'rate': 15, 'intensity': Math.pow(clamp(x, 0, 1), 0.7) }) },
};

const ANALYSIS_INFO = {
'samples': { type: 'TimeSeries', transform: x => Array.from(x).map(v => (v - 128) / 128) },
'spectrum': { type: 'PowerSpectrum', transform: x => Array.from(x).map(v => v / 255) }
};

const DRUM_TO_NOTE = {
'kick': 'A1', 'kick #2': 'C2', 'snare': 'G1', 'side stick snare': 'C#2',
'open snare': 'E2', 'closed hi-hat': 'F#2', 'clap': 'Eb2', 'tom': 'C3',
Expand Down Expand Up @@ -56,7 +62,7 @@

const PREFETCH = (async () => {
try {
MIDI_DEVICES = (await audio.getAvailableMidiDevices()).map((x) => `${x}---(midi)`);
MIDI_DEVICES = (await audio.getAvailableMidiDevices()).map(x => `${x}---(midi)`);
} catch (e) {
console.error('failed to load midi devices', e);
}
Expand All @@ -73,7 +79,7 @@
console.log('beginning instrument pre-fetch...');
const tempTrack = '<<<temp-track>>>';
audio.createTrack(tempTrack);
await Promise.all(INSTRUMENTS.map((x) => audio.updateInstrument(tempTrack, x)));
await Promise.all(INSTRUMENTS.map(x => audio.updateInstrument(tempTrack, x)));
audio.removeTrack(tempTrack);
console.log('instrument pre-fetch completed');

Expand All @@ -86,7 +92,7 @@

function snapify(value) {
if (typeof (value.map) === 'function') {
return new List(value.map(x => snapify(x)));
return new List((Array.isArray(value) ? value : Array.from(value)).map(x => snapify(x)));
} else if (typeof (value) === 'object') {
const res = [];
for (const key in value) {
Expand All @@ -108,8 +114,8 @@
}

function parseNote(note) {
if (Array.isArray(note)) return note.map((x) => parseNote(x));
if (note.contents !== undefined) return note.contents.map((x) => parseNote(x));
if (Array.isArray(note)) return note.map(x => parseNote(x));
if (note.contents !== undefined) return note.contents.map(x => parseNote(x));
if (typeof (note) === 'number' && Number.isInteger(note)) return note;
if (typeof (note) !== 'string' || note === '') throw Error(`expected a note, got '${note}'`);
if (note === 'Rest') return NOTES[note];
Expand Down Expand Up @@ -154,8 +160,8 @@
return accidental ? -off : off;
}
function parseDrumNote(note) {
if (Array.isArray(note)) return note.map((x) => parseDrumNote(x));
if (note.contents !== undefined) return note.contents.map((x) => parseDrumNote(x));
if (Array.isArray(note)) return note.map(x => parseDrumNote(x));
if (note.contents !== undefined) return note.contents.map(x => parseDrumNote(x));
if (typeof (note) === 'number' && Number.isInteger(note)) return note;
if (typeof (note) !== 'string' || note === '') throw Error(`expected a drum note, got '${note}'`);
if (note === 'Rest') return NOTES[note];
Expand Down Expand Up @@ -240,6 +246,7 @@
'-',
new Extension.Palette.Block('playClip'),
new Extension.Palette.Block('queryClip'),
new Extension.Palette.Block('audioAnalysis'),
new Extension.Palette.Block('createClip'),
'-',
new Extension.Palette.Block('setAudioEffect'),
Expand All @@ -250,8 +257,6 @@
new Extension.Palette.Block('startRecording'),
new Extension.Palette.Block('finishRecording'),
new Extension.Palette.Block('isRecording'),
'-',
new Extension.Palette.Block('audioAnalysis'),
];
return [
new Extension.PaletteCategory('music', blocks, SpriteMorph),
Expand Down Expand Up @@ -368,7 +373,7 @@
await waitUntil(this.musicInfo.t - SCHEDULING_WINDOW);
}, { args: [], timeout: I32_MAX });
}),
new Extension.Block('queryClip', 'reporter', 'music', '%audioQuery of sound %snd', ['name'], function (query, rawSound) {
new Extension.Block('queryClip', 'reporter', 'music', '%audioQuery of sound %snd', ['samples'], function (query, rawSound) {
return this.runAsyncFn(async () => {
const sound = typeof(rawSound) === 'string' ? this.receiver.sounds.contents.find(x => x.name === rawSound) : rawSound;
if (!sound) throw Error(typeof(rawSound) === 'string' ? `unknown sound: "${rawSound}"` : 'input must be a sound');
Expand Down Expand Up @@ -399,6 +404,12 @@
}
}, { args: [], timeout: I32_MAX });
}),
new Extension.Block('audioAnalysis', 'reporter', 'music', 'output %audioAnalysis', ['samples'], function (analysis) {
const { type, transform } = ANALYSIS_INFO[analysis] || {};
if (type === undefined || transform === undefined) throw Error(`unknown audio analysis type: "${analysis}"`);

return snapify(transform(audio.analyzeAudio(ANALYSES[type])));
}),
new Extension.Block('createClip', 'reporter', 'music', 'samples %l at %n Hz', [null, 44100], function (samples, sampleRate) {
return this.runAsyncFn(async () => {
sampleRate = +sampleRate;
Expand Down Expand Up @@ -548,10 +559,6 @@
new Extension.Block('isRecording', 'predicate', 'music', 'recording?', [], function () {
return !!activeRecording;
}),
new Extension.Block('audioAnalysis', 'reporter', 'music', 'get output %audioAnalysis', ['TimeSeries'], function (ty) {
if (!ANALYSES[ty]) throw Error(`unknown audio analysis type: '${ty}'`);
return snapify(audio.analyzeAudio(ANALYSES[ty]));
}),
];
}

Expand Down Expand Up @@ -597,7 +604,7 @@
basicEnum('audioEffectAug', identityMap([...Object.keys(EFFECT_INFO), 'every', 'every active'])),
basicEnum('audioInput', identityMap([...MIDI_DEVICES, ...INPUT_DEVICES])),
basicEnum('io', identityMap(['input', 'output'])),
basicEnum('audioAnalysis', identityMap(Object.keys(ANALYSES))),
basicEnum('audioAnalysis', identityMap(Object.keys(ANALYSIS_INFO))),
];
}
}
Expand Down

0 comments on commit 4451a1d

Please sign in to comment.