App-MHFS
view release on metacpan or search on metacpan
share/public_html/static/music_inc/src/drflac_cache.c view on Meta::CPAN
void *network_drflac_open_mem(const size_t filesize, const NetworkDrFlacMem *pMem, NetworkDrFlac_Error *error)
{
printf("network_drflac: allocating %lu\n", sizeof(NetworkDrFlac));
NetworkDrFlac *ndrflac = malloc(sizeof(NetworkDrFlac));
ndrflac->fileoffset = 0;
ndrflac->filesize = filesize;
ndrflac->error = error;
ndrflac->pFlac = NULL;
ndrflac->pMem = pMem;
// finally open the file
drflac *pFlac = drflac_open(&on_read_mem, &on_seek_mem, ndrflac, NULL);
if((pFlac == NULL) || (!NDRFLAC_OK(ndrflac)))
{
if(!NDRFLAC_OK(ndrflac))
{
printf("network_drflac: another error?\n");
if(pFlac != NULL) drflac_close(pFlac);
}
else
{
printf("network_drflac: failed to open drflac\n");
error->code = NDRFLAC_GENERIC_ERROR;
}
}
else
{
printf("network_drflac: opened successfully\n");
ndrflac->pFlac = pFlac;
return ndrflac;
}
free(ndrflac);
return NULL;
}
/* returns of samples */
uint64_t network_drflac_read_pcm_frames_f32_mem(NetworkDrFlac *ndrflac, uint32_t start_pcm_frame, uint32_t desired_pcm_frames, float32_t *outFloat, NetworkDrFlac_Error *error)
{
drflac *pFlac = ndrflac->pFlac;
ndrflac->error = error;
// seek to sample
printf("seek to %u\n", start_pcm_frame);
const uint32_t currentPCMFrame32 = pFlac->currentPCMFrame;
const drflac_bool32 seekres = drflac_seek_to_pcm_frame(pFlac, start_pcm_frame);
if(!NDRFLAC_OK(ndrflac))
{
printf("network_drflac_read_pcm_frames_f32_mem: failed seek_to_pcm_frame NOT OK current: %u desired: %u\n", currentPCMFrame32, start_pcm_frame);
return 0;
}
else if(!seekres)
{
printf("network_drflac_read_pcm_frames_f32_mem: seek failed current: %u desired: %u\n", currentPCMFrame32, start_pcm_frame);
error->code = NDRFLAC_GENERIC_ERROR;
return 0;
}
// decode to pcm
float32_t *data = malloc(pFlac->channels*sizeof(float32_t)*desired_pcm_frames);
uint32_t frames_decoded = drflac_read_pcm_frames_f32(pFlac, desired_pcm_frames, data);
if(frames_decoded != desired_pcm_frames)
{
printf("network_drflac_read_pcm_frames_f32_mem: expected %u decoded %u\n", desired_pcm_frames, frames_decoded);
}
if(!NDRFLAC_OK(ndrflac))
{
printf("network_drflac_read_pcm_frames_f32_mem: failed read_pcm_frames_f32\n");
free(data);
return 0;
}
#ifdef NETWORK_DR_FLAC_FORCE_REDBOOK
// resample
if(pFlac->sampleRate != 44100)
{
printf("Converting samplerate from %u to %u\n", pFlac->sampleRate, 44100);
ma_resampler_config config = ma_resampler_config_init(ma_format_f32, pFlac->channels, pFlac->sampleRate, 44100, ma_resample_algorithm_linear);
ma_resampler resampler;
ma_result result = ma_resampler_init(&config, &resampler);
ma_uint64 frameCountIn = frames_decoded;
ma_uint64 frameCountOut = ma_resampler_get_expected_output_frame_count(&resampler, frameCountIn);
float32_t *pFramesOut = malloc(frameCountOut*pFlac->channels*sizeof(float32_t));
ma_result result_process = ma_resampler_process_pcm_frames(&resampler, data, &frameCountIn, pFramesOut, &frameCountOut);
free(data);
if (result_process != MA_SUCCESS)
{
printf("network_drflac_read_pcm_frames_f32_mem: failed read_pcm_frames_f32 resample\n");
return 0;
}
data = pFramesOut;
frames_decoded = frameCountOut;
}
#endif
// deinterleave
for(unsigned i = 0; i < frames_decoded; i++)
{
for(unsigned j = 0; j < pFlac->channels; j++)
{
unsigned chanIndex = j*frames_decoded;
float32_t sample = data[(i*pFlac->channels) + j];
outFloat[chanIndex+i] = sample;
}
}
printf("returning from start_pcm_frame: %u frames_decoded %u data %p\n", start_pcm_frame, frames_decoded, data);
free(data);
// return number of samples
return frames_decoded;
}
( run in 0.451 second using v1.01-cache-2.11-cpan-d7f47b0818f )