Merge pull request #126 from kd7tck/develop

clean up audio chiptunes support
This commit is contained in:
Ray 2016-06-08 22:23:23 +02:00
commit ee049641ba
2 changed files with 40 additions and 31 deletions

View File

@ -107,6 +107,7 @@ typedef struct Music {
float totalLengthSeconds;
bool loop;
bool chipTune; // chiptune is loaded?
bool enabled;
} Music;
// Audio errors registered
@ -831,7 +832,7 @@ int PlayMusicStream(int index, char *fileName)
TraceLog(DEBUG, "[%s] Temp memory required: %i", fileName, info.temp_memory_required);
musicChannels_g[index].loop = true; // We loop by default
musicEnabled_g = true;
musicChannels_g[index].enabled = true;
musicChannels_g[index].totalSamplesLeft = (unsigned int)stb_vorbis_stream_length_in_samples(musicChannels_g[index].stream) * info.channels;
@ -861,7 +862,7 @@ int PlayMusicStream(int index, char *fileName)
jar_xm_set_max_loop_count(musicChannels_g[index].xmctx, 0); // infinite number of loops
musicChannels_g[index].totalSamplesLeft = (unsigned int)jar_xm_get_remaining_samples(musicChannels_g[index].xmctx);
musicChannels_g[index].totalLengthSeconds = ((float)musicChannels_g[index].totalSamplesLeft) / 48000.f;
musicEnabled_g = true;
musicChannels_g[index].enabled = true;
TraceLog(INFO, "[%s] XM number of samples: %i", fileName, musicChannels_g[index].totalSamplesLeft);
TraceLog(INFO, "[%s] XM track length: %11.6f sec", fileName, musicChannels_g[index].totalLengthSeconds);
@ -888,7 +889,7 @@ int PlayMusicStream(int index, char *fileName)
musicChannels_g[index].loop = true;
musicChannels_g[index].totalSamplesLeft = (unsigned int)jar_mod_max_samples(&musicChannels_g[index].modctx);
musicChannels_g[index].totalLengthSeconds = ((float)musicChannels_g[index].totalSamplesLeft) / 48000.f;
musicEnabled_g = true;
musicChannels_g[index].enabled = true;
TraceLog(INFO, "[%s] MOD number of samples: %i", fileName, musicChannels_g[index].totalSamplesLeft);
TraceLog(INFO, "[%s] MOD track length: %11.6f sec", fileName, musicChannels_g[index].totalLengthSeconds);
@ -921,15 +922,14 @@ void StopMusicStream(int index)
{
CloseMixChannel(musicChannels_g[index].mixc);
if (musicChannels_g[index].chipTune && musicChannels_g[index].xmctx)
{
if (musicChannels_g[index].xmctx)
jar_xm_free_context(musicChannels_g[index].xmctx);
musicChannels_g[index].xmctx = 0;
}
else if (musicChannels_g[index].chipTune && musicChannels_g[index].modctx.mod_loaded) jar_mod_unload(&musicChannels_g[index].modctx);
else stb_vorbis_close(musicChannels_g[index].stream);
else if (musicChannels_g[index].modctx.mod_loaded)
jar_mod_unload(&musicChannels_g[index].modctx);
else
stb_vorbis_close(musicChannels_g[index].stream);
if (!GetMusicStreamCount()) musicEnabled_g = false;
musicChannels_g[index].enabled = false;
if (musicChannels_g[index].stream || musicChannels_g[index].xmctx)
{
@ -957,7 +957,7 @@ int GetMusicStreamCount(void)
void PauseMusicStream(int index)
{
// Pause music stream if music available!
if (index < MAX_MUSIC_STREAMS && musicChannels_g[index].mixc && musicEnabled_g)
if (index < MAX_MUSIC_STREAMS && musicChannels_g[index].mixc && musicChannels_g[index].enabled)
{
TraceLog(INFO, "Pausing music stream");
alSourcePause(musicChannels_g[index].mixc->alSource);
@ -1154,7 +1154,7 @@ void UpdateMusicStream(int index)
bool active = true;
int numBuffers = IsMusicStreamReadyForBuffering(index);
if (musicChannels_g[index].mixc->playing && (index < MAX_MUSIC_STREAMS) && musicEnabled_g && musicChannels_g[index].mixc && numBuffers)
if (musicChannels_g[index].mixc->playing && (index < MAX_MUSIC_STREAMS) && musicChannels_g[index].enabled && musicChannels_g[index].mixc && numBuffers)
{
active = BufferMusicStream(index, numBuffers);
@ -1163,7 +1163,8 @@ void UpdateMusicStream(int index)
if (musicChannels_g[index].chipTune)
{
if(musicChannels_g[index].modctx.mod_loaded) jar_mod_seek_start(&musicChannels_g[index].modctx);
musicChannels_g[index].totalSamplesLeft = musicChannels_g[index].totalLengthSeconds * 48000;
musicChannels_g[index].totalSamplesLeft = musicChannels_g[index].totalLengthSeconds * 48000.f;
}
else
{
@ -1171,7 +1172,7 @@ void UpdateMusicStream(int index)
musicChannels_g[index].totalSamplesLeft = stb_vorbis_stream_length_in_samples(musicChannels_g[index].stream) * musicChannels_g[index].mixc->channels;
}
active = true;
active = BufferMusicStream(index, IsMusicStreamReadyForBuffering(index));
}
if (alGetError() != AL_NO_ERROR) TraceLog(WARNING, "Error buffering data...");

View File

@ -1063,7 +1063,6 @@ bool jar_mod_init(jar_mod_context_t * modctx)
modctx->stereo_separation = 1;
modctx->bits = 16;
modctx->filter = 1;
modctx->loopcount = 0;
for(i=0; i < PERIOD_TABLE_LENGTH - 1; i++)
{
@ -1472,7 +1471,7 @@ void jar_mod_fillbuffer( jar_mod_context_t * modctx, short * outbuffer, unsigned
}
//resets internals for mod context
static void jar_mod_reset( jar_mod_context_t * modctx)
static bool jar_mod_reset( jar_mod_context_t * modctx)
{
if(modctx)
{
@ -1488,7 +1487,6 @@ static void jar_mod_reset( jar_mod_context_t * modctx)
modctx->patterntickse = 0;
modctx->patternticksaim = 0;
modctx->sampleticksconst = 0;
modctx->loopcount = 0;
modctx->samplenb = 0;
memclear(modctx->channels, 0, sizeof(modctx->channels));
modctx->number_of_channels = 0;
@ -1496,8 +1494,9 @@ static void jar_mod_reset( jar_mod_context_t * modctx)
modctx->last_r_sample = 0;
modctx->last_l_sample = 0;
jar_mod_init(modctx);
return jar_mod_init(modctx);
}
return 0;
}
void jar_mod_unload( jar_mod_context_t * modctx)
@ -1508,6 +1507,8 @@ void jar_mod_unload( jar_mod_context_t * modctx)
{
free(modctx->modfile);
modctx->modfile = 0;
modctx->modfilesize = 0;
modctx->loopcount = 0;
}
jar_mod_reset(modctx);
}
@ -1556,27 +1557,34 @@ mulong jar_mod_current_samples(jar_mod_context_t * modctx)
// Works, however it is very slow, this data should be cached to ensure it is run only once per file
mulong jar_mod_max_samples(jar_mod_context_t * ctx)
{
jar_mod_context_t tmpctx;
jar_mod_init(&tmpctx);
if(!jar_mod_load(&tmpctx, (void*)ctx->modfile, ctx->modfilesize)) return 0;
muint buff[2];
mulong lastcount = tmpctx.loopcount;
mulong len;
mulong lastcount = ctx->loopcount;
while(1){
jar_mod_fillbuffer( &tmpctx, buff, 1, 0 );
if(tmpctx.loopcount > lastcount) break;
}
return tmpctx.samplenb;
while(ctx->loopcount <= lastcount)
jar_mod_fillbuffer(ctx, buff, 1, 0);
len = ctx->samplenb;
jar_mod_seek_start(ctx);
return len;
}
// move seek_val to sample index, 0 -> jar_mod_max_samples is the range
void jar_mod_seek_start(jar_mod_context_t * ctx)
{
if(ctx)
if(ctx && ctx->modfile)
{
jar_mod_reset(ctx);
jar_mod_load(ctx, ctx->modfile, ctx->modfilesize);
muchar* ftmp = ctx->modfile;
mulong stmp = ctx->modfilesize;
muint lcnt = ctx->loopcount;
if(jar_mod_reset(ctx)){
jar_mod_load(ctx, ftmp, stmp);
ctx->modfile = ftmp;
ctx->modfilesize = stmp;
ctx->loopcount = lcnt;
}
}
}