From 955b67b6b84ef33f80347f2de07e83f97018199a Mon Sep 17 00:00:00 2001 From: "Matthias L. Jugel" Date: Thu, 27 Oct 2016 19:48:42 +0200 Subject: [PATCH] rev. 0.2 sound patches --- libs/calliope-test/calliope.ts | 169 ++++++++++++++++++++++++++++----- libs/core/music.cpp | 28 ++++++ libs/core/music.ts | 48 +++++----- libs/core/pxt.json | 1 + libs/core/shims.d.ts | 21 ++++ 5 files changed, 218 insertions(+), 49 deletions(-) create mode 100644 libs/core/music.cpp diff --git a/libs/calliope-test/calliope.ts b/libs/calliope-test/calliope.ts index 44f2091d..2b7b5dad 100644 --- a/libs/calliope-test/calliope.ts +++ b/libs/calliope-test/calliope.ts @@ -1,54 +1,173 @@ -/*basic.showString("RGB") -basic.setLedColor(Colors.Blue) -basic.pause(500) -basic.setLedColor(Colors.Red) -basic.pause(500) -basic.setLedColor(Colors.Green) -basic.pause(500) -basic.setLedColor(Colors.Violet) -basic.pause(500) +basic.showString("RGB"); +basic.setLedColor(Colors.Blue); +basic.pause(500); +basic.setLedColor(Colors.Red); +basic.pause(500); +basic.setLedColor(Colors.Green); +basic.pause(500); +basic.setLedColor(Colors.Violet); +basic.pause(500); basic.setLedColor(0); -basic.showString("Gesten") +basic.showString("Gesten"); input.onGesture(Gesture.Shake, () => { basic.showString("S") -}) +}); input.onGesture(Gesture.LogoUp, () => { basic.showString("U") -}) +}); input.onGesture(Gesture.LogoDown, () => { basic.showString("D") -}) +}); input.onGesture(Gesture.ScreenUp, () => { basic.showString("+") -}) +}); input.onGesture(Gesture.TiltRight, () => { basic.showString("R") -}) +}); input.onGesture(Gesture.FreeFall, () => { basic.showString("F") -}) +}); input.onGesture(Gesture.ScreenDown, () => { basic.showString("-") -}) +}); input.onGesture(Gesture.TiltLeft, () => { basic.showString("L") -}) +}); input.onGesture(Gesture.ThreeG, () => { basic.showString("3") -}) +}); input.onGesture(Gesture.SixG, () => { basic.showString("6") -}) -*/ +}); input.onPinPressed(TouchPin.P0, () => { basic.showNumber(0) -}) +}); input.onPinPressed(TouchPin.P1, () => { basic.showNumber(1) -}) +}); input.onPinPressed(TouchPin.P2, () => { basic.showNumber(2) -}) +}); input.onPinPressed(TouchPin.P3, () => { basic.showNumber(3) -}) +}); + +basic.showString("Sound"); +music.setTempo(150); +let whole = music.beat(BeatFraction.Whole); +function note(n: Note, l: BeatFraction): number[] { + return [music.noteFrequency(n), music.beat(l)]; +} + +function getNoteName(frequency: number): string { + switch (frequency) { + case 262: + return "C"; + case 277: + return "CSharp"; + case 294: + return "D"; + case 311: + return "Eb"; + case 330: + return "E"; + case 349: + return "F"; + case 370: + return "FSharp"; + case 392: + return "G"; + case 415: + return "GSharp"; + case 440: + return "A"; + case 466: + return "Bb"; + case 494: + return "B"; + case 131: + return "C3"; + case 139: + return "CSharp3"; + case 147: + return "D3"; + case 156: + return "Eb3"; + case 165: + return "E3"; + case 175: + return "F3"; + case 185: + return "FSharp3"; + case 196: + return "G3"; + case 208: + return "GSharp3"; + case 220: + return "A3"; + case 233: + return "Bb3"; + case 247: + return "B3"; + case 523: + return "C5"; + case 555: + return "CSharp5"; + case 587: + return "D5"; + case 622: + return "Eb5"; + case 659: + return "E5"; + case 698: + return "F5"; + case 740: + return "FSharp5"; + case 784: + return "G5"; + case 831: + return "GSharp5"; + case 880: + return "A5"; + case 932: + return "Bb5"; + case 989: + return "B5"; + default: + return "?"; + } +} + +var notes = [ + note(Note.E, BeatFraction.Quarter), note(Note.E, BeatFraction.Quarter), note(Note.F, BeatFraction.Quarter), + note(Note.G, BeatFraction.Quarter), note(Note.G, BeatFraction.Quarter), note(Note.F, BeatFraction.Quarter), + note(Note.E, BeatFraction.Quarter), note(Note.D, BeatFraction.Quarter), note(Note.C, BeatFraction.Quarter), + note(Note.C, BeatFraction.Quarter), note(Note.D, BeatFraction.Quarter), note(Note.E, BeatFraction.Quarter), + note(Note.E, BeatFraction.Quarter + BeatFraction.Eighth), + note(Note.D, BeatFraction.Eighth), note(Note.D, BeatFraction.Half), + note(Note.E, BeatFraction.Quarter), note(Note.E, BeatFraction.Quarter), note(Note.F, BeatFraction.Quarter), + note(Note.G, BeatFraction.Quarter), note(Note.G, BeatFraction.Quarter), note(Note.F, BeatFraction.Quarter), + note(Note.E, BeatFraction.Quarter), note(Note.D, BeatFraction.Quarter), note(Note.C, BeatFraction.Quarter), + note(Note.C, BeatFraction.Quarter), note(Note.D, BeatFraction.Quarter), note(Note.E, BeatFraction.Quarter), + note(Note.D, BeatFraction.Quarter + BeatFraction.Eighth), + note(Note.C, BeatFraction.Eighth), note(Note.C, BeatFraction.Half), + note(Note.D, BeatFraction.Quarter), note(Note.D, BeatFraction.Quarter), note(Note.E, BeatFraction.Quarter), + note(Note.C, BeatFraction.Quarter), note(Note.D, BeatFraction.Quarter), + note(Note.E, BeatFraction.Eighth), note(Note.F, BeatFraction.Eighth), + note(Note.E, BeatFraction.Quarter), note(Note.C, BeatFraction.Quarter), note(Note.D, BeatFraction.Quarter), + note(Note.E, BeatFraction.Eighth), note(Note.F, BeatFraction.Eighth), + note(Note.E, BeatFraction.Quarter), note(Note.D, BeatFraction.Quarter), note(Note.C, BeatFraction.Quarter), + note(Note.D, BeatFraction.Quarter), note(Note.G3, BeatFraction.Quarter), note(Note.E, BeatFraction.Half), + note(Note.E, BeatFraction.Quarter), note(Note.F, BeatFraction.Quarter), note(Note.G, BeatFraction.Quarter), + note(Note.G, BeatFraction.Quarter), note(Note.F, BeatFraction.Quarter), note(Note.E, BeatFraction.Quarter), + note(Note.E, BeatFraction.Eighth), note(Note.F, BeatFraction.Eighth), + note(Note.C, BeatFraction.Quarter), note(Note.C, BeatFraction.Quarter), note(Note.D, BeatFraction.Quarter), + note(Note.E, BeatFraction.Quarter), note(Note.D, BeatFraction.Quarter + BeatFraction.Eighth), + note(Note.C, BeatFraction.Eighth), note(Note.C, BeatFraction.Half) +]; + +for (var t = 0; t < notes.length; t++) { + music.playTone(notes[t][0], notes[t][1]); + basic.showString(getNoteName(notes[t][0])); + music.rest(whole - notes[t][1]); +} \ No newline at end of file diff --git a/libs/core/music.cpp b/libs/core/music.cpp new file mode 100644 index 00000000..544c4089 --- /dev/null +++ b/libs/core/music.cpp @@ -0,0 +1,28 @@ +#include "ksbit.h" + +namespace music { + /** + * Plays a tone through ``speaker`` for the given duration. + * @param frequency pitch of the tone to play in Hertz (Hz) + * @param ms tone duration in milliseconds (ms) + */ + //% help=music/play-tone weight=90 + //% blockId=device_play_note block="play|tone %note=device_note|for %duration=device_beat" icon="\uf025" blockGap=8 + //% parts="speaker" + void playTone(int freqency, int ms) { + uBit.soundmotor.Sound_On(freqency); + if(ms > 0) uBit.sleep(ms); + uBit.soundmotor.Sound_Off(); + } + + /** + * Plays a tone through ``speaker``. + * @param frequency pitch of the tone to play in Hertz (Hz) + */ + //% help=music/ring-tone weight=80 + //% blockId=device_ring block="ring tone (Hz)|%note=device_note" icon="\uf025" blockGap=8 + //% parts="speaker" + void ringTone(int frequency) { + playTone(frequency, 0); + } +} diff --git a/libs/core/music.ts b/libs/core/music.ts index 1c6aaada..3d26116b 100644 --- a/libs/core/music.ts +++ b/libs/core/music.ts @@ -129,30 +129,30 @@ enum BeatFraction { namespace music { let beatsPerMinute: number = 120; - /** - * Plays a tone through pin ``P0`` for the given duration. - * @param frequency pitch of the tone to play in Hertz (Hz) - * @param ms tone duration in milliseconds (ms) - */ - //% help=music/play-tone weight=90 - //% blockId=device_play_note block="play|tone %note=device_note|for %duration=device_beat" icon="\uf025" blockGap=8 - //% parts="speaker" - export function playTone(frequency: number, ms: number): void { - pins.analogSetPitchPin(AnalogPin.P0); - pins.analogPitch(frequency, ms); - } - - /** - * Plays a tone through pin ``P0``. - * @param frequency pitch of the tone to play in Hertz (Hz) - */ - //% help=music/ring-tone weight=80 - //% blockId=device_ring block="ring tone (Hz)|%note=device_note" icon="\uf025" blockGap=8 - //% parts="speaker" - export function ringTone(frequency: number): void { - pins.analogSetPitchPin(AnalogPin.P0); - pins.analogPitch(frequency, 0); - } + // /** + // * Plays a tone through pin ``P0`` for the given duration. + // * @param frequency pitch of the tone to play in Hertz (Hz) + // * @param ms tone duration in milliseconds (ms) + // */ + // //% help=music/play-tone weight=90 + // //% blockId=device_play_note block="play|tone %note=device_note|for %duration=device_beat" icon="\uf025" blockGap=8 + // //% parts="speaker" + // export function playTone(frequency: number, ms: number): void { + // pins.analogSetPitchPin(AnalogPin.P0); + // pins.analogPitch(frequency, ms); + // } + // + // /** + // * Plays a tone through pin ``P0``. + // * @param frequency pitch of the tone to play in Hertz (Hz) + // */ + // //% help=music/ring-tone weight=80 + // //% blockId=device_ring block="ring tone (Hz)|%note=device_note" icon="\uf025" blockGap=8 + // //% parts="speaker" + // export function ringTone(frequency: number): void { + // pins.analogSetPitchPin(AnalogPin.P0); + // pins.analogPitch(frequency, 0); + // } /** * Rests (plays nothing) for a specified time through pin ``P0``. diff --git a/libs/core/pxt.json b/libs/core/pxt.json index 7f123c70..2e25ea6a 100644 --- a/libs/core/pxt.json +++ b/libs/core/pxt.json @@ -23,6 +23,7 @@ "led.cpp", "led.ts", "motors.cpp", + "music.cpp", "music.ts", "pins.cpp", "pins.ts", diff --git a/libs/core/shims.d.ts b/libs/core/shims.d.ts index eac6dda2..affa3b2d 100644 --- a/libs/core/shims.d.ts +++ b/libs/core/shims.d.ts @@ -524,6 +524,27 @@ declare namespace motors { //% parts=dcmotor shim=motors::motorOn function motorOn(power: number): void; } +declare namespace music { + + /** + * Plays a tone through ``speaker`` for the given duration. + * @param frequency pitch of the tone to play in Hertz (Hz) + * @param ms tone duration in milliseconds (ms) + */ + //% help=music/play-tone weight=90 + //% blockId=device_play_note block="play|tone %note=device_note|for %duration=device_beat" icon="\uf025" blockGap=8 + //% parts="speaker" shim=music::playTone + function playTone(freqency: number, ms: number): void; + + /** + * Plays a tone through ``speaker``. + * @param frequency pitch of the tone to play in Hertz (Hz) + */ + //% help=music/ring-tone weight=80 + //% blockId=device_ring block="ring tone (Hz)|%note=device_note" icon="\uf025" blockGap=8 + //% parts="speaker" shim=music::ringTone + function ringTone(frequency: number): void; +} declare namespace pins { /**