- Several waveforms to choose for chirps
This commit is contained in:
120
chirp.cpp
120
chirp.cpp
@@ -2,51 +2,61 @@
|
|||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <SDL2/SDL.h>
|
#include <SDL2/SDL.h>
|
||||||
|
//1 2 3 4 6 8 12 16 24 32
|
||||||
|
//f s c n b r
|
||||||
|
|
||||||
|
#define SILENCE 128
|
||||||
|
#define NOISE 129
|
||||||
|
|
||||||
|
#define AUDIO_NONE 0
|
||||||
|
#define AUDIO_PLAY 1
|
||||||
|
|
||||||
|
#define MAX_CHANNELS 5
|
||||||
|
|
||||||
|
typedef void (*waveform_t)(const uint16_t, const uint32_t, uint8_t*);
|
||||||
|
waveform_t waveforms[1];
|
||||||
|
|
||||||
const static uint16_t lengths[10] = { 313, 625, 938, 1250, 1875, 2500, 3750, 5000, 7500, 10000 };
|
const static uint16_t lengths[10] = { 313, 625, 938, 1250, 1875, 2500, 3750, 5000, 7500, 10000 };
|
||||||
const static uint16_t tempos[10] = { 13230, 8820, 6615, 5292, 4410, 3780, 3308, 2940, 2646, 2406 };
|
const static uint16_t tempos[10] = { 13230, 8820, 6615, 5292, 4410, 3780, 3308, 2940, 2646, 2406 };
|
||||||
const float periods[108] = { 1348.49207, 1272.80688, 1201.37, 1133.94214, 1070.29871, 1010.22772, 953.527893, 900.010376, 849.496887, 801.818176, 756.815613, 714.338745, 674.246033, 636.403564, 600.684875, 566.971069, 535.149475, 505.11377, 476.763947, 450.005249, 424.748352, 400.909088, 378.407806, 357.169373, 337.123016, 318.201782, 300.342438, 283.485535, 267.574738, 252.556885, 238.381973, 225.002625, 212.374176, 200.454544, 189.203888, 178.584702, 168.561508, 159.100876, 150.171234, 141.742767, 133.787354, 126.278458, 119.190987, 112.501305, 106.187096, 100.227272, 94.6019516, 89.2923508, 84.2807541, 79.5504379, 75.0856171, 70.8713837, 66.8936768, 63.139225, 59.5954933, 56.2506561, 53.0935478, 50.113636, 47.3009758, 44.6461754, 42.140377, 39.775219, 37.5428085, 35.4356918, 33.4468384, 31.5696125, 29.7977467, 28.1253281, 26.5467739, 25.056818, 23.650486, 22.3230877, 21.0701885, 19.8876095, 18.7714043, 17.7178459, 16.7234192, 15.7848072, 14.8988733, 14.0626631, 13.273387, 12.528409, 11.8252439, 11.1615429, 10.5350943, 9.94380569, 9.38570118, 8.85892296, 8.36171055, 7.89240265, 7.44943666, 7.03133202, 6.636693, 6.2642045, 5.91262197, 5.58077145, 5.26754713, 4.97190285, 4.69285059, 4.42946148, 4.18085527, 3.94620132, 3.72471833, 3.51566601, 3.3183465, 3.13210225, 2.95631051, 2.7903862 };
|
const float periods[108] = { 1348.49207, 1272.80688, 1201.37, 1133.94214, 1070.29871, 1010.22772, 953.527893, 900.010376, 849.496887, 801.818176, 756.815613, 714.338745, 674.246033, 636.403564, 600.684875, 566.971069, 535.149475, 505.11377, 476.763947, 450.005249, 424.748352, 400.909088, 378.407806, 357.169373, 337.123016, 318.201782, 300.342438, 283.485535, 267.574738, 252.556885, 238.381973, 225.002625, 212.374176, 200.454544, 189.203888, 178.584702, 168.561508, 159.100876, 150.171234, 141.742767, 133.787354, 126.278458, 119.190987, 112.501305, 106.187096, 100.227272, 94.6019516, 89.2923508, 84.2807541, 79.5504379, 75.0856171, 70.8713837, 66.8936768, 63.139225, 59.5954933, 56.2506561, 53.0935478, 50.113636, 47.3009758, 44.6461754, 42.140377, 39.775219, 37.5428085, 35.4356918, 33.4468384, 31.5696125, 29.7977467, 28.1253281, 26.5467739, 25.056818, 23.650486, 22.3230877, 21.0701885, 19.8876095, 18.7714043, 17.7178459, 16.7234192, 15.7848072, 14.8988733, 14.0626631, 13.273387, 12.528409, 11.8252439, 11.1615429, 10.5350943, 9.94380569, 9.38570118, 8.85892296, 8.36171055, 7.89240265, 7.44943666, 7.03133202, 6.636693, 6.2642045, 5.91262197, 5.58077145, 5.26754713, 4.97190285, 4.69285059, 4.42946148, 4.18085527, 3.94620132, 3.72471833, 3.51566601, 3.3183465, 3.13210225, 2.95631051, 2.7903862 };
|
||||||
|
|
||||||
|
SDL_AudioDeviceID audio_device;
|
||||||
|
uint8_t audio_state = AUDIO_NONE;
|
||||||
|
|
||||||
|
struct channel_t {
|
||||||
|
char* song = NULL;
|
||||||
|
char* song_ptr = NULL;
|
||||||
|
float length = 0.25f;
|
||||||
|
uint8_t volume = 32;
|
||||||
|
uint8_t octave = 4;
|
||||||
|
uint32_t tempo = 44100;
|
||||||
|
uint8_t waveform = 0;
|
||||||
|
Uint8* play_pos;
|
||||||
|
Uint32 play_len;
|
||||||
|
Uint8 play_buffer[132300];
|
||||||
|
};
|
||||||
|
channel_t channels[MAX_CHANNELS];
|
||||||
|
|
||||||
static float default_length = 0.25f;
|
static float default_length = 0.25f;
|
||||||
static uint8_t volume = 64;
|
static uint8_t volume = 32;
|
||||||
static uint8_t octave = 4;
|
static uint8_t octave = 4;
|
||||||
static uint32_t tempo = 44100;
|
static uint32_t tempo = 44100;
|
||||||
|
static uint8_t waveform = 0;
|
||||||
static char* song_ptr = NULL;
|
static char* song_ptr = NULL;
|
||||||
static char* song = NULL;
|
static char* song = NULL;
|
||||||
static bool audio_lock = false;
|
|
||||||
|
|
||||||
|
|
||||||
#define AUDIO_NONE 0
|
|
||||||
#define AUDIO_SOUND 1
|
|
||||||
#define AUDIO_PLAY 2
|
|
||||||
|
|
||||||
uint8_t audio_state = AUDIO_NONE;
|
|
||||||
int audio_freq = 0;
|
|
||||||
uint32_t audio_len = 0;
|
|
||||||
|
|
||||||
SDL_AudioDeviceID audio_device;
|
|
||||||
|
|
||||||
static Uint8* play_pos;
|
static Uint8* play_pos;
|
||||||
static Uint32 play_len;
|
static Uint32 play_len;
|
||||||
static Uint8 play_buffer[132300];
|
static Uint8 play_buffer[132300];
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
void audioCallback(void * userdata, uint8_t * stream, int len) {
|
void audioCallback(void * userdata, uint8_t * stream, int len) {
|
||||||
if (audio_state == AUDIO_SOUND) {
|
if (audio_state == AUDIO_PLAY) {
|
||||||
if (audio_len <= 0) audio_state = AUDIO_NONE;
|
|
||||||
static int period=0;
|
|
||||||
static int v = 16;
|
|
||||||
SDL_memset(stream, 0, len);
|
|
||||||
const int flen = SDL_min(audio_len, len);
|
|
||||||
audio_len -= flen;
|
|
||||||
for (int i=0; i<flen; ++i) {
|
|
||||||
stream[i] = v;
|
|
||||||
period++; if (period>=audio_freq) {period = 0; v=-v;}
|
|
||||||
}
|
|
||||||
} else if (audio_state == AUDIO_PLAY) {
|
|
||||||
while( len > 0 ) {
|
while( len > 0 ) {
|
||||||
while( play_len == 0 ) {
|
while( play_len == 0 ) {
|
||||||
play_len = interpret_next_token(play_buffer);
|
play_len = interpret_next_token(play_buffer);
|
||||||
if (play_len == -1) { audio_state=AUDIO_NONE; audio_lock=false; SDL_memset( stream, 0, len ); return; }
|
if (play_len == -1) { audio_state=AUDIO_NONE; if (song != NULL) {free(song);song=NULL;}; SDL_memset( stream, 0, len ); return; }
|
||||||
play_pos = play_buffer;
|
play_pos = play_buffer;
|
||||||
}
|
}
|
||||||
const int actual_len = ( len > play_len ? play_len : len );
|
const int actual_len = ( len > play_len ? play_len : len );
|
||||||
@@ -64,25 +74,56 @@ void audioCallback(void * userdata, uint8_t * stream, int len) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void square_waveform(const uint16_t period, const uint32_t length, uint8_t *buffer) {
|
||||||
|
for( int i = 0; i < length; i++ ) buffer[i] = ( (i % period) < (period >> 1) ? volume : -volume );
|
||||||
|
}
|
||||||
|
void saw_waveform(const uint16_t period, const uint32_t length, uint8_t *buffer) {
|
||||||
|
for( int i = 0; i < length; i++ ) buffer[i] = -volume + uint16_t( float(i % period) / float(period) * volume*2 );
|
||||||
|
}
|
||||||
|
void triangle_waveform(const uint16_t period, const uint32_t length, uint8_t *buffer) {
|
||||||
|
for( int i = 0; i < length; i++ ) {
|
||||||
|
uint16_t pos = i % period;
|
||||||
|
uint16_t half_period = period >> 1;
|
||||||
|
if (pos < half_period) {
|
||||||
|
buffer[i] = -volume + uint16_t( (float(pos) / float(half_period)) * float(volume*2) );
|
||||||
|
} else {
|
||||||
|
buffer[i] = volume - uint16_t( (float(pos-half_period) / float(half_period)) * float(volume*2) );
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
void pulse12_waveform(const uint16_t period, const uint32_t length, uint8_t *buffer) {
|
||||||
|
for( int i = 0; i < length; i++ ) buffer[i] = ( (i % period) < (period >> 3) ? volume : -volume );
|
||||||
|
}
|
||||||
|
void pulse25_waveform(const uint16_t period, const uint32_t length, uint8_t *buffer) {
|
||||||
|
for( int i = 0; i < length; i++ ) buffer[i] = ( (i % period) < (period >> 2) ? volume : -volume );
|
||||||
|
}
|
||||||
|
void pulse75_waveform(const uint16_t period, const uint32_t length, uint8_t *buffer) {
|
||||||
|
for( int i = 0; i < length; i++ ) buffer[i] = ( (i % period) < (period-(period >> 2)) ? volume : -volume );
|
||||||
|
}
|
||||||
|
|
||||||
void chirp_init() {
|
void chirp_init() {
|
||||||
SDL_AudioSpec audioSpec = {22050, AUDIO_S8, 1, 0, 512, 0, 0, audioCallback, NULL};
|
SDL_AudioSpec audioSpec = {22050, AUDIO_S8, 1, 0, 512, 0, 0, audioCallback, NULL};
|
||||||
audio_device = SDL_OpenAudioDevice(NULL, 0, &audioSpec, NULL, 0);
|
audio_device = SDL_OpenAudioDevice(NULL, 0, &audioSpec, NULL, 0);
|
||||||
SDL_PauseAudioDevice(audio_device, 0);
|
SDL_PauseAudioDevice(audio_device, 0);
|
||||||
|
waveforms[0] = &square_waveform;
|
||||||
|
waveforms[1] = &saw_waveform;
|
||||||
|
waveforms[2] = &triangle_waveform;
|
||||||
|
waveforms[3] = &pulse12_waveform;
|
||||||
|
waveforms[4] = &pulse25_waveform;
|
||||||
|
waveforms[5] = &pulse75_waveform;
|
||||||
}
|
}
|
||||||
|
|
||||||
uint32_t interpret_note(uint8_t* buffer, const char note, const char param ) {
|
uint32_t interpret_note(uint8_t* buffer, const char note, const char param ) {
|
||||||
const uint32_t length = ( param == -1 ? default_length : ((float)lengths[param])/10000.0f ) * tempo;
|
const uint32_t length = ( param == -1 ? default_length : ((float)lengths[param])/10000.0f ) * tempo;
|
||||||
if( note == 100 ) { memset( buffer, 0, length ); return length; }
|
if( note == SILENCE ) { memset( buffer, 0, length ); return length; }
|
||||||
if( note == 101 ) { for( int i = 0; i < length; i++ ) buffer[i] = rand()%2==0 ? volume : -volume; return length; }
|
if( note == NOISE ) { for( int i = 0; i < length; i++ ) buffer[i] = rand()%2==0 ? volume : -volume; return length; }
|
||||||
const uint16_t period = periods[note + octave*12];
|
|
||||||
|
|
||||||
for( int i = 0; i < length; i++ ) buffer[i] = ( (i % period) < (period >> 1) ? volume : -volume );
|
const uint16_t period = periods[note + octave*12];
|
||||||
|
waveforms[waveform](period, length, buffer);
|
||||||
return length;
|
return length;
|
||||||
}
|
}
|
||||||
|
|
||||||
void chirp_play(const char* new_song, const bool lock) {
|
void chirp_play(const char* new_song) {
|
||||||
if (audio_lock and !lock) return;
|
|
||||||
audio_lock = lock;
|
|
||||||
play_pos = play_buffer;
|
play_pos = play_buffer;
|
||||||
play_len = 0;
|
play_len = 0;
|
||||||
|
|
||||||
@@ -117,11 +158,11 @@ int32_t interpret_next_token(uint8_t* buffer) {
|
|||||||
case 'r':
|
case 'r':
|
||||||
param = *++*token;
|
param = *++*token;
|
||||||
if( param >= 48 && param <= 57 ) { param -= 48; ++*token; } else { param = -1; }
|
if( param >= 48 && param <= 57 ) { param -= 48; ++*token; } else { param = -1; }
|
||||||
return interpret_note( buffer, 100, param );
|
return interpret_note( buffer, SILENCE, param );
|
||||||
case 'n':
|
case 'n':
|
||||||
param = *++*token;
|
param = *++*token;
|
||||||
if( param >= 48 && param <= 57 ) { param -= 48; ++*token; } else { param = -1; }
|
if( param >= 48 && param <= 57 ) { param -= 48; ++*token; } else { param = -1; }
|
||||||
return interpret_note( buffer, 101, param );
|
return interpret_note( buffer, NOISE, param );
|
||||||
case 'o':
|
case 'o':
|
||||||
param = *++*token;
|
param = *++*token;
|
||||||
if( param >= 48 && param <= 57 ) { octave = (param - 48) % 8; ++*token; }
|
if( param >= 48 && param <= 57 ) { octave = (param - 48) % 8; ++*token; }
|
||||||
@@ -144,6 +185,10 @@ int32_t interpret_next_token(uint8_t* buffer) {
|
|||||||
param = *++*token;
|
param = *++*token;
|
||||||
if( param >= 48 && param <= 57 ) { tempo = tempos[param - 48] * 10; ++*token; }
|
if( param >= 48 && param <= 57 ) { tempo = tempos[param - 48] * 10; ++*token; }
|
||||||
return 0;
|
return 0;
|
||||||
|
case 'w':
|
||||||
|
param = *++*token;
|
||||||
|
if( param >= 48 && param <= 57 ) { waveform = param - 48; ++*token; }
|
||||||
|
return 0;
|
||||||
case '=':
|
case '=':
|
||||||
song_ptr = song;
|
song_ptr = song;
|
||||||
return 0;
|
return 0;
|
||||||
@@ -156,6 +201,9 @@ int32_t interpret_next_token(uint8_t* buffer) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void chirp_stop() {
|
void chirp_stop() {
|
||||||
|
if (song != NULL) {
|
||||||
|
free(song);
|
||||||
|
song=NULL;
|
||||||
|
}
|
||||||
audio_state=AUDIO_NONE;
|
audio_state=AUDIO_NONE;
|
||||||
audio_lock=false;
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ x=0
|
|||||||
|
|
||||||
function _init()
|
function _init()
|
||||||
text="HOLA MINI"
|
text="HOLA MINI"
|
||||||
|
playchirp("o4w4cdw5cd")
|
||||||
end
|
end
|
||||||
|
|
||||||
function _update()
|
function _update()
|
||||||
|
|||||||
Reference in New Issue
Block a user