#include "play.h" #include #include 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 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 }; static float default_length = 0.25f; static uint8_t volume = 64; static uint8_t octave = 4; static uint32_t tempo = tempos[4]*10; static uint8_t wave_type = 0; static char* song_ptr = NULL; static char song[256]; uint8_t generate_square_wave(unsigned int i, uint16_t period, uint8_t volume) { return ( (i % period) < (period >> 1) ? volume : -volume ); } uint8_t generate_saw_wave(unsigned int i, uint16_t period, uint8_t volume) { uint16_t t = i % period; // Mapea t (0..period-1) a un rango lineal -volume..+volume int16_t v = -volume + (2 * volume * t) / period; return (int8_t)v; } uint8_t generate_triangle_wave(unsigned int i, uint16_t period, uint8_t volume) { uint16_t t = i % period; int16_t v = (t < period/2) ? (-volume + (2 * volume * t) / (period/2)) : ( volume - (2 * volume * (t - period/2)) / (period/2)); return (int8_t)v; } uint8_t generate_periodic_noise(unsigned int i, uint16_t period, uint8_t volume) { static int8_t current = 0; if (i % period == 0) { current = (rand() % (2 * volume + 1)) - volume; } return current; } uint8_t generate_wave(unsigned int i, uint16_t period, uint8_t volume) { switch (wave_type) { case 1: return generate_saw_wave(i, period, volume); case 2: return generate_periodic_noise(i, period, volume); default:return generate_square_wave(i, period, volume); } } uint32_t interpret_note(uint8_t* buffer, const char note, const char param ) { const uint32_t length = ( param == -1 ? default_length : ((float)lengths[uint8_t(param)])/10000.0f ) * tempo; if( note == 100 ) { memset( buffer, 0, length ); return length; } const uint16_t period = periods[note + octave*12]; for( unsigned int i = 0; i < length; i++ ) buffer[i] = generate_wave(i, period, volume); return length; } void play_init(const char* new_song) { default_length = 0.25f; volume=64; octave=4; tempo=tempos[4]*10; wave_type = 0; int len = strlen( new_song ) + 1; if (len > 256) len = 256; strncpy(song, new_song, len); song_ptr = song; } int32_t interpret_next_token(uint8_t* buffer) { char** token = &song_ptr; char note = 0; char param = -1; switch( **token ) { case 'b': note += 2; case 'a': note += 2; case 'g': note += 2; case 'f': note += 1; case 'e': note += 2; case 'd': note += 2; case 'c': param = *++*token; if( param == '#' || param == '+' ) { note++; param = *++*token; } else if( param == '-' ) { note--; param = *++*token; } if( param >= 48 && param <= 57 ) { param -= 48; ++*token; } else { param = -1; } return interpret_note( buffer, note, param ); case 'r': param = *++*token; if( param >= 48 && param <= 57 ) { param -= 48; ++*token; } else { param = -1; } return interpret_note( buffer, 100, param ); case 'o': param = *++*token; if( param >= 48 && param <= 57 ) { octave = (param - 48) % 8; ++*token; } return 0; case '>': octave = (octave+1) % 8; ++*token; return 0; case '<': octave = (octave-1) % 8; ++*token; return 0; case 'l': param = *++*token; if( param >= 48 && param <= 57 ) { default_length = ((float)lengths[param - 48])/10000.0f; ++*token; } return 0; case 'v': param = *++*token; if( param >= 48 && param <= 57 ) { volume = (param - 48) << 4; ++*token; } return 0; case 't': param = *++*token; if( param >= 48 && param <= 57 ) { tempo = tempos[param - 48] * 10; ++*token; } return 0; case 'w': param = *++*token; if( param >= 48 && param <= 50 ) { wave_type = param - 48; ++*token; } return 0; case '\0': return -1; default: ++*token; return 0; }; }