static void *asap_play_thread(void *arg) { while (thread_run) { static unsigned char buffer[BUFFERED_BLOCKS * (BITS_PER_SAMPLE / 8) * 2]; int buffered_bytes; if (generated_eof) { xmms_usleep(10000); continue; } if (seek_to >= 0) { mod.output->flush(seek_to); ASAP_Seek(asap, seek_to); seek_to = -1; } buffered_bytes = BUFFERED_BLOCKS * channels * (BITS_PER_SAMPLE / 8); buffered_bytes = ASAP_Generate(asap, buffer, buffered_bytes, BITS_PER_SAMPLE == 8 ? ASAPSampleFormat_U8 : ASAPSampleFormat_S16_L_E); if (buffered_bytes == 0) { generated_eof = TRUE; mod.output->buffer_free(); mod.output->buffer_free(); continue; } mod.add_vis_pcm(mod.output->written_time(), BITS_PER_SAMPLE == 8 ? FMT_U8 : FMT_S16_LE, channels, buffered_bytes, buffer); while (thread_run && mod.output->buffer_free() < buffered_bytes) xmms_usleep(20000); if (thread_run) mod.output->write_audio(buffer, buffered_bytes); } pthread_exit(NULL); }
static int Process(asap* p,format_stream* Stream) { int Result = ERR_NONE; if (Stream->Pending) { Result = Format_Send(&p->Format,Stream); if (Result == ERR_BUFFER_FULL || Result == ERR_SYNCED) return Result; } if (p->Samples >= p->Total) return Format_CheckEof(&p->Format,Stream); ASAP_Generate(p->Buffer, BUFFER_SIZE); Stream->Packet.RefTime = Scale(p->Samples,TICKSPERSEC,SAMPLE_RATE); Stream->Packet.Data[0] = p->Buffer; Stream->Packet.Length = BUFFER_SIZE; Stream->Pending = 1; p->Samples += p->SamplePerBuffer; Result = Format_Send(&p->Format,Stream); if (Result == ERR_BUFFER_FULL || Result == ERR_NEED_MORE_DATA) Result = ERR_NONE; return Result; }
static void WaveOut_Write(LPWAVEHDR pwh) { if (playing) { ASAP_Generate(pwh->lpData, pwh->dwBufferLength); if (waveOutWrite(hwo, pwh, sizeof(WAVEHDR)) != MMSYSERR_NOERROR) WaveOut_Stop(); } }
static DWORD CALLBACK StreamProc(HSTREAM handle, void *buffer, DWORD length, void *inst) { ASAPSTREAM *stream = (ASAPSTREAM *) inst; int c = ASAP_Generate(&stream->asap, buffer, length, ASAP_FORMAT_S16_LE); if (c < length) c |= BASS_STREAMPROC_END; return c; }
static int asap_decode(void *data, char *buf, int buf_len, struct sound_params *sound_params) { ASAP_Decoder *d = (ASAP_Decoder *) data; sound_params->channels = ASAPInfo_GetChannels(ASAP_GetInfo(d->asap)); sound_params->rate = ASAP_SAMPLE_RATE; sound_params->fmt = BITS_PER_SAMPLE == 8 ? SFMT_U8 : (SFMT_S16 | SFMT_LE); return ASAP_Generate(d->asap, (unsigned char *) buf, buf_len, BITS_PER_SAMPLE == 8 ? ASAPSampleFormat_U8 : ASAPSampleFormat_S16_L_E); }
static void WaveOut_Write(LPWAVEHDR pwh) { if (playing) { int len = ASAP_Generate(&asap, pwh->lpData, pwh->dwBufferLength, BITS_PER_SAMPLE); if (len < (int) pwh->dwBufferLength || waveOutWrite(hwo, pwh, sizeof(WAVEHDR)) != MMSYSERR_NOERROR) { /* calling StopPlayback() here causes a deadlock */ PostMessage(hWnd, WM_COMMAND, IDM_STOP, 0); } } }
int ReadPCM(void* context, uint8_t* pBuffer, int size, int *actualsize) { if (!context) return 1; ASAPContext* ctx = (ASAPContext*)context; *actualsize = ASAP_Generate(ctx->asap, pBuffer, size, ASAPSampleFormat_S16_L_E); return *actualsize == 0; }
static DWORD WINAPI playThread(LPVOID dummy) { while (thread_run) { static #if BITS_PER_SAMPLE == 8 byte #else short #endif buffer[BUFFERED_BLOCKS * 2 #if SUPPORT_EQUALIZER * 2 #endif ]; int buffered_bytes = BUFFERED_BLOCKS * channels * (BITS_PER_SAMPLE / 8); if (seek_needed >= 0) { mod.outMod->Flush(seek_needed); ASAP_Seek(&asap, seek_needed); seek_needed = -1; } if (mod.outMod->CanWrite() >= buffered_bytes #if SUPPORT_EQUALIZER << mod.dsp_isactive() #endif ) { int t; buffered_bytes = ASAP_Generate(&asap, buffer, buffered_bytes, BITS_PER_SAMPLE); if (buffered_bytes <= 0) { mod.outMod->CanWrite(); if (!mod.outMod->IsPlaying()) { PostMessage(mod.hMainWindow, WM_WA_MPEG_EOF, 0, 0); return 0; } Sleep(10); continue; } t = mod.outMod->GetWrittenTime(); mod.SAAddPCMData(buffer, channels, BITS_PER_SAMPLE, t); mod.VSAAddPCMData(buffer, channels, BITS_PER_SAMPLE, t); #if SUPPORT_EQUALIZER t = buffered_bytes / (channels * (BITS_PER_SAMPLE / 8)); t = mod.dsp_dosamples((short *) buffer, t, BITS_PER_SAMPLE, channels, ASAP_SAMPLE_RATE); t *= channels * (BITS_PER_SAMPLE / 8); mod.outMod->Write((char *) buffer, t); #else mod.outMod->Write((char *) buffer, buffered_bytes); #endif } else Sleep(20); } return 0; }
int EMSCRIPTEN_KEEPALIVE asap_generate(unsigned char *buffer, int bufferLen, int format) { ASAPSampleFormat f; switch(format) { case 1: f= ASAPSampleFormat_S16_L_E; break; case 2: f= ASAPSampleFormat_S16_B_E; break; default: f= ASAPSampleFormat_U8; break; } return ASAP_Generate(getAsap(), buffer, bufferLen, f); }
bool decode_run(audio_chunk &p_chunk, abort_callback &p_abort) { int channels = ASAPInfo_GetChannels(ASAP_GetInfo(asap)); int buffered_bytes = BUFFERED_BLOCKS * channels * (BITS_PER_SAMPLE / 8); static BYTE buffer[BUFFERED_BLOCKS * 2 * (BITS_PER_SAMPLE / 8)]; buffered_bytes = ASAP_Generate(asap, buffer, buffered_bytes, BITS_PER_SAMPLE == 8 ? ASAPSampleFormat_U8 : ASAPSampleFormat_S16_L_E); if (buffered_bytes == 0) return false; p_chunk.set_data_fixedpoint(buffer, buffered_bytes, ASAP_SAMPLE_RATE, channels, BITS_PER_SAMPLE, channels == 2 ? audio_chunk::channel_config_stereo : audio_chunk::channel_config_mono); return true; }
static BOOL DoSaveWav(ASAP_State *asap) { HANDLE fh; byte buffer[8192]; DWORD len; fh = CreateFile(wav_filename, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL); if (fh == INVALID_HANDLE_VALUE) return FALSE; ASAP_GetWavHeader(asap, buffer, BITS_PER_SAMPLE); len = ASAP_WAV_HEADER_BYTES; while (len > 0) { if (!WriteFile(fh, buffer, len, &len, NULL)) { CloseHandle(fh); return FALSE; } len = ASAP_Generate(asap, buffer, sizeof(buffer), BITS_PER_SAMPLE); } CloseHandle(fh); return TRUE; }
bool decode_run(audio_chunk &p_chunk, abort_callback &p_abort) { int channels = module_info.channels; int buffered_bytes = BUFFERED_BLOCKS * channels * (BITS_PER_SAMPLE / 8); static #if BITS_PER_SAMPLE == 8 byte #else short #endif buffer[BUFFERED_BLOCKS * 2]; buffered_bytes = ASAP_Generate(&asap, buffer, buffered_bytes, (ASAP_SampleFormat) BITS_PER_SAMPLE); if (buffered_bytes == 0) return false; p_chunk.set_data_fixedpoint(buffer, buffered_bytes, ASAP_SAMPLE_RATE, channels, BITS_PER_SAMPLE, channels == 2 ? audio_chunk::channel_config_stereo : audio_chunk::channel_config_mono); return true; }
HRESULT FillBuffer(IMediaSample *pSample) { CheckPointer(pSample, E_POINTER); CAutoLock lck(&cs); if (!loaded) return E_FAIL; BYTE *pData; HRESULT hr = pSample->GetPointer(&pData); if (FAILED(hr)) return hr; int cbData = pSample->GetSize(); cbData = ASAP_Generate(&asap, pData, cbData, (ASAP_SampleFormat) BITS_PER_SAMPLE); if (cbData == 0) return S_FALSE; pSample->SetActualDataLength(cbData); LONGLONG startTime = blocks * UNITS / ASAP_SAMPLE_RATE; blocks += cbData / (asap.module_info.channels * (BITS_PER_SAMPLE / 8)); LONGLONG endTime = blocks * UNITS / ASAP_SAMPLE_RATE; pSample->SetTime(&startTime, &endTime); pSample->SetSyncPoint(TRUE); return S_OK; }
static void *play_thread(void *arg) { for (;;) { static #if BITS_PER_SAMPLE == 8 unsigned char #else short int #endif buffer[BUFFERED_BLOCKS * 2]; ASAP_Generate(buffer, buffered_bytes); mod.add_vis_pcm(mod.output->written_time(), BITS_PER_SAMPLE == 8 ? FMT_U8 : FMT_S16_NE, channels, buffered_bytes, buffer); while (thread_run && mod.output->buffer_free() < buffered_bytes) xmms_usleep(20000); if (!thread_run) break; mod.output->write_audio(buffer, buffered_bytes); } mod.output->buffer_free(); mod.output->buffer_free(); pthread_exit(NULL); }
static int WINAPI asapDecodeFile(WAVEHDR *pHdr) { int len = ASAP_Generate(&asap, pHdr->lpData, pHdr->dwBufferLength, BITS_PER_SAMPLE); pHdr->dwBytesRecorded = len; return len < (int) pHdr->dwBufferLength ? PLUGIN_RET_EOF : PLUGIN_RET_SUCCESS; }
static gboolean play_start(InputPlayback *playback, const char *filename, VFSFile *file, int start_time, int stop_time, gboolean pause) { int song = -1; unsigned char module[ASAPInfo_MAX_MODULE_LENGTH]; int module_len; gboolean ok; const ASAPInfo *info; int channels; #if _AUD_PLUGIN_VERSION >= 10 char *real_filename = filename_split_subtune(filename, &song); if (real_filename != NULL) filename = real_filename; #endif module_len = load_module(filename, file, module); ok = module_len > 0 && ASAP_Load(asap, filename, module, module_len); #if _AUD_PLUGIN_VERSION >= 10 g_free(real_filename); #endif if (!ok) return FALSE; info = ASAP_GetInfo(asap); channels = ASAPInfo_GetChannels(info); if (song > 0) song--; else song = ASAPInfo_GetDefaultSong(info); if (stop_time < 0) stop_time = ASAPInfo_GetDuration(info, song); if (!ASAP_PlaySong(asap, song, stop_time)) return FALSE; if (start_time > 0) ASAP_Seek(asap, start_time); if (!playback->output->open_audio(BITS_PER_SAMPLE == 8 ? FMT_U8 : FMT_S16_LE, ASAP_SAMPLE_RATE, channels)) return FALSE; playback->set_params(playback, #if _AUD_PLUGIN_VERSION < 18 NULL, 0, #endif 0, ASAP_SAMPLE_RATE, channels); if (pause) playback->output->pause(TRUE); playing = TRUE; playback->set_pb_ready(playback); for (;;) { static unsigned char buffer[4096]; int len; pthread_mutex_lock(&control_mutex); if (!playing) { pthread_mutex_unlock(&control_mutex); break; } len = ASAP_Generate(asap, buffer, sizeof(buffer), BITS_PER_SAMPLE == 8 ? ASAPSampleFormat_U8 : ASAPSampleFormat_S16_L_E); pthread_mutex_unlock(&control_mutex); if (len <= 0) { #if _AUD_PLUGIN_VERSION < 18 playback->eof = TRUE; #endif break; } #if _AUD_PLUGIN_VERSION >= 14 playback->output->write_audio(buffer, len); #else playback->pass_audio(playback, BITS_PER_SAMPLE == 8 ? FMT_U8 : FMT_S16_LE, channels, len, buffer, NULL); #endif } #if _AUD_PLUGIN_VERSION_MIN < 40 while (playing && playback->output->buffer_playing()) g_usleep(10000); #endif pthread_mutex_lock(&control_mutex); playing = FALSE; pthread_mutex_unlock(&control_mutex); #if _AUD_PLUGIN_VERSION_MIN < 40 playback->output->close_audio(); #endif return TRUE; }
int __cdecl GetNextAudioChunk(void **buffer) { *buffer = buf; return ASAP_Generate(&asap, buf, sizeof(buf), (ASAP_SampleFormat) BITS_PER_SAMPLE); }
int __cdecl GetNextAudioChunk(void **buffer) { *buffer = buf; return ASAP_Generate(asap, buf, sizeof(buf), BITS_PER_SAMPLE == 8 ? ASAPSampleFormat_U8 : ASAPSampleFormat_S16_L_E); }
/* this is the codec entry point */ enum codec_status codec_main(void) { int n_bytes; int song; int duration; char* module; int bytesPerSample =2; next_track: if (codec_init()) { DEBUGF("codec init failed\n"); return CODEC_ERROR; } while (!*ci->taginfo_ready && !ci->stop_codec) ci->sleep(1); codec_set_replaygain(ci->id3); int bytes_done =0; size_t filesize; ci->seek_buffer(0); module = ci->request_buffer(&filesize, ci->filesize); if (!module || (size_t)filesize < (size_t)ci->filesize) { DEBUGF("loading error\n"); return CODEC_ERROR; } /*Init ASAP */ if (!ASAP_Load(&asap, ci->id3->path, module, filesize)) { DEBUGF("%s: format not supported",ci->id3->path); return CODEC_ERROR; } /* Make use of 44.1khz */ ci->configure(DSP_SET_FREQUENCY, 44100); /* Sample depth is 16 bit little endian */ ci->configure(DSP_SET_SAMPLE_DEPTH, 16); /* Stereo or Mono output ? */ if(asap.module_info.channels ==1) { ci->configure(DSP_SET_STEREO_MODE, STEREO_MONO); bytesPerSample = 2; } else { ci->configure(DSP_SET_STEREO_MODE, STEREO_INTERLEAVED); bytesPerSample = 4; } /* reset eleapsed */ ci->set_elapsed(0); song = asap.module_info.default_song; duration = asap.module_info.durations[song]; if (duration < 0) duration = 180 * 1000; /* set id3 length, because metadata parse might not have done it */ ci->id3->length = duration; ASAP_PlaySong(&asap, song, duration); ASAP_MutePokeyChannels(&asap, 0); /* The main decoder loop */ while (1) { ci->yield(); if (ci->stop_codec || ci->new_track) break; if (ci->seek_time) { /* New time is ready in ci->seek_time */ /* seek to pos */ ASAP_Seek(&asap,ci->seek_time); /* update elapsed */ ci->set_elapsed(ci->seek_time); /* update bytes_done */ bytes_done = ci->seek_time*44.1*2; /* seek ready */ ci->seek_complete(); } /* Generate a buffer full of Audio */ #ifdef ROCKBOX_LITTLE_ENDIAN n_bytes = ASAP_Generate(&asap, samples, sizeof(samples), ASAP_FORMAT_S16_LE); #else n_bytes = ASAP_Generate(&asap, samples, sizeof(samples), ASAP_FORMAT_S16_BE); #endif ci->pcmbuf_insert(samples, NULL, n_bytes /bytesPerSample); bytes_done += n_bytes; ci->set_elapsed((bytes_done / 2) / 44.1); if(n_bytes != sizeof(samples)) break; } if (ci->request_next_track()) goto next_track; return CODEC_OK; }
static void audio_callback(void *userdata, Uint8 *stream, int len) { ASAP_Generate((ASAP *) userdata, stream, len, ASAPSampleFormat_S16_L_E); }