Skip to content

Commit

Permalink
quit gracefully
Browse files Browse the repository at this point in the history
  • Loading branch information
ravachol committed Dec 1, 2023
1 parent e31b29e commit e89f029
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 21 deletions.
2 changes: 2 additions & 0 deletions src/kew.c
Original file line number Diff line number Diff line change
Expand Up @@ -652,6 +652,7 @@ void play(Node *song)

void cleanupOnExit()
{
pthread_mutex_lock(&dataSourceMutex);
resetDecoders();
resetVorbisDecoders();
resetOpusDecoders();
Expand All @@ -675,6 +676,7 @@ void cleanupOnExit()
free(mainPlaylist);
freeVisuals();
showCursor();
pthread_mutex_unlock(&dataSourceMutex);

#ifdef DEBUG
fclose(logFile);
Expand Down
34 changes: 17 additions & 17 deletions src/soundbuiltin.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ static ma_result builtin_file_data_source_read(ma_data_source *pDataSource, void

static ma_result builtin_file_data_source_seek(ma_data_source *pDataSource, ma_uint64 frameIndex)
{
AudioData *pPCMDataSource = (AudioData *)pDataSource;
AudioData *audioData = (AudioData *)pDataSource;

if (getCurrentDecoder() == NULL)
{
Expand All @@ -30,7 +30,7 @@ static ma_result builtin_file_data_source_seek(ma_data_source *pDataSource, ma_u

if (result == MA_SUCCESS)
{
pPCMDataSource->currentPCMFrame = (ma_uint32)frameIndex;
audioData->currentPCMFrame = (ma_uint32)frameIndex;
return MA_SUCCESS;
}
else
Expand All @@ -43,17 +43,17 @@ static ma_result builtin_file_data_source_get_data_format(ma_data_source *pDataS
{
(void)pChannelMap;
(void)channelMapCap;
AudioData *pPCMDataSource = (AudioData *)pDataSource;
*pFormat = pPCMDataSource->format;
*pChannels = pPCMDataSource->channels;
*pSampleRate = pPCMDataSource->sampleRate;
AudioData *audioData = (AudioData *)pDataSource;
*pFormat = audioData->format;
*pChannels = audioData->channels;
*pSampleRate = audioData->sampleRate;
return MA_SUCCESS;
}

static ma_result builtin_file_data_source_get_cursor(ma_data_source *pDataSource, ma_uint64 *pCursor)
{
AudioData *pPCMDataSource = (AudioData *)pDataSource;
*pCursor = pPCMDataSource->currentPCMFrame;
AudioData *audioData = (AudioData *)pDataSource;
*pCursor = audioData->currentPCMFrame;

return MA_SUCCESS;
}
Expand Down Expand Up @@ -100,7 +100,7 @@ ma_data_source_vtable builtin_file_data_source_vtable = {

void builtin_read_pcm_frames(ma_data_source *pDataSource, void *pFramesOut, ma_uint64 frameCount, ma_uint64 *pFramesRead)
{
AudioData *pPCMDataSource = (AudioData *)pDataSource;
AudioData *audioData = (AudioData *)pDataSource;
ma_uint64 framesRead = 0;

while (framesRead < frameCount)
Expand All @@ -112,15 +112,15 @@ void builtin_read_pcm_frames(ma_data_source *pDataSource, void *pFramesOut, ma_u
return;
}

if (isImplSwitchReached() || pPCMDataSource == NULL)
if (isImplSwitchReached() || audioData == NULL)
{
pthread_mutex_unlock(&dataSourceMutex);
return;
}

if (pPCMDataSource->switchFiles)
if (audioData->switchFiles)
{
executeSwitch(pPCMDataSource);
executeSwitch(audioData);
pthread_mutex_unlock(&dataSourceMutex);
break;
}
Expand All @@ -133,12 +133,12 @@ void builtin_read_pcm_frames(ma_data_source *pDataSource, void *pFramesOut, ma_u
return;
}

if (pPCMDataSource->totalFrames == 0)
ma_data_source_get_length_in_pcm_frames(decoder, &pPCMDataSource->totalFrames);
if (audioData->totalFrames == 0)
ma_data_source_get_length_in_pcm_frames(decoder, &audioData->totalFrames);

if (isSeekRequested())
{
ma_uint64 totalFrames = pPCMDataSource->totalFrames;
ma_uint64 totalFrames = audioData->totalFrames;
ma_uint64 seekPercent = getSeekPercentage();
if (seekPercent >= 100.0)
seekPercent = 100.0;
Expand Down Expand Up @@ -168,9 +168,9 @@ void builtin_read_pcm_frames(ma_data_source *pDataSource, void *pFramesOut, ma_u
result = ma_data_source_read_pcm_frames(firstDecoder, (ma_int32 *)pFramesOut + framesRead * decoder->outputChannels, remainingFrames, &framesToRead);
ma_data_source_get_cursor_in_pcm_frames(decoder, &cursor);

if (((pPCMDataSource->totalFrames != 0 && cursor != 0 && cursor >= pPCMDataSource->totalFrames) || framesToRead == 0 || isSkipToNext() || result != MA_SUCCESS) && !isEOFReached())
if (((audioData->totalFrames != 0 && cursor != 0 && cursor >= audioData->totalFrames) || framesToRead == 0 || isSkipToNext() || result != MA_SUCCESS) && !isEOFReached())
{
activateSwitch(pPCMDataSource);
activateSwitch(audioData);
pthread_mutex_unlock(&dataSourceMutex);
continue;
}
Expand Down
4 changes: 0 additions & 4 deletions src/soundbuiltin.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,6 @@ extern ma_data_source_vtable builtin_file_data_source_vtable;

void builtin_read_pcm_frames(ma_data_source *pDataSource, void *pFramesOut, ma_uint64 frameCount, ma_uint64 *pFramesRead);

void builtin_read_pcm_frames(ma_data_source *pDataSource, void *pFramesOut, ma_uint64 frameCount, ma_uint64 *pFramesRead);

void builtin_read_pcm_frames(ma_data_source *pDataSource, void *pFramesOut, ma_uint64 frameCount, ma_uint64 *pFramesRead);

void builtin_on_audio_frames(ma_device *pDevice, void *pFramesOut, const void *pFramesIn, ma_uint32 frameCount);

#endif

0 comments on commit e89f029

Please sign in to comment.