Add sounds (only simulator for now)
This commit is contained in:
parent
a8a7267851
commit
ebbbe6e86c
@ -27,8 +27,9 @@ class MMap : public RefObject {
|
||||
|
||||
extern volatile bool paniced;
|
||||
|
||||
// Buffer and Image share representation.
|
||||
// Buffer, Sound, and Image share representation.
|
||||
typedef Buffer Image;
|
||||
typedef Buffer Sound;
|
||||
|
||||
}
|
||||
|
||||
|
@ -1,9 +1,12 @@
|
||||
{
|
||||
"Sound.buffer": "Returns the underlaying Buffer object.",
|
||||
"Sound.play": "Play sound with given volume.",
|
||||
"music": "Generation of music tones.",
|
||||
"music.beat": "Return the duration of a beat in milliseconds (the beat fraction).",
|
||||
"music.beat|param|fraction": "the fraction of the current whole note, eg: BeatFraction.Half",
|
||||
"music.changeTempoBy": "Change the tempo up or down by some amount of beats per minute (bpm).",
|
||||
"music.changeTempoBy|param|bpm": "The change in beats per minute to the tempo, eg: 20",
|
||||
"music.fromWAV": "Makes a sound bound to a buffer in WAV format.",
|
||||
"music.noteFrequency": "Get the frequency of a note.",
|
||||
"music.noteFrequency|param|name": "the note name, eg: Note.C",
|
||||
"music.playSound": "Start playing a sound and don't wait for it to finish.\nNotes are expressed as a string of characters with this format: NOTE[octave][:duration]",
|
||||
|
@ -42,5 +42,7 @@
|
||||
"music.stopAllSounds|block": "stop all sounds",
|
||||
"music.tempo|block": "tempo (bpm)",
|
||||
"music|block": "music",
|
||||
"{id:category}Music": "Music"
|
||||
"{id:category}Music": "Music",
|
||||
"{id:category}Sound": "Sound",
|
||||
"{id:category}Sounds": "Sounds"
|
||||
}
|
@ -6,7 +6,6 @@
|
||||
#include <sys/stat.h>
|
||||
#include <fcntl.h>
|
||||
|
||||
|
||||
#define NOTE_PAUSE 20
|
||||
|
||||
namespace music {
|
||||
@ -51,8 +50,7 @@ static void _stopSound() {
|
||||
writeDev(&cmd, sizeof(cmd));
|
||||
}
|
||||
|
||||
static void _playTone(uint16_t frequency, uint16_t duration, uint8_t volume)
|
||||
{
|
||||
static void _playTone(uint16_t frequency, uint16_t duration, uint8_t volume) {
|
||||
ToneCmd cmd;
|
||||
cmd.cmd = SOUND_CMD_TONE;
|
||||
cmd.vol = volume;
|
||||
@ -90,4 +88,31 @@ void playTone(int frequency, int ms) {
|
||||
}
|
||||
sleep_ms(1);
|
||||
}
|
||||
|
||||
|
||||
/** Makes a sound bound to a buffer in WAV format. */
|
||||
//%
|
||||
Sound fromWAV(Buffer buf) {
|
||||
incrRC(buf);
|
||||
return buf;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
//% fixedInstances
|
||||
namespace SoundMethods {
|
||||
|
||||
/** Returns the underlaying Buffer object. */
|
||||
//% property
|
||||
Buffer buffer(Sound snd) {
|
||||
incrRC(snd);
|
||||
return snd;
|
||||
}
|
||||
|
||||
/** Play sound with given volume. */
|
||||
//% promise
|
||||
void play(Sound snd, int volume) {
|
||||
// TODO
|
||||
}
|
||||
|
||||
}
|
@ -8,6 +8,8 @@
|
||||
"shims.d.ts",
|
||||
"melodies.ts",
|
||||
"music.ts",
|
||||
"sounds.jres",
|
||||
"sounds.ts",
|
||||
"ns.ts"
|
||||
],
|
||||
"testFiles": [
|
||||
|
17
libs/music/shims.d.ts
vendored
17
libs/music/shims.d.ts
vendored
@ -24,6 +24,23 @@ declare namespace music {
|
||||
//% blockNamespace=music
|
||||
//% weight=76 blockGap=8 shim=music::playTone
|
||||
function playTone(frequency: int32, ms: int32): void;
|
||||
|
||||
/** Makes a sound bound to a buffer in WAV format. */
|
||||
//% shim=music::fromWAV
|
||||
function fromWAV(buf: Buffer): Sound;
|
||||
}
|
||||
|
||||
|
||||
|
||||
//% fixedInstances
|
||||
declare interface Sound {
|
||||
/** Returns the underlaying Buffer object. */
|
||||
//% property shim=SoundMethods::buffer
|
||||
buffer: Buffer;
|
||||
|
||||
/** Play sound with given volume. */
|
||||
//% promise shim=SoundMethods::play
|
||||
play(volume: int32): void;
|
||||
}
|
||||
|
||||
// Auto-generated. Do not edit. Really.
|
||||
|
134
libs/music/sounds.jres
Normal file
134
libs/music/sounds.jres
Normal file
File diff suppressed because one or more lines are too long
256
libs/music/sounds.ts
Normal file
256
libs/music/sounds.ts
Normal file
@ -0,0 +1,256 @@
|
||||
namespace sounds {
|
||||
//% fixedInstance jres
|
||||
export const animalsCatPurr = music.fromWAV(hex``);
|
||||
//% fixedInstance jres
|
||||
export const animalsDogBark1 = music.fromWAV(hex``);
|
||||
//% fixedInstance jres
|
||||
export const animalsDogBark2 = music.fromWAV(hex``);
|
||||
//% fixedInstance jres
|
||||
export const animalsDogGrowl = music.fromWAV(hex``);
|
||||
//% fixedInstance jres
|
||||
export const animalsDogSniff = music.fromWAV(hex``);
|
||||
//% fixedInstance jres
|
||||
export const animalsDogWhine = music.fromWAV(hex``);
|
||||
//% fixedInstance jres
|
||||
export const animalsElephantCall = music.fromWAV(hex``);
|
||||
//% fixedInstance jres
|
||||
export const animalsInsectBuzz1 = music.fromWAV(hex``);
|
||||
//% fixedInstance jres
|
||||
export const animalsInsectBuzz2 = music.fromWAV(hex``);
|
||||
//% fixedInstance jres
|
||||
export const animalsInsectChirp = music.fromWAV(hex``);
|
||||
//% fixedInstance jres
|
||||
export const animalsSnakeHiss = music.fromWAV(hex``);
|
||||
//% fixedInstance jres
|
||||
export const animalsSnakeRattle = music.fromWAV(hex``);
|
||||
//% fixedInstance jres
|
||||
export const animalsTRexRoar = music.fromWAV(hex``);
|
||||
//% fixedInstance jres
|
||||
export const colorsBlack = music.fromWAV(hex``);
|
||||
//% fixedInstance jres
|
||||
export const colorsBlue = music.fromWAV(hex``);
|
||||
//% fixedInstance jres
|
||||
export const colorsBrown = music.fromWAV(hex``);
|
||||
//% fixedInstance jres
|
||||
export const colorsGreen = music.fromWAV(hex``);
|
||||
//% fixedInstance jres
|
||||
export const colorsRed = music.fromWAV(hex``);
|
||||
//% fixedInstance jres
|
||||
export const colorsWhite = music.fromWAV(hex``);
|
||||
//% fixedInstance jres
|
||||
export const colorsYellow = music.fromWAV(hex``);
|
||||
//% fixedInstance jres
|
||||
export const communicationBravo = music.fromWAV(hex``);
|
||||
//% fixedInstance jres
|
||||
export const communicationEv3 = music.fromWAV(hex``);
|
||||
//% fixedInstance jres
|
||||
export const communicationFantastic = music.fromWAV(hex``);
|
||||
//% fixedInstance jres
|
||||
export const communicationGameOver = music.fromWAV(hex``);
|
||||
//% fixedInstance jres
|
||||
export const communicationGo = music.fromWAV(hex``);
|
||||
//% fixedInstance jres
|
||||
export const communicationGoodJob = music.fromWAV(hex``);
|
||||
//% fixedInstance jres
|
||||
export const communicationGood = music.fromWAV(hex``);
|
||||
//% fixedInstance jres
|
||||
export const communicationGoodbye = music.fromWAV(hex``);
|
||||
//% fixedInstance jres
|
||||
export const communicationHello = music.fromWAV(hex``);
|
||||
//% fixedInstance jres
|
||||
export const communicationHi = music.fromWAV(hex``);
|
||||
//% fixedInstance jres
|
||||
export const communicationLego = music.fromWAV(hex``);
|
||||
//% fixedInstance jres
|
||||
export const communicationMindstorms = music.fromWAV(hex``);
|
||||
//% fixedInstance jres
|
||||
export const communicationMorning = music.fromWAV(hex``);
|
||||
//% fixedInstance jres
|
||||
export const communicationNo = music.fromWAV(hex``);
|
||||
//% fixedInstance jres
|
||||
export const communicationOkay = music.fromWAV(hex``);
|
||||
//% fixedInstance jres
|
||||
export const communicationOkeyDokey = music.fromWAV(hex``);
|
||||
//% fixedInstance jres
|
||||
export const communicationSorry = music.fromWAV(hex``);
|
||||
//% fixedInstance jres
|
||||
export const communicationThankYou = music.fromWAV(hex``);
|
||||
//% fixedInstance jres
|
||||
export const communicationYes = music.fromWAV(hex``);
|
||||
//% fixedInstance jres
|
||||
export const expressionsBoing = music.fromWAV(hex``);
|
||||
//% fixedInstance jres
|
||||
export const expressionsBoo = music.fromWAV(hex``);
|
||||
//% fixedInstance jres
|
||||
export const expressionsCheering = music.fromWAV(hex``);
|
||||
//% fixedInstance jres
|
||||
export const expressionsCrunching = music.fromWAV(hex``);
|
||||
//% fixedInstance jres
|
||||
export const expressionsCrying = music.fromWAV(hex``);
|
||||
//% fixedInstance jres
|
||||
export const expressionsFanfare = music.fromWAV(hex``);
|
||||
//% fixedInstance jres
|
||||
export const expressionsKungFu = music.fromWAV(hex``);
|
||||
//% fixedInstance jres
|
||||
export const expressionsLaughing1 = music.fromWAV(hex``);
|
||||
//% fixedInstance jres
|
||||
export const expressionsLaughing2 = music.fromWAV(hex``);
|
||||
//% fixedInstance jres
|
||||
export const expressionsMagicWand = music.fromWAV(hex``);
|
||||
//% fixedInstance jres
|
||||
export const expressionsOuch = music.fromWAV(hex``);
|
||||
//% fixedInstance jres
|
||||
export const expressionsShouting = music.fromWAV(hex``);
|
||||
//% fixedInstance jres
|
||||
export const expressionsSmack = music.fromWAV(hex``);
|
||||
//% fixedInstance jres
|
||||
export const expressionsSneezing = music.fromWAV(hex``);
|
||||
//% fixedInstance jres
|
||||
export const expressionsSnoring = music.fromWAV(hex``);
|
||||
//% fixedInstance jres
|
||||
export const expressionsUhOh = music.fromWAV(hex``);
|
||||
//% fixedInstance jres
|
||||
export const informationActivate = music.fromWAV(hex``);
|
||||
//% fixedInstance jres
|
||||
export const informationAnalyze = music.fromWAV(hex``);
|
||||
//% fixedInstance jres
|
||||
export const informationBackwards = music.fromWAV(hex``);
|
||||
//% fixedInstance jres
|
||||
export const informationColor = music.fromWAV(hex``);
|
||||
//% fixedInstance jres
|
||||
export const informationDetected = music.fromWAV(hex``);
|
||||
//% fixedInstance jres
|
||||
export const informationDown = music.fromWAV(hex``);
|
||||
//% fixedInstance jres
|
||||
export const informationErrorAlarm = music.fromWAV(hex``);
|
||||
//% fixedInstance jres
|
||||
export const informationError = music.fromWAV(hex``);
|
||||
//% fixedInstance jres
|
||||
export const informationFlashing = music.fromWAV(hex``);
|
||||
//% fixedInstance jres
|
||||
export const informationForward = music.fromWAV(hex``);
|
||||
//% fixedInstance jres
|
||||
export const informationLeft = music.fromWAV(hex``);
|
||||
//% fixedInstance jres
|
||||
export const informationObject = music.fromWAV(hex``);
|
||||
//% fixedInstance jres
|
||||
export const informationRight = music.fromWAV(hex``);
|
||||
//% fixedInstance jres
|
||||
export const informationSearching = music.fromWAV(hex``);
|
||||
//% fixedInstance jres
|
||||
export const informationStart = music.fromWAV(hex``);
|
||||
//% fixedInstance jres
|
||||
export const informationStop = music.fromWAV(hex``);
|
||||
//% fixedInstance jres
|
||||
export const informationTouch = music.fromWAV(hex``);
|
||||
//% fixedInstance jres
|
||||
export const informationTurn = music.fromWAV(hex``);
|
||||
//% fixedInstance jres
|
||||
export const informationUp = music.fromWAV(hex``);
|
||||
//% fixedInstance jres
|
||||
export const mechanicalAirRelease = music.fromWAV(hex``);
|
||||
//% fixedInstance jres
|
||||
export const mechanicalAirbrake = music.fromWAV(hex``);
|
||||
//% fixedInstance jres
|
||||
export const mechanicalBackingAlert = music.fromWAV(hex``);
|
||||
//% fixedInstance jres
|
||||
export const mechanicalBlip1 = music.fromWAV(hex``);
|
||||
//% fixedInstance jres
|
||||
export const mechanicalBlip2 = music.fromWAV(hex``);
|
||||
//% fixedInstance jres
|
||||
export const mechanicalBlip3 = music.fromWAV(hex``);
|
||||
//% fixedInstance jres
|
||||
export const mechanicalBlip4 = music.fromWAV(hex``);
|
||||
//% fixedInstance jres
|
||||
export const mechanicalHorn1 = music.fromWAV(hex``);
|
||||
//% fixedInstance jres
|
||||
export const mechanicalHorn2 = music.fromWAV(hex``);
|
||||
//% fixedInstance jres
|
||||
export const mechanicalLaser = music.fromWAV(hex``);
|
||||
//% fixedInstance jres
|
||||
export const mechanicalMotorIdle = music.fromWAV(hex``);
|
||||
//% fixedInstance jres
|
||||
export const mechanicalMotorStart = music.fromWAV(hex``);
|
||||
//% fixedInstance jres
|
||||
export const mechanicalMotorStop = music.fromWAV(hex``);
|
||||
//% fixedInstance jres
|
||||
export const mechanicalRatchet = music.fromWAV(hex``);
|
||||
//% fixedInstance jres
|
||||
export const mechanicalSonar = music.fromWAV(hex``);
|
||||
//% fixedInstance jres
|
||||
export const mechanicalTickTack = music.fromWAV(hex``);
|
||||
//% fixedInstance jres
|
||||
export const mechanicalWalk = music.fromWAV(hex``);
|
||||
//% fixedInstance jres
|
||||
export const movementsArm1 = music.fromWAV(hex``);
|
||||
//% fixedInstance jres
|
||||
export const movementsArm2 = music.fromWAV(hex``);
|
||||
//% fixedInstance jres
|
||||
export const movementsArm3 = music.fromWAV(hex``);
|
||||
//% fixedInstance jres
|
||||
export const movementsArm4 = music.fromWAV(hex``);
|
||||
//% fixedInstance jres
|
||||
export const movementsDropLoad = music.fromWAV(hex``);
|
||||
//% fixedInstance jres
|
||||
export const movementsLiftLoad = music.fromWAV(hex``);
|
||||
//% fixedInstance jres
|
||||
export const movementsServo1 = music.fromWAV(hex``);
|
||||
//% fixedInstance jres
|
||||
export const movementsServo2 = music.fromWAV(hex``);
|
||||
//% fixedInstance jres
|
||||
export const movementsServo3 = music.fromWAV(hex``);
|
||||
//% fixedInstance jres
|
||||
export const movementsServo4 = music.fromWAV(hex``);
|
||||
//% fixedInstance jres
|
||||
export const movementsSlideLoad = music.fromWAV(hex``);
|
||||
//% fixedInstance jres
|
||||
export const movementsSnap = music.fromWAV(hex``);
|
||||
//% fixedInstance jres
|
||||
export const movementsSpeedDown = music.fromWAV(hex``);
|
||||
//% fixedInstance jres
|
||||
export const movementsSpeedIdle = music.fromWAV(hex``);
|
||||
//% fixedInstance jres
|
||||
export const movementsSpeedUp = music.fromWAV(hex``);
|
||||
//% fixedInstance jres
|
||||
export const movementsSpeeding = music.fromWAV(hex``);
|
||||
//% fixedInstance jres
|
||||
export const numbersEight = music.fromWAV(hex``);
|
||||
//% fixedInstance jres
|
||||
export const numbersFive = music.fromWAV(hex``);
|
||||
//% fixedInstance jres
|
||||
export const numbersFour = music.fromWAV(hex``);
|
||||
//% fixedInstance jres
|
||||
export const numbersNine = music.fromWAV(hex``);
|
||||
//% fixedInstance jres
|
||||
export const numbersOne = music.fromWAV(hex``);
|
||||
//% fixedInstance jres
|
||||
export const numbersSeven = music.fromWAV(hex``);
|
||||
//% fixedInstance jres
|
||||
export const numbersSix = music.fromWAV(hex``);
|
||||
//% fixedInstance jres
|
||||
export const numbersTen = music.fromWAV(hex``);
|
||||
//% fixedInstance jres
|
||||
export const numbersThree = music.fromWAV(hex``);
|
||||
//% fixedInstance jres
|
||||
export const numbersTwo = music.fromWAV(hex``);
|
||||
//% fixedInstance jres
|
||||
export const numbersZero = music.fromWAV(hex``);
|
||||
//% fixedInstance jres
|
||||
export const systemClick = music.fromWAV(hex``);
|
||||
//% fixedInstance jres
|
||||
export const systemConfirm = music.fromWAV(hex``);
|
||||
//% fixedInstance jres
|
||||
export const systemConnect = music.fromWAV(hex``);
|
||||
//% fixedInstance jres
|
||||
export const systemDownload = music.fromWAV(hex``);
|
||||
//% fixedInstance jres
|
||||
export const systemGeneralAlert = music.fromWAV(hex``);
|
||||
//% fixedInstance jres
|
||||
export const systemOverpower = music.fromWAV(hex``);
|
||||
//% fixedInstance jres
|
||||
export const systemPowerDown = music.fromWAV(hex``);
|
||||
//% fixedInstance jres
|
||||
export const systemReady = music.fromWAV(hex``);
|
||||
//% fixedInstance jres
|
||||
export const systemStartUp = music.fromWAV(hex``);
|
||||
}
|
@ -1,7 +1,9 @@
|
||||
#!/usr/bin/env node
|
||||
|
||||
const fs = require("fs")
|
||||
const rsf = fs.readFileSync(process.argv[2])
|
||||
|
||||
function convertFile(fn) {
|
||||
const rsf = fs.readFileSync(fn)
|
||||
|
||||
const fmt = rsf.readInt16BE(0) // 0x0100
|
||||
if (fmt != 0x0100) {
|
||||
@ -35,12 +37,39 @@ wavHd.writeInt16LE(8, 34) // bits per sample
|
||||
writeMark(36, 'data')
|
||||
wavHd.writeInt32LE(datasize, 40)
|
||||
|
||||
const wav = Buffer.concat([wavHd, rsf.slice(8)])
|
||||
let wav = Buffer.concat([wavHd, rsf.slice(8)])
|
||||
return wav
|
||||
}
|
||||
|
||||
console.log("writing out.wav; " + samplerate + "Hz")
|
||||
fs.writeFileSync("out.wav", wav)
|
||||
let out = {
|
||||
"*": {
|
||||
namespace: "sounds",
|
||||
dataEncoding: "base64",
|
||||
mimeType: "audio/wav"
|
||||
}
|
||||
}
|
||||
let ts = "namespace sounds {\n"
|
||||
let bf
|
||||
for (let i = 2; i < process.argv.length; ++i) {
|
||||
let fn = process.argv[i]
|
||||
let m = /([^\/]+\/[^/]+)\.rsf$/.exec(fn)
|
||||
let bn = m[1]
|
||||
bf = convertFile(fn)
|
||||
bn = bn.replace(/[\/\-]/g, " ")
|
||||
.toLowerCase()
|
||||
.replace(/(\d)\s+/g, (f, n) => n)
|
||||
.replace(/\s+(.)/g, (f, l) => l.toUpperCase())
|
||||
out[bn] = bf.toString("base64")
|
||||
ts += ` //% fixedInstance jres\n`
|
||||
ts += ` export const ${bn} = music.fromWAV(hex\`\`);\n`
|
||||
}
|
||||
ts += `}\n`
|
||||
|
||||
if (require("os").platform() == "darwin")
|
||||
require('child_process').execSync("afplay out.wav")
|
||||
fs.writeFileSync("out.json", JSON.stringify(out, null, 4))
|
||||
fs.writeFileSync("out.ts", ts)
|
||||
fs.writeFileSync("out.wav", bf)
|
||||
|
||||
//if (require("os").platform() == "darwin")
|
||||
// require('child_process').execSync("afplay out.wav")
|
||||
|
||||
// TODO also play on Windows
|
||||
|
33
sim/state/sounds.ts
Normal file
33
sim/state/sounds.ts
Normal file
@ -0,0 +1,33 @@
|
||||
|
||||
namespace pxsim.music {
|
||||
export function fromWAV(buf: RefBuffer) {
|
||||
return incr(buf)
|
||||
}
|
||||
}
|
||||
|
||||
namespace pxsim.SoundMethods {
|
||||
|
||||
export function buffer(buf: RefBuffer) {
|
||||
return incr(buf)
|
||||
}
|
||||
|
||||
export function uint8ArrayToString(input: Uint8Array) {
|
||||
let len = input.length;
|
||||
let res = ""
|
||||
for (let i = 0; i < len; ++i)
|
||||
res += String.fromCharCode(input[i]);
|
||||
return res;
|
||||
}
|
||||
|
||||
export function play(buf: RefBuffer, volume: number) {
|
||||
return new Promise<void>(resolve => {
|
||||
let url = "data:audio/wav;base64," + btoa(uint8ArrayToString(buf.data))
|
||||
let audio = new Audio(url)
|
||||
audio.onended = () => {
|
||||
resolve()
|
||||
}
|
||||
audio.play()
|
||||
})
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user