From c7dcf8bb8e3bbb9a9f8b2b0d4fd676e46c009140 Mon Sep 17 00:00:00 2001 From: Raimon Zamora Date: Mon, 9 Jan 2023 17:02:04 +0100 Subject: [PATCH] =?UTF-8?q?-=20Implementada=20polifonia=20en=20Chirping=20?= =?UTF-8?q?-=20Arreglats=20un=20mont=C3=B3=20de=20warnings=20per=20lo=20mo?= =?UTF-8?q?n?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- chirp.cpp | 166 +++++++++++++++++++++++++++----------------------- chirp.h | 4 +- data/game.lua | 3 +- gif.c | 60 +----------------- jfile.cpp | 2 +- lua.cpp | 4 +- mini.cpp | 15 ++--- mini.h | 2 +- 8 files changed, 106 insertions(+), 150 deletions(-) diff --git a/chirp.cpp b/chirp.cpp index 830b961..f76f75c 100644 --- a/chirp.cpp +++ b/chirp.cpp @@ -13,8 +13,8 @@ #define MAX_CHANNELS 5 -typedef void (*waveform_t)(const uint16_t, const uint32_t, uint8_t*); -waveform_t waveforms[1]; +typedef void (*waveform_t)(const uint16_t, const uint32_t, uint8_t*, const uint8_t); +static waveform_t waveforms[6]; 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 }; @@ -32,56 +32,59 @@ struct channel_t { uint32_t tempo = 44100; uint8_t waveform = 0; Uint8* play_pos; - Uint32 play_len; + int32_t play_len; Uint8 play_buffer[132300]; }; -channel_t channels[MAX_CHANNELS]; - -static float default_length = 0.25f; -static uint8_t volume = 32; -static uint8_t octave = 4; -static uint32_t tempo = 44100; -static uint8_t waveform = 0; -static char* song_ptr = NULL; -static char* song = NULL; - -static Uint8* play_pos; -static Uint32 play_len; -static Uint8 play_buffer[132300]; +static channel_t channels[MAX_CHANNELS]; +void chirp_stop_channel(const int c) { + if (channels[c].song != NULL) { + free(channels[c].song); + channels[c].song=NULL; + } + for (int i=0;i 0 ) { - while( play_len == 0 ) { - play_len = interpret_next_token(play_buffer); - 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; - } - const int actual_len = ( len > play_len ? play_len : len ); - //SDL_memset( stream, 0, actual_len ); - //SDL_MixAudio( stream, audio_pos, actual_len, SDL_MIX_MAXVOLUME ); - SDL_memcpy(stream, play_pos, actual_len); + for (int i=0;i 0 ) { + while( channels[i].play_len == 0 ) { + channels[i].play_len = interpret_next_token(i); - stream += actual_len; - play_pos += actual_len; - play_len -= actual_len; - len -= actual_len; - } - } else { - SDL_memset(stream, 0, len); + if (channels[i].play_len == -1) { + chirp_stop_channel(i); + break; + } + channels[i].play_pos = channels[i].play_buffer; + } + if (channels[i].play_len == -1) break; + + const int actual_len = ( l_len > channels[i].play_len ? channels[i].play_len : l_len ); + for (int j=0;j> 1) ? volume : -volume ); +void square_waveform(const uint16_t period, const uint32_t length, uint8_t *buffer, const uint8_t volume) { + for( uint32_t 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 saw_waveform(const uint16_t period, const uint32_t length, uint8_t *buffer, const uint8_t volume) { + for( uint32_t 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++ ) { +void triangle_waveform(const uint16_t period, const uint32_t length, uint8_t *buffer, const uint8_t volume) { + for( uint32_t i = 0; i < length; i++ ) { uint16_t pos = i % period; uint16_t half_period = period >> 1; if (pos < half_period) { @@ -91,14 +94,14 @@ void triangle_waveform(const uint16_t period, const uint32_t length, uint8_t *bu } } } -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 pulse12_waveform(const uint16_t period, const uint32_t length, uint8_t *buffer, const uint8_t volume) { + for( uint32_t 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 pulse25_waveform(const uint16_t period, const uint32_t length, uint8_t *buffer, const uint8_t volume) { + for( uint32_t 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 pulse75_waveform(const uint16_t period, const uint32_t length, uint8_t *buffer, const uint8_t volume) { + for( uint32_t i = 0; i < length; i++ ) buffer[i] = ( (i % period) < uint16_t(period-(period >> 2)) ? volume : -volume ); } void chirp_init() { @@ -111,35 +114,42 @@ void chirp_init() { waveforms[3] = &pulse12_waveform; waveforms[4] = &pulse25_waveform; waveforms[5] = &pulse75_waveform; + for (uint8_t i=0;i= 48 && param <= 57 ) { param -= 48; ++*token; } else { param = -1; } - return interpret_note( buffer, note, param ); + return interpret_note( c, note, param ); case 'r': param = *++*token; if( param >= 48 && param <= 57 ) { param -= 48; ++*token; } else { param = -1; } - return interpret_note( buffer, SILENCE, param ); + return interpret_note( c, SILENCE, param ); case 'n': param = *++*token; if( param >= 48 && param <= 57 ) { param -= 48; ++*token; } else { param = -1; } - return interpret_note( buffer, NOISE, param ); + return interpret_note( c, NOISE, param ); case 'o': param = *++*token; - if( param >= 48 && param <= 57 ) { octave = (param - 48) % 8; ++*token; } + if( param >= 48 && param <= 57 ) { channels[c].octave = (param - 48) % 8; ++*token; } return 0; case '>': - octave = (octave+1) % 8; ++*token; + channels[c].octave = (channels[c].octave+1) % 8; ++*token; return 0; case '<': - octave = (octave-1) % 8; ++*token; + channels[c].octave = (channels[c].octave-1) % 8; ++*token; return 0; case 'l': param = *++*token; - if( param >= 48 && param <= 57 ) { default_length = ((float)lengths[param - 48])/10000.0f; ++*token; } + if( param >= 48 && param <= 57 ) { channels[c].length = ((float)lengths[param - 48])/10000.0f; ++*token; } return 0; case 'v': param = *++*token; - if( param >= 48 && param <= 57 ) { volume = (param - 48) << 3; ++*token; } + if( param >= 48 && param <= 57 ) { channels[c].volume = (param - 48) << 3; ++*token; } return 0; case 't': param = *++*token; - if( param >= 48 && param <= 57 ) { tempo = tempos[param - 48] * 10; ++*token; } + if( param >= 48 && param <= 57 ) { channels[c].tempo = tempos[param - 48] * 10; ++*token; } return 0; case 'w': param = *++*token; - if( param >= 48 && param <= 57 ) { waveform = param - 48; ++*token; } + if( param >= 48 && param <= 57 ) { channels[c].waveform = param - 48; ++*token; } return 0; case '=': - song_ptr = song; + channels[c].song_ptr = channels[c].song; return 0; case '\0': return -1; @@ -201,9 +211,11 @@ int32_t interpret_next_token(uint8_t* buffer) { } void chirp_stop() { - if (song != NULL) { - free(song); - song=NULL; + for (int i=0;i> 4) + 1; + //color_resolution_bits = ((screen_descriptor.fields & 0x70) >> 4) + 1; global_color_table = (uint32_t *)calloc(1, 1024); if (screen_descriptor.fields & 0x80) { diff --git a/jfile.cpp b/jfile.cpp index 2cb7211..94f5df1 100644 --- a/jfile.cpp +++ b/jfile.cpp @@ -86,7 +86,7 @@ FILE *file_getfilepointer(const char *resourcename, int& filesize, const bool bi if (file_source==SOURCE_FILE) { bool found = false; - int count = 0; + uint32_t count = 0; while( !found && count < data_file->header.num_files ) { found = ( strcmp( resourcename, data_file->index.file_info[count].name ) == 0 ); if( !found ) count++; diff --git a/lua.cpp b/lua.cpp index e76d72b..75c7b63 100644 --- a/lua.cpp +++ b/lua.cpp @@ -651,9 +651,7 @@ extern "C" { static int cpp_playchirp(lua_State *L) { const char* str = luaL_checkstring(L, 1); - bool value = false; - if (lua_gettop(L) > 1) value = lua_toboolean(L, 2); - playchirp(str, value); + playchirp(str); return 0; } diff --git a/mini.cpp b/mini.cpp index cac48f9..cbc537d 100644 --- a/mini.cpp +++ b/mini.cpp @@ -186,8 +186,8 @@ int main(int argc,char*argv[]){ // initfont() int bi = 0; for (int ci=0; ci<96; ci+=2) { - font[ci] = base64font[bi]-48+((base64font[bi+1]-48)<<6)+((base64font[bi+2]-48&7)<<12); - font[ci+1] = ((base64font[bi+2]-48)>>3)+((base64font[bi+3]-48)<<3)+((base64font[bi+4]-48)<<9); + font[ci] = base64font[bi] - 48 + ( ( base64font[bi+1] - 48 ) << 6) + ( ( ( base64font[bi+2] - 48 ) & 7 ) << 12 ); + font[ci+1] = ( ( base64font[bi+2] - 48 ) >> 3 ) + ( ( base64font[bi+3] - 48 ) << 3 ) + ( ( base64font[bi+4] - 48 ) << 9 ); bi += 5; } @@ -255,7 +255,7 @@ int main(int argc,char*argv[]){ key_just_pressed = 0; } SDL_LockTexture(mini_bak, NULL, (void**)&pixels, &pitch); - for (int i=0;isize;++i) pixels[i] = palette[screen_surface->p[i]]; + for (uint32_t i=0;isize;++i) pixels[i] = palette[screen_surface->p[i]]; SDL_UnlockTexture(mini_bak); SDL_RenderCopy(mini_ren, mini_bak, NULL, NULL); SDL_RenderPresent(mini_ren); @@ -512,7 +512,7 @@ const char t195_symbol[]={1,2,0,0,0,0,0,3,1,2,0,0,1,2,0,0,0,4,1,2,0,0,0,0,0,1,2, void print(const char *str, int x, int y) { int pos=0; - for (int i=0; i < SDL_strlen(str); ++i) { + for (size_t i=0; i < SDL_strlen(str); ++i) { if ((unsigned char)str[i]==194) { i++; if ((unsigned char)str[i]==161) print_char_hv('!', x+pos*4, y); @@ -678,7 +678,8 @@ void spr(uint8_t n, int x, int y, float w, float h, bool flip_x, bool flip_y) { } void sspr(int sx, int sy, int sw, int sh, int dx, int dy, int dw, int dh, bool flip_x, bool flip_y) { - if (dw==0)dw=sw; if (dh==0)dh=sh; + if (dw==0) dw=sw; + if (dh==0) dh=sh; float sdx = float(sw)/float(dw); float sdy = float(sh)/float(dh); float ssx = sx; float ssy = sy; @@ -986,8 +987,8 @@ bool freadb() { return strcmp(fstr, "true")==0?true:false; } -void playchirp(const char *song, const bool lock) { - chirp_play(song, lock); +void playchirp(const char *song) { + chirp_play(song); } void stopchirp() { diff --git a/mini.h b/mini.h index 94e7179..04e60f0 100644 --- a/mini.h +++ b/mini.h @@ -257,7 +257,7 @@ const char *freads(); const char *freadw(); bool freadb(); -void playchirp(const char *song, const bool lock); +void playchirp(const char *song); void stopchirp(); void exit();