Own implementation of music
This commit is contained in:
parent
ad5d5daee5
commit
02e45762b4
@ -24,6 +24,6 @@ namespace pxt {
|
|||||||
|
|
||||||
void target_init() {
|
void target_init() {
|
||||||
OutputInit();
|
OutputInit();
|
||||||
SoundInit();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -1,11 +1,23 @@
|
|||||||
#include "pxt.h"
|
#include "pxt.h"
|
||||||
#include "ev3.h"
|
#include "ev3const.h"
|
||||||
|
|
||||||
|
#include <unistd.h>
|
||||||
|
#include <sys/types.h>
|
||||||
|
#include <sys/stat.h>
|
||||||
|
#include <fcntl.h>
|
||||||
|
|
||||||
|
|
||||||
#define NOTE_PAUSE 20
|
#define NOTE_PAUSE 20
|
||||||
|
|
||||||
namespace music {
|
namespace music {
|
||||||
|
|
||||||
byte currVolume = 100;
|
uint8_t currVolume = 2;
|
||||||
|
|
||||||
|
void writeDev(void *data, int size) {
|
||||||
|
int fd = open("/dev/lms_sound", O_WRONLY);
|
||||||
|
write(fd, data, size);
|
||||||
|
close(fd);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Set the output volume of the sound synthesizer.
|
* Set the output volume of the sound synthesizer.
|
||||||
@ -21,6 +33,35 @@ void setVolume(int volume) {
|
|||||||
currVolume = max(0, min(100, volume * 100 / 256));
|
currVolume = max(0, min(100, volume * 100 / 256));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#define SOUND_CMD_BREAK 0
|
||||||
|
#define SOUND_CMD_TONE 1
|
||||||
|
#define SOUND_CMD_PLAY 2
|
||||||
|
#define SOUND_CMD_REPEAT 3
|
||||||
|
#define SOUND_CMD_SERVICE 4
|
||||||
|
|
||||||
|
struct ToneCmd {
|
||||||
|
uint8_t cmd;
|
||||||
|
uint8_t vol;
|
||||||
|
uint16_t freq;
|
||||||
|
uint16_t duration;
|
||||||
|
};
|
||||||
|
|
||||||
|
static void _stopSound() {
|
||||||
|
uint8_t cmd = SOUND_CMD_BREAK;
|
||||||
|
writeDev(&cmd, sizeof(cmd));
|
||||||
|
}
|
||||||
|
|
||||||
|
static void _playTone(uint16_t frequency, uint16_t duration, uint8_t volume)
|
||||||
|
{
|
||||||
|
ToneCmd cmd;
|
||||||
|
cmd.cmd = SOUND_CMD_TONE;
|
||||||
|
cmd.vol = volume;
|
||||||
|
cmd.freq = frequency;
|
||||||
|
cmd.duration = duration;
|
||||||
|
// (*SoundInstance.pSound).Busy = TRUE;
|
||||||
|
writeDev(&cmd, sizeof(cmd));
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Play a tone through the speaker for some amount of time.
|
* Play a tone through the speaker for some amount of time.
|
||||||
* @param frequency pitch of the tone to play in Hertz (Hz)
|
* @param frequency pitch of the tone to play in Hertz (Hz)
|
||||||
@ -32,21 +73,20 @@ void setVolume(int volume) {
|
|||||||
//% blockNamespace=music
|
//% blockNamespace=music
|
||||||
void playTone(int frequency, int ms) {
|
void playTone(int frequency, int ms) {
|
||||||
if (frequency <= 0) {
|
if (frequency <= 0) {
|
||||||
StopSound();
|
_stopSound();
|
||||||
if (ms >= 0)
|
if (ms >= 0)
|
||||||
sleep_ms(ms);
|
sleep_ms(ms);
|
||||||
} else {
|
} else {
|
||||||
if (ms > 0) {
|
if (ms > 0) {
|
||||||
int d = max(1, ms - NOTE_PAUSE); // allow for short rest
|
int d = max(1, ms - NOTE_PAUSE); // allow for short rest
|
||||||
int r = max(1, ms - d);
|
int r = max(1, ms - d);
|
||||||
PlayToneEx(frequency, d, currVolume);
|
_playTone(frequency, d, currVolume);
|
||||||
sleep_ms(d + r);
|
sleep_ms(d + r);
|
||||||
} else {
|
} else {
|
||||||
// ring
|
// ring
|
||||||
PlayToneEx(frequency, 60000, currVolume);
|
_playTone(frequency, 0, currVolume);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
sleep_ms(1);
|
sleep_ms(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
7
libs/music/test.ts
Normal file
7
libs/music/test.ts
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
music.setVolume(3)
|
||||||
|
music.playTone(440, 500)
|
||||||
|
loops.pause(500)
|
||||||
|
music.playTone(1440, 500)
|
||||||
|
loops.pause(500)
|
||||||
|
music.playTone(2440, 500)
|
||||||
|
loops.pause(500)
|
9
libs/music/tsconfig.json
Normal file
9
libs/music/tsconfig.json
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
{
|
||||||
|
"compilerOptions": {
|
||||||
|
"target": "es5",
|
||||||
|
"noImplicitAny": true,
|
||||||
|
"outDir": "built",
|
||||||
|
"rootDir": "."
|
||||||
|
},
|
||||||
|
"exclude": ["pxt_modules/**/*test.ts"]
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user