From db36920c416340000687c1f229c2e4f4075a919e Mon Sep 17 00:00:00 2001 From: nesbox Date: Sun, 16 Sep 2018 23:48:03 +0300 Subject: [PATCH] #732 added stereo channels support --- 3rd-party | 2 +- src/console.c | 2 +- src/machine.h | 8 -------- src/music.c | 32 ++++++++++++++--------------- src/system.c | 4 ++-- src/tic.c | 57 ++++++++++++++++++++++++++------------------------- src/tic.h | 27 ++++++++++++++++++------ 7 files changed, 70 insertions(+), 62 deletions(-) diff --git a/3rd-party b/3rd-party index ad41cab..e2d0f1a 160000 --- a/3rd-party +++ b/3rd-party @@ -1 +1 @@ -Subproject commit ad41cab4620ac249ea8414ebd83b6e636cc024a5 +Subproject commit e2d0f1a9760d75f241e70628f713777fef626ceb diff --git a/src/console.c b/src/console.c index adaaf1b..f79548b 100644 --- a/src/console.c +++ b/src/console.c @@ -2278,7 +2278,7 @@ static void onConsoleRamCommand(Console* console, const char* param) {offsetof(tic_ram, sfx.samples), "SFX"}, {offsetof(tic_ram, music.patterns.data), "MUSIC PATTERNS"}, {offsetof(tic_ram, music.tracks.data), "MUSIC TRACKS"}, - {offsetof(tic_ram, music_pos), "MUSIC POS"}, + {offsetof(tic_ram, sound_state), "SOUND STATE"}, {TIC_RAM_SIZE, "..."}, }; diff --git a/src/machine.h b/src/machine.h index 7da7121..164ed38 100644 --- a/src/machine.h +++ b/src/machine.h @@ -86,15 +86,7 @@ typedef struct Channel channels[TIC_SOUND_CHANNELS]; struct { - enum - { - MusicStop = 0, - MusicPlayFrame, - MusicPlay, - } play; - s32 ticks; - Channel channels[TIC_SOUND_CHANNELS]; } music; diff --git a/src/music.c b/src/music.c index f254c75..18c6eca 100644 --- a/src/music.c +++ b/src/music.c @@ -49,9 +49,9 @@ static void redo(Music* music) history_redo(music->history); } -const tic_music_pos* getMusicPos(Music* music) +static const tic_sound_state* getMusicPos(Music* music) { - return &music->tic->ram.music_pos; + return &music->tic->ram.sound_state; } static void drawDownBorder(Music* music, s32 x, s32 y, s32 w, s32 h) @@ -298,8 +298,8 @@ static void upRow(Music* music) static void downRow(Music* music) { - const tic_music_pos* pos = getMusicPos(music); - if(pos->track == music->track && music->tracker.follow) return; + const tic_sound_state* pos = getMusicPos(music); + if(pos->music.track == music->track && music->tracker.follow) return; if (music->tracker.row < getRows(music) - 1) { @@ -386,17 +386,17 @@ static void downFrame(Music* music) static bool checkPlayFrame(Music* music, s32 frame) { - const tic_music_pos* pos = getMusicPos(music); + const tic_sound_state* pos = getMusicPos(music); - return pos->track == music->track && - pos->frame == frame; + return pos->music.track == music->track && + pos->music.frame == frame; } static bool checkPlayRow(Music* music, s32 row) { - const tic_music_pos* pos = getMusicPos(music); + const tic_sound_state* pos = getMusicPos(music); - return checkPlayFrame(music, music->tracker.frame) && pos->row == row; + return checkPlayFrame(music, music->tracker.frame) && pos->music.row == row; } static tic_track_pattern* getPattern(Music* music, s32 channel) @@ -789,8 +789,8 @@ static void processTrackerKeyboard(Music* music) else if(keyWasPressed(tic_key_space)) playNote(music); else if(keyWasPressed(tic_key_return)) { - const tic_music_pos* pos = getMusicPos(music); - pos->track < 0 + const tic_sound_state* pos = getMusicPos(music); + pos->music.track < 0 ? (shift ? playFrameRow(music) : playFrame(music)) : stopTrack(music); } @@ -1589,14 +1589,14 @@ static void drawTrackerLayout(Music* music) if(music->tracker.follow) { - const tic_music_pos* pos = getMusicPos(music); + const tic_sound_state* pos = getMusicPos(music); - if(pos->track == music->track && + if(pos->music.track == music->track && music->tracker.row >= 0 && - pos->row >= 0) + pos->music.row >= 0) { - music->tracker.frame = pos->frame; - music->tracker.row = pos->row; + music->tracker.frame = pos->music.frame; + music->tracker.row = pos->music.row; updateTracker(music); } } diff --git a/src/system.c b/src/system.c index fb20722..7b5e096 100644 --- a/src/system.c +++ b/src/system.c @@ -105,7 +105,7 @@ static void initSound() { .freq = 44100, .format = AUDIO_S16, - .channels = 1, + .channels = TIC_STEREO_CHANNLES, .userdata = NULL, }; @@ -115,7 +115,7 @@ static void initSound() if(platform.audio.cvt.needed) { - platform.audio.cvt.len = platform.audio.spec.freq * sizeof(s16) / TIC_FRAMERATE; + platform.audio.cvt.len = platform.audio.spec.freq * platform.audio.spec.channels * sizeof(s16) / TIC_FRAMERATE; platform.audio.cvt.buf = SDL_malloc(platform.audio.cvt.len * platform.audio.cvt.len_mult); } } diff --git a/src/tic.c b/src/tic.c index f99b5ed..d98a9a2 100644 --- a/src/tic.c +++ b/src/tic.c @@ -417,19 +417,19 @@ static void setMusic(tic_machine* machine, s32 index, s32 frame, s32 row, bool l { tic_mem* memory = (tic_mem*)machine; - memory->ram.music_pos.track = index; + memory->ram.sound_state.music.track = index; if(index < 0) { - machine->state.music.play = MusicStop; + memory->ram.sound_state.flag.music_state = tic_music_stop; resetMusic(memory); } else { - memory->ram.music_pos.row = row; - memory->ram.music_pos.frame = frame < 0 ? 0 : frame; - memory->ram.music_pos.flag.loop = loop; - machine->state.music.play = MusicPlay; + memory->ram.sound_state.music.row = row; + memory->ram.sound_state.music.frame = frame < 0 ? 0 : frame; + memory->ram.sound_state.flag.music_loop = loop; + memory->ram.sound_state.flag.music_state = tic_music_play; const tic_track* track = &machine->sound.music->tracks.data[index]; machine->state.music.ticks = row >= 0 ? row * (track->speed + DEFAULT_SPEED) * NOTES_PER_MUNUTE / (track->tempo + DEFAULT_TEMPO) / DEFAULT_SPEED : 0; @@ -443,7 +443,7 @@ static void api_music(tic_mem* memory, s32 index, s32 frame, s32 row, bool loop) setMusic(machine, index, frame, row, loop); if(index >= 0) - machine->state.music.play = MusicPlay; + memory->ram.sound_state.flag.music_state = tic_music_play; } static void soundClear(tic_mem* memory) @@ -1214,9 +1214,9 @@ static void processMusic(tic_mem* memory) { tic_machine* machine = (tic_machine*)memory; - if(machine->state.music.play == MusicStop) return; + if(memory->ram.sound_state.flag.music_state == tic_music_stop) return; - const tic_track* track = &machine->sound.music->tracks.data[memory->ram.music_pos.track]; + const tic_track* track = &machine->sound.music->tracks.data[memory->ram.sound_state.music.track]; s32 row = machine->state.music.ticks * (track->tempo + DEFAULT_TEMPO) * DEFAULT_SPEED / (track->speed + DEFAULT_SPEED) / NOTES_PER_MUNUTE; s32 rows = MUSIC_PATTERN_ROWS - track->rows; @@ -1226,14 +1226,14 @@ static void processMusic(tic_mem* memory) machine->state.music.ticks = 0; resetMusic(memory); - if(machine->state.music.play == MusicPlay) + if(memory->ram.sound_state.flag.music_state == tic_music_play) { - memory->ram.music_pos.frame++; + memory->ram.sound_state.music.frame++; - if(memory->ram.music_pos.frame >= MUSIC_FRAMES) + if(memory->ram.sound_state.music.frame >= MUSIC_FRAMES) { - if(memory->ram.music_pos.flag.loop) - memory->ram.music_pos.frame = 0; + if(memory->ram.sound_state.flag.music_loop) + memory->ram.sound_state.music.frame = 0; else { api_music(memory, -1, 0, 0, false); @@ -1244,12 +1244,12 @@ static void processMusic(tic_mem* memory) { s32 val = 0; for (s32 c = 0; c < TIC_SOUND_CHANNELS; c++) - val += tic_tool_get_pattern_id(track, memory->ram.music_pos.frame, c); + val += tic_tool_get_pattern_id(track, memory->ram.sound_state.music.frame, c); if(!val) { - if(memory->ram.music_pos.flag.loop) - memory->ram.music_pos.frame = 0; + if(memory->ram.sound_state.flag.music_loop) + memory->ram.sound_state.music.frame = 0; else { api_music(memory, -1, 0, 0, false); @@ -1258,9 +1258,9 @@ static void processMusic(tic_mem* memory) } } } - else if(machine->state.music.play == MusicPlayFrame) + else if(memory->ram.sound_state.flag.music_state == tic_music_play_frame) { - if(!memory->ram.music_pos.flag.loop) + if(!memory->ram.sound_state.flag.music_loop) { api_music(memory, -1, 0, 0, false); return; @@ -1268,18 +1268,18 @@ static void processMusic(tic_mem* memory) } } - if (row != memory->ram.music_pos.row) + if (row != memory->ram.sound_state.music.row) { - memory->ram.music_pos.row = row; + memory->ram.sound_state.music.row = row; for (s32 channel = 0; channel < TIC_SOUND_CHANNELS; channel++) { - s32 patternId = tic_tool_get_pattern_id(track, memory->ram.music_pos.frame, channel); + s32 patternId = tic_tool_get_pattern_id(track, memory->ram.sound_state.music.frame, channel); if (!patternId) continue; const tic_track_pattern* pattern = &machine->sound.music->patterns.data[patternId - PATTERN_START]; - s32 note = pattern->rows[memory->ram.music_pos.row].note; + s32 note = pattern->rows[memory->ram.sound_state.music.row].note; if (note > NoteNone) { @@ -1287,9 +1287,9 @@ static void processMusic(tic_mem* memory) if (note >= NoteStart) { - s32 octave = pattern->rows[memory->ram.music_pos.row].octave; + s32 octave = pattern->rows[memory->ram.sound_state.music.row].octave; s32 sfx = (pattern->rows[row].sfxhi << MUSIC_SFXID_LOW_BITS) | pattern->rows[row].sfxlow; - s32 volume = pattern->rows[memory->ram.music_pos.row].volume; + s32 volume = pattern->rows[memory->ram.sound_state.music.row].volume; musicSfx(memory, sfx, note - NoteStart, octave, volume, channel); } } @@ -1394,7 +1394,8 @@ static void api_tick_end(tic_mem* memory) } blip_end_frame(machine->blip, EndTime); - blip_read_samples(machine->blip, machine->memory.samples.buffer, machine->samplerate / TIC_FRAMERATE, 0); + blip_read_samples(machine->blip, machine->memory.samples.buffer, machine->samplerate / TIC_FRAMERATE, + !memory->ram.sound_state.flag.stereo_mute_left, !memory->ram.sound_state.flag.stereo_mute_right); machine->state.setpix = setPixelOvr; machine->state.getpix = getPixelOvr; @@ -1434,7 +1435,7 @@ static void api_music_frame(tic_mem* memory, s32 index, s32 frame, s32 row, bool setMusic(machine, index, frame, row, loop); if(index >= 0) - machine->state.music.play = MusicPlayFrame; + memory->ram.sound_state.flag.music_state = tic_music_play_frame; } static void initCover(tic_mem* tic) @@ -2030,7 +2031,7 @@ tic_mem* tic_create(s32 samplerate) initApi(&machine->memory.api); machine->samplerate = samplerate; - machine->memory.samples.size = samplerate / TIC_FRAMERATE * sizeof(s16); + machine->memory.samples.size = samplerate * TIC_STEREO_CHANNLES / TIC_FRAMERATE * sizeof(s16); machine->memory.samples.buffer = malloc(machine->memory.samples.size); machine->blip = blip_new(samplerate / 10); diff --git a/src/tic.h b/src/tic.h index 741d9a4..c9c21be 100644 --- a/src/tic.h +++ b/src/tic.h @@ -79,6 +79,7 @@ #define TIC_SAVEID_SIZE 64 #define TIC_SOUND_CHANNELS 4 +#define TIC_STEREO_CHANNLES 2 #define SFX_TICKS 30 #define SFX_COUNT_BITS 6 #define SFX_COUNT (1 << SFX_COUNT_BITS) @@ -266,18 +267,32 @@ typedef struct tic_tracks tracks; }tic_music; +typedef enum +{ + tic_music_stop, + tic_music_play_frame, + tic_music_play, +} tic_music_state; + typedef struct { - s8 track; - s8 frame; - s8 row; + struct + { + s8 track; + s8 frame; + s8 row; + } music; struct { - bool loop:1; + u8 music_loop:1; + u8 music_state:2; // enum tic_music_state + u8 unknown:1; + u8 stereo_mute_left:1; + u8 stereo_mute_right:1; } flag; -} tic_music_pos; +} tic_sound_state; typedef struct { @@ -421,7 +436,7 @@ typedef union tic_sound_register registers[TIC_SOUND_CHANNELS]; tic_sfx sfx; tic_music music; - tic_music_pos music_pos; + tic_sound_state sound_state; }; u8 data[TIC_RAM_SIZE];