#732 added stereo channels support

This commit is contained in:
nesbox 2018-09-16 23:48:03 +03:00
parent f2c68ca6a8
commit db36920c41
7 changed files with 70 additions and 62 deletions

@ -1 +1 @@
Subproject commit ad41cab4620ac249ea8414ebd83b6e636cc024a5 Subproject commit e2d0f1a9760d75f241e70628f713777fef626ceb

View File

@ -2278,7 +2278,7 @@ static void onConsoleRamCommand(Console* console, const char* param)
{offsetof(tic_ram, sfx.samples), "SFX"}, {offsetof(tic_ram, sfx.samples), "SFX"},
{offsetof(tic_ram, music.patterns.data), "MUSIC PATTERNS"}, {offsetof(tic_ram, music.patterns.data), "MUSIC PATTERNS"},
{offsetof(tic_ram, music.tracks.data), "MUSIC TRACKS"}, {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, "..."}, {TIC_RAM_SIZE, "..."},
}; };

View File

@ -86,15 +86,7 @@ typedef struct
Channel channels[TIC_SOUND_CHANNELS]; Channel channels[TIC_SOUND_CHANNELS];
struct struct
{ {
enum
{
MusicStop = 0,
MusicPlayFrame,
MusicPlay,
} play;
s32 ticks; s32 ticks;
Channel channels[TIC_SOUND_CHANNELS]; Channel channels[TIC_SOUND_CHANNELS];
} music; } music;

View File

@ -49,9 +49,9 @@ static void redo(Music* music)
history_redo(music->history); 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) 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) static void downRow(Music* music)
{ {
const tic_music_pos* pos = getMusicPos(music); const tic_sound_state* pos = getMusicPos(music);
if(pos->track == music->track && music->tracker.follow) return; if(pos->music.track == music->track && music->tracker.follow) return;
if (music->tracker.row < getRows(music) - 1) if (music->tracker.row < getRows(music) - 1)
{ {
@ -386,17 +386,17 @@ static void downFrame(Music* music)
static bool checkPlayFrame(Music* music, s32 frame) 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 && return pos->music.track == music->track &&
pos->frame == frame; pos->music.frame == frame;
} }
static bool checkPlayRow(Music* music, s32 row) 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) 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_space)) playNote(music);
else if(keyWasPressed(tic_key_return)) else if(keyWasPressed(tic_key_return))
{ {
const tic_music_pos* pos = getMusicPos(music); const tic_sound_state* pos = getMusicPos(music);
pos->track < 0 pos->music.track < 0
? (shift ? playFrameRow(music) : playFrame(music)) ? (shift ? playFrameRow(music) : playFrame(music))
: stopTrack(music); : stopTrack(music);
} }
@ -1589,14 +1589,14 @@ static void drawTrackerLayout(Music* music)
if(music->tracker.follow) 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 && music->tracker.row >= 0 &&
pos->row >= 0) pos->music.row >= 0)
{ {
music->tracker.frame = pos->frame; music->tracker.frame = pos->music.frame;
music->tracker.row = pos->row; music->tracker.row = pos->music.row;
updateTracker(music); updateTracker(music);
} }
} }

View File

@ -105,7 +105,7 @@ static void initSound()
{ {
.freq = 44100, .freq = 44100,
.format = AUDIO_S16, .format = AUDIO_S16,
.channels = 1, .channels = TIC_STEREO_CHANNLES,
.userdata = NULL, .userdata = NULL,
}; };
@ -115,7 +115,7 @@ static void initSound()
if(platform.audio.cvt.needed) 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); platform.audio.cvt.buf = SDL_malloc(platform.audio.cvt.len * platform.audio.cvt.len_mult);
} }
} }

View File

@ -417,19 +417,19 @@ static void setMusic(tic_machine* machine, s32 index, s32 frame, s32 row, bool l
{ {
tic_mem* memory = (tic_mem*)machine; tic_mem* memory = (tic_mem*)machine;
memory->ram.music_pos.track = index; memory->ram.sound_state.music.track = index;
if(index < 0) if(index < 0)
{ {
machine->state.music.play = MusicStop; memory->ram.sound_state.flag.music_state = tic_music_stop;
resetMusic(memory); resetMusic(memory);
} }
else else
{ {
memory->ram.music_pos.row = row; memory->ram.sound_state.music.row = row;
memory->ram.music_pos.frame = frame < 0 ? 0 : frame; memory->ram.sound_state.music.frame = frame < 0 ? 0 : frame;
memory->ram.music_pos.flag.loop = loop; memory->ram.sound_state.flag.music_loop = loop;
machine->state.music.play = MusicPlay; memory->ram.sound_state.flag.music_state = tic_music_play;
const tic_track* track = &machine->sound.music->tracks.data[index]; 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; 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); setMusic(machine, index, frame, row, loop);
if(index >= 0) if(index >= 0)
machine->state.music.play = MusicPlay; memory->ram.sound_state.flag.music_state = tic_music_play;
} }
static void soundClear(tic_mem* memory) static void soundClear(tic_mem* memory)
@ -1214,9 +1214,9 @@ static void processMusic(tic_mem* memory)
{ {
tic_machine* machine = (tic_machine*)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 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; s32 rows = MUSIC_PATTERN_ROWS - track->rows;
@ -1226,14 +1226,14 @@ static void processMusic(tic_mem* memory)
machine->state.music.ticks = 0; machine->state.music.ticks = 0;
resetMusic(memory); 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) if(memory->ram.sound_state.flag.music_loop)
memory->ram.music_pos.frame = 0; memory->ram.sound_state.music.frame = 0;
else else
{ {
api_music(memory, -1, 0, 0, false); api_music(memory, -1, 0, 0, false);
@ -1244,12 +1244,12 @@ static void processMusic(tic_mem* memory)
{ {
s32 val = 0; s32 val = 0;
for (s32 c = 0; c < TIC_SOUND_CHANNELS; c++) 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(!val)
{ {
if(memory->ram.music_pos.flag.loop) if(memory->ram.sound_state.flag.music_loop)
memory->ram.music_pos.frame = 0; memory->ram.sound_state.music.frame = 0;
else else
{ {
api_music(memory, -1, 0, 0, false); 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); api_music(memory, -1, 0, 0, false);
return; 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++) 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; if (!patternId) continue;
const tic_track_pattern* pattern = &machine->sound.music->patterns.data[patternId - PATTERN_START]; 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) if (note > NoteNone)
{ {
@ -1287,9 +1287,9 @@ static void processMusic(tic_mem* memory)
if (note >= NoteStart) 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 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); 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_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.setpix = setPixelOvr;
machine->state.getpix = getPixelOvr; 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); setMusic(machine, index, frame, row, loop);
if(index >= 0) 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) static void initCover(tic_mem* tic)
@ -2030,7 +2031,7 @@ tic_mem* tic_create(s32 samplerate)
initApi(&machine->memory.api); initApi(&machine->memory.api);
machine->samplerate = samplerate; 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->memory.samples.buffer = malloc(machine->memory.samples.size);
machine->blip = blip_new(samplerate / 10); machine->blip = blip_new(samplerate / 10);

View File

@ -79,6 +79,7 @@
#define TIC_SAVEID_SIZE 64 #define TIC_SAVEID_SIZE 64
#define TIC_SOUND_CHANNELS 4 #define TIC_SOUND_CHANNELS 4
#define TIC_STEREO_CHANNLES 2
#define SFX_TICKS 30 #define SFX_TICKS 30
#define SFX_COUNT_BITS 6 #define SFX_COUNT_BITS 6
#define SFX_COUNT (1 << SFX_COUNT_BITS) #define SFX_COUNT (1 << SFX_COUNT_BITS)
@ -266,18 +267,32 @@ typedef struct
tic_tracks tracks; tic_tracks tracks;
}tic_music; }tic_music;
typedef enum
{
tic_music_stop,
tic_music_play_frame,
tic_music_play,
} tic_music_state;
typedef struct typedef struct
{ {
s8 track; struct
s8 frame; {
s8 row; s8 track;
s8 frame;
s8 row;
} music;
struct 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; } flag;
} tic_music_pos; } tic_sound_state;
typedef struct typedef struct
{ {
@ -421,7 +436,7 @@ typedef union
tic_sound_register registers[TIC_SOUND_CHANNELS]; tic_sound_register registers[TIC_SOUND_CHANNELS];
tic_sfx sfx; tic_sfx sfx;
tic_music music; tic_music music;
tic_music_pos music_pos; tic_sound_state sound_state;
}; };
u8 data[TIC_RAM_SIZE]; u8 data[TIC_RAM_SIZE];