pxt-common-packages 10.3.1 → 10.3.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (34) hide show
  1. package/libs/azureiot/built/debug/binary.js +461 -461
  2. package/libs/color/built/debug/binary.js +8 -8
  3. package/libs/color-sensor/built/debug/binary.js +8 -8
  4. package/libs/controller/built/debug/binary.js +6969 -6969
  5. package/libs/controller---none/built/debug/binary.js +6949 -6949
  6. package/libs/datalogger/built/debug/binary.js +63 -63
  7. package/libs/edge-connector/built/debug/binary.js +9 -9
  8. package/libs/esp32/built/debug/binary.js +462 -462
  9. package/libs/game/assetTemplates.ts +10 -0
  10. package/libs/game/built/debug/binary.js +6888 -6888
  11. package/libs/lcd/built/debug/binary.js +8 -8
  12. package/libs/light-spectrum-sensor/built/debug/binary.js +8 -8
  13. package/libs/lora/built/debug/binary.js +8 -8
  14. package/libs/matrix-keypad/built/debug/binary.js +8 -8
  15. package/libs/mixer/instrument.ts +776 -0
  16. package/libs/mixer/melody.h +1 -1
  17. package/libs/mixer/melody.ts +29 -1
  18. package/libs/mixer/pxt.json +2 -0
  19. package/libs/mixer/sequencer.ts +88 -0
  20. package/libs/mqtt/built/debug/binary.js +176 -176
  21. package/libs/net/built/debug/binary.js +176 -176
  22. package/libs/net-game/built/debug/binary.js +8477 -8477
  23. package/libs/palette/built/debug/binary.js +6887 -6887
  24. package/libs/pixel/built/debug/binary.js +8 -8
  25. package/libs/power/built/debug/binary.js +8 -8
  26. package/libs/proximity/built/debug/binary.js +8 -8
  27. package/libs/radio/built/debug/binary.js +8 -8
  28. package/libs/radio-broadcast/built/debug/binary.js +8 -8
  29. package/libs/rotary-encoder/built/debug/binary.js +8 -8
  30. package/libs/screen/built/debug/binary.js +50 -50
  31. package/libs/servo/built/debug/binary.js +8 -8
  32. package/libs/sprite-scaling/built/debug/binary.js +6887 -6887
  33. package/libs/storyboard/built/debug/binary.js +6887 -6887
  34. package/package.json +1 -1
@@ -31,7 +31,7 @@ struct SoundInstruction {
31
31
 
32
32
  namespace music {
33
33
 
34
- #define MAX_SOUNDS 5
34
+ #define MAX_SOUNDS 8
35
35
 
36
36
  STATIC_ASSERT((1 << (16 - OUTPUT_BITS)) > MAX_SOUNDS);
37
37
 
@@ -313,7 +313,7 @@ namespace music {
313
313
  }
314
314
  }
315
315
 
316
- function addNote(sndInstr: Buffer, sndInstrPtr: number, ms: number, beg: number, end: number, soundWave: number, hz: number, volume: number, endHz: number) {
316
+ export function addNote(sndInstr: Buffer, sndInstrPtr: number, ms: number, beg: number, end: number, soundWave: number, hz: number, volume: number, endHz: number) {
317
317
  if (ms > 0) {
318
318
  sndInstr.setNumber(NumberFormat.UInt8LE, sndInstrPtr, soundWave)
319
319
  sndInstr.setNumber(NumberFormat.UInt8LE, sndInstrPtr + 1, 0)
@@ -583,6 +583,34 @@ namespace music {
583
583
  }
584
584
  }
585
585
 
586
+ let currentSequencer: sequencer.Sequencer;
587
+
588
+ //% shim=TD_ID
589
+ //% blockId=music_song_field_editor
590
+ //% block="$song"
591
+ //% song.fieldEditor=musiceditor
592
+ export function _songFieldEditor(song: Buffer) {
593
+ return song;
594
+ }
595
+
596
+ //% blockId=music_start_song
597
+ //% block="start song $song looping $loop"
598
+ //% song.shadow=music_song_field_editor
599
+ export function startSong(song: Buffer, loop: boolean) {
600
+ if (currentSequencer) currentSequencer.stop();
601
+
602
+ currentSequencer = new sequencer.Sequencer(new sequencer.Song(song));
603
+ currentSequencer.start(loop);
604
+ }
605
+
606
+ export function playInstructions(when: number, instructions: Buffer) {
607
+ queuePlayInstructions(when, instructions);
608
+ }
609
+
610
+ export function lookupFrequency(note: number) {
611
+ return freqs.getNumber(NumberFormat.UInt16LE, note * 2) || 0
612
+ }
613
+
586
614
  //% fixedInstance whenUsed block="ba ding"
587
615
  export const baDing = new Melody('b5:1 e6:3')
588
616
 
@@ -14,6 +14,8 @@
14
14
  "targetoverrides.ts",
15
15
  "music.ts",
16
16
  "soundEffect.ts",
17
+ "instrument.ts",
18
+ "sequencer.ts",
17
19
  "pxtparts.json",
18
20
  "headphone.svg"
19
21
  ],
@@ -0,0 +1,88 @@
1
+ namespace music.sequencer {
2
+ export class Sequencer {
3
+ currentTick: number;
4
+ isPlaying: boolean;
5
+ isLooping: boolean;
6
+ isRunning: boolean;
7
+
8
+ constructor(public song: Song) {
9
+ this.currentTick = 0;
10
+ this.isPlaying = false;
11
+ this.isLooping = false;
12
+ }
13
+
14
+ start(loop: boolean) {
15
+ this.currentTick = 0;
16
+ this.isLooping = loop;
17
+ this.isPlaying = true;
18
+
19
+ if (this.isRunning) return;
20
+ this.isRunning = true;
21
+
22
+ control.runInParallel(() => {
23
+ while (this.isPlaying) {
24
+ this.scheduleCurrentTick();
25
+
26
+ this.currentTick ++;
27
+
28
+ if (this.currentTick >= this.song.beatsPerMeasure * this.song.measures * this.song.ticksPerBeat) {
29
+ if (this.isLooping) this.currentTick = 0;
30
+ else this.isPlaying = false;
31
+ }
32
+
33
+ pause(this.tickToMs(1))
34
+ }
35
+ this.isRunning = false;
36
+ })
37
+ }
38
+
39
+ stop() {
40
+ this.isPlaying = false;
41
+ }
42
+
43
+ tickToMs(ticks: number) {
44
+ return ((60000 / this.song.beatsPerMinute) / this.song.ticksPerBeat) * ticks;
45
+ }
46
+
47
+ protected scheduleCurrentTick() {
48
+ for (const track of this.song.tracks) {
49
+ if (track.currentNoteEvent.startTick === this.currentTick) {
50
+ if (track.isMelodicTrack) {
51
+ this.scheduleMelodicTrack(track as MelodicTrack);
52
+ }
53
+ else {
54
+ this.scheduleDrumTrack(track as DrumTrack);
55
+ }
56
+
57
+ track.advanceNoteEvent();
58
+ }
59
+ }
60
+ }
61
+
62
+ protected scheduleMelodicTrack(track: MelodicTrack) {
63
+ for (let i = 0; i < track.currentNoteEvent.polyphony; i++) {
64
+ playInstructions(
65
+ 0,
66
+ renderInstrument(
67
+ track.instrument,
68
+ lookupFrequency(track.currentNoteEvent.getNote(i)),
69
+ this.tickToMs(track.currentNoteEvent.endTick - track.currentNoteEvent.startTick),
70
+ music.volume()
71
+ )
72
+ );
73
+ }
74
+ }
75
+
76
+ protected scheduleDrumTrack(track: DrumTrack) {
77
+ for (let i = 0; i < track.currentNoteEvent.polyphony; i++) {
78
+ playInstructions(
79
+ 0,
80
+ renderDrumInstrument(
81
+ track.drums[track.currentNoteEvent.getNote(i)],
82
+ music.volume()
83
+ )
84
+ );
85
+ }
86
+ }
87
+ }
88
+ }