Skip to content

Commit

Permalink
Uniquesounds (#94)
Browse files Browse the repository at this point in the history
* generate unique profile for sounds on each client

* change volume
  • Loading branch information
pelikhan authored Jun 10, 2022
1 parent fea6c7d commit e711139
Show file tree
Hide file tree
Showing 16 changed files with 76 additions and 28 deletions.
1 change: 1 addition & 0 deletions tools/microbit-jukebox/accelerometer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ machine.addClientFactory(jacdac.SRV_ACCELEROMETER, devid => {
client.onTiltRight(() => basic.showArrow(ArrowNames.East))
client.onTiltUp(() => basic.showArrow(ArrowNames.North))
client.onTiltDown(() => basic.showArrow(ArrowNames.South))
return client
})
1 change: 1 addition & 0 deletions tools/microbit-jukebox/airpressure.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ machine.addClientFactory(jacdac.SRV_AIR_PRESSURE, devid => {
const reading = client.pressure()
machine.showNumber(IconNames.Chessboard, reading)
})
return client
})
5 changes: 3 additions & 2 deletions tools/microbit-jukebox/button.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ const buttonNotes = [
Note.C5,
]
let buttonIndex = 0
machine.addClientFactory(jacdac.SRV_BUTTON, devid => {
machine.addClientFactory(jacdac.SRV_BUTTON, (devid, srvid, options) => {
const bi = buttonIndex++
const down = buttonDownIcons[bi % buttonDownIcons.length]
const hold = buttonHoldIcons[bi % buttonHoldIcons.length]
Expand All @@ -41,9 +41,10 @@ machine.addClientFactory(jacdac.SRV_BUTTON, devid => {
})
client.onDown(() => {
basic.showIcon(down, 0)
machine.scheduleTone(tone)
machine.scheduleTone(tone, options)
})
client.onHold(() => {
basic.showIcon(hold, 0)
})
return client
})
1 change: 1 addition & 0 deletions tools/microbit-jukebox/buzzer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ machine.addClientFactory(jacdac.SRV_BUZZER, devid => {
const tone = machine.microbit.tone
client.playTone(tone, 100)
})
return client
})
1 change: 1 addition & 0 deletions tools/microbit-jukebox/gamepad.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,6 @@ machine.addClientFactory(jacdac.SRV_GAMEPAD, devid => {
// if (Math.abs(x) > 0.1 || Math.abs(y) > 0.1)
// mouseMove(x * maxSpeed, y * maxSpeed)
})
return client
})

1 change: 1 addition & 0 deletions tools/microbit-jukebox/humidity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ machine.addClientFactory(jacdac.SRV_HUMIDITY, devid => {
const reading = client.humidity()
machine.showNumber(IconNames.Umbrella, reading)
})
return client
})
1 change: 1 addition & 0 deletions tools/microbit-jukebox/led.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,5 @@ machine.addClientFactory(jacdac.SRV_LED, devid => {
}
})
client.setPixelColor(1, 0x00ff00)
return client
})
5 changes: 3 additions & 2 deletions tools/microbit-jukebox/lightlevel.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
machine.addClientFactory(jacdac.SRV_LIGHT_LEVEL, devid => {
machine.addClientFactory(jacdac.SRV_LIGHT_LEVEL, (devid, srvid, options) => {
const client = new modules.LightLevelClient(devid)
client.onLightLevelChangedBy(20, () => {
machine.plot(client.lightLevel(), 100)
machine.plot(client.lightLevel(), 100, options)
})
return client
})
5 changes: 3 additions & 2 deletions tools/microbit-jukebox/magneticfieldlevel.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
machine.addClientFactory(jacdac.SRV_MAGNETIC_FIELD_LEVEL, devid => {
machine.addClientFactory(jacdac.SRV_MAGNETIC_FIELD_LEVEL, (devid, srvid, options) => {
const client = new modules.MagneticFieldLevelClient(devid)
client.onStrengthChangedBy(5, () => {
const value = client.strength()
machine.plot(value, 100)
machine.plot(value, 100, options)
})
return client
})
5 changes: 3 additions & 2 deletions tools/microbit-jukebox/potentiometer.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
machine.addClientFactory(jacdac.SRV_POTENTIOMETER, devid => {
machine.addClientFactory(jacdac.SRV_POTENTIOMETER, (devid, srvid, options) => {
const client = new modules.PotentiometerClient(devid)
client.onPositionChangedBy(5, () => {
const current = client.position()
machine.plot(current, 100)
machine.plot(current, 100, options)
machine.microbit.emit(machine.POT_CHANGE, current)
})
return client
})
1 change: 1 addition & 0 deletions tools/microbit-jukebox/relay.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@ machine.addClientFactory(jacdac.SRV_RELAY, devid => {
else basic.showIcon(IconNames.No, 0)
})
})
return client
})
5 changes: 3 additions & 2 deletions tools/microbit-jukebox/rotaryencoder.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
machine.addClientFactory(jacdac.SRV_ROTARY_ENCODER, devid => {
machine.addClientFactory(jacdac.SRV_ROTARY_ENCODER, (devid, srvid, options) => {
const client = new modules.RotaryEncoderClient(devid)
let currentPosition = client.position()
if (!currentPosition) currentPosition = 0
Expand All @@ -10,6 +10,7 @@ machine.addClientFactory(jacdac.SRV_ROTARY_ENCODER, devid => {
currentPosition < newPosition ? machine.EVENT_A : machine.EVENT_B
)
currentPosition = newPosition
machine.plot(newPosition % clicks, clicks)
machine.plot(newPosition % clicks, clicks, options)
})
return client
})
69 changes: 51 additions & 18 deletions tools/microbit-jukebox/runtime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,16 +76,17 @@ namespace machine {
// tone player
namespace machine {
let nextTone: number
let nextWaveShape = WaveShape.Square
let nextToneEffect = SoundExpressionEffect.Tremolo
let nextToneInterpolation = InterpolationCurve.Linear
let nextToneOptions: SonifyOptions

function startTonePlayer() {
music.stopAllSounds()
control.runInBackground(() => {
while (nextTone) {
while (nextTone && nextToneOptions) {
const t = nextTone
const options = nextToneOptions

nextTone = 0
nextToneOptions = undefined

machine.microbit.playTone(Math.abs(t))

Expand All @@ -94,36 +95,46 @@ namespace machine {
const startf = t >= 0 ? t : 0
const endf = t < 0 ? -t : 0
const effect = music.createSoundEffect(
nextWaveShape,
options.waveShape,
startf,
endf,
255,
0,
options.startVolume,
255 - options.startVolume,
duration,
nextToneEffect,
nextToneInterpolation
options.effect,
options.interpolation
)
music.playSoundEffect(effect, SoundExpressionPlayMode.UntilDone)

basic.pause(20)
}
})
}
export function scheduleTone(f: number) {
export function scheduleTone(f: number, options: SonifyOptions) {
nextTone = f
nextToneOptions = options
startTonePlayer()
}

export function sonify(value: number, max: number) {
export class SonifyOptions {
constructor(
public waveShape: WaveShape,
public effect: SoundExpressionEffect,
public interpolation: InterpolationCurve,
public startVolume: number
) {}
}

function sonify(value: number, max: number, options: SonifyOptions) {
const fmin = 200
const fmax = 10000
const f = Math.map(Math.abs(value), 0, max, fmin, fmax)
scheduleTone(Math.sign(value) * f)
scheduleTone(Math.sign(value) * f, options)
}

export function plot(value: number, max: number) {
export function plot(value: number, max: number, options: SonifyOptions) {
led.plotBarGraph(value, max)
machine.sonify(value, max)
sonify(value, max, options)
}
}

Expand Down Expand Up @@ -207,14 +218,22 @@ namespace machine {
class ClientFactory {
constructor(
public serviceClass: number,
public handler: (devid: string, serviceIndex: number) => void
public handler: (
devid: string,
serviceIndex: number,
sonifyOptions: SonifyOptions
) => jacdac.Client
) {}
}
const factories: ClientFactory[] = []

export function addClientFactory(
serviceClass: number,
handler: (devid: string, serviceIndex: number) => void
handler: (
devid: string,
serviceIndex: number,
sonifyOptions: SonifyOptions
) => jacdac.Client
) {
console.log(`modules: register ${serviceClass}`)
factories.push(new ClientFactory(serviceClass, handler))
Expand All @@ -224,10 +243,24 @@ namespace machine {
if (devices) devices.forEach(startClients)
}

function startClient(d: jacdac.Device, serviceIndex: number) {
function startClient(
d: jacdac.Device,
serviceIndex: number
): jacdac.Client {
const serviceClass = d.serviceClassAt(serviceIndex)
const factory = factories.find(f => f.serviceClass === serviceClass)
if (factory) return factory.handler(d.deviceId, serviceIndex)
if (factory) {
// waveshape: 0..4
// effect: 0..3
// interpoliation: 0..2
const sonifyOptions = new SonifyOptions(
<WaveShape>Math.randomRange(0, 4),
<SoundExpressionEffect>Math.randomRange(0, 3),
<InterpolationCurve>Math.randomRange(0, 1),
Math.randomRange(0, 255)
)
return factory.handler(d.deviceId, serviceIndex, sonifyOptions)
}
return null
}

Expand Down
1 change: 1 addition & 0 deletions tools/microbit-jukebox/servo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,5 @@ machine.addClientFactory(jacdac.SRV_SERVO, devid => {
client.setAngle(angle)
}
)
return client
})
1 change: 1 addition & 0 deletions tools/microbit-jukebox/switch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ machine.addClientFactory(jacdac.SRV_SWITCH, devid => {
client.onOff(() => {
basic.showIcon(IconNames.Yes, 0)
})
return client
})
1 change: 1 addition & 0 deletions tools/microbit-jukebox/temperature.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ machine.addClientFactory(jacdac.SRV_TEMPERATURE, devid => {
const value = client.temperature()
machine.showNumber(IconNames.House, value)
})
return client
})

0 comments on commit e711139

Please sign in to comment.