diff --git a/flac.c b/flac.c index 5845a0b..6116417 100644 --- a/flac.c +++ b/flac.c @@ -23,15 +23,16 @@ static int flac_calc_size(drflac *flac, drflac_uint64 frames, size_t *out_bytes) static JSValue flac_make_info(JSContext *js, drflac *flac) { - JSValue obj = JS_NewObject(js); - JS_SetPropertyStr(js, obj, "channels", JS_NewInt32(js, flac->channels)); - JS_SetPropertyStr(js, obj, "sample_rate", JS_NewInt32(js, flac->sampleRate)); - JS_SetPropertyStr(js, obj, "bits_per_sample", JS_NewInt32(js, flac->bitsPerSample)); - JS_SetPropertyStr(js, obj, "total_pcm_frames", JS_NewFloat64(js, (double)flac->totalPCMFrameCount)); - JS_SetPropertyStr(js, obj, "decoded_bytes_per_frame", + JS_FRAME(js); + JS_ROOT(obj, JS_NewObject(js)); + JS_SetPropertyStr(js, obj.val, "channels", JS_NewInt32(js, flac->channels)); + JS_SetPropertyStr(js, obj.val, "sample_rate", JS_NewInt32(js, flac->sampleRate)); + JS_SetPropertyStr(js, obj.val, "bits_per_sample", JS_NewInt32(js, flac->bitsPerSample)); + JS_SetPropertyStr(js, obj.val, "total_pcm_frames", JS_NewFloat64(js, (double)flac->totalPCMFrameCount)); + JS_SetPropertyStr(js, obj.val, "decoded_bytes_per_frame", JS_NewInt32(js, (int)((size_t)flac->channels * sizeof(drflac_int32)))); - JS_SetPropertyStr(js, obj, "format", JS_NewString(js, "s32")); - return obj; + JS_SetPropertyStr(js, obj.val, "format", JS_NewString(js, "s32")); + JS_RETURN(obj.val); } JSC_CCALL(flac_info, @@ -90,17 +91,17 @@ JSC_CCALL(flac_decode, if (pcm_bytes > 0) bytes_read = (size_t)(frames_read * bytes_per_frame); - JSValue result = flac_make_info(js, flac); - + JS_FRAME(js); + JS_ROOT(result, flac_make_info(js, flac)); + // Update format info - JS_SetPropertyStr(js, result, "format", JS_NewString(js, "f32")); - JS_SetPropertyStr(js, result, "decoded_bytes_per_frame", JS_NewInt32(js, (int)bytes_per_frame)); - - JSValue blob = js_new_blob_stoned_copy(js, pcm, bytes_read); - JS_SetPropertyStr(js, result, "pcm", blob); + JS_SetPropertyStr(js, result.val, "format", JS_NewString(js, "f32")); + JS_SetPropertyStr(js, result.val, "decoded_bytes_per_frame", JS_NewInt32(js, (int)bytes_per_frame)); + + JS_SetPropertyStr(js, result.val, "pcm", js_new_blob_stoned_copy(js, pcm, bytes_read)); free(pcm); drflac_close(flac); - return result; + JS_RETURN(result.val); ) static const JSCFunctionListEntry js_flac_funcs[] = { diff --git a/mp3.c b/mp3.c index 072deb9..dea55e7 100644 --- a/mp3.c +++ b/mp3.c @@ -7,17 +7,18 @@ static JSValue mp3_make_info(JSContext *js, drmp3_uint32 channels, drmp3_uint32 sample_rate, drmp3_uint64 frames) { - JSValue obj = JS_NewObject(js); - JS_SetPropertyStr(js, obj, "channels", JS_NewInt32(js, channels)); - JS_SetPropertyStr(js, obj, "sample_rate", JS_NewInt32(js, sample_rate)); - JS_SetPropertyStr(js, obj, "bits_per_sample", JS_NewInt32(js, 16)); + JS_FRAME(js); + JS_ROOT(obj, JS_NewObject(js)); + JS_SetPropertyStr(js, obj.val, "channels", JS_NewInt32(js, channels)); + JS_SetPropertyStr(js, obj.val, "sample_rate", JS_NewInt32(js, sample_rate)); + JS_SetPropertyStr(js, obj.val, "bits_per_sample", JS_NewInt32(js, 16)); double total_frames = frames == DRMP3_UINT64_MAX ? -1.0 : (double)frames; - JS_SetPropertyStr(js, obj, "total_pcm_frames", JS_NewFloat64(js, total_frames)); - JS_SetPropertyStr(js, obj, "decoded_bytes_per_frame", + JS_SetPropertyStr(js, obj.val, "total_pcm_frames", JS_NewFloat64(js, total_frames)); + JS_SetPropertyStr(js, obj.val, "decoded_bytes_per_frame", JS_NewInt32(js, (int)((size_t)channels * sizeof(drmp3_int16)))); - JS_SetPropertyStr(js, obj, "format", JS_NewString(js, "s16")); - return obj; + JS_SetPropertyStr(js, obj.val, "format", JS_NewString(js, "s16")); + JS_RETURN(obj.val); } JSC_CCALL(mp3_info, @@ -81,16 +82,16 @@ JSC_CCALL(mp3_decode, } total_bytes = (size_t)(frames * bytes_per_frame); - JSValue result = mp3_make_info(js, config.channels, config.sampleRate, frames); - - // Update format info - JS_SetPropertyStr(js, result, "format", JS_NewString(js, "f32")); - JS_SetPropertyStr(js, result, "decoded_bytes_per_frame", JS_NewInt32(js, (int)bytes_per_frame)); + JS_FRAME(js); + JS_ROOT(result, mp3_make_info(js, config.channels, config.sampleRate, frames)); - JSValue blob = js_new_blob_stoned_copy(js, pcm, total_bytes); - JS_SetPropertyStr(js, result, "pcm", blob); + // Update format info + JS_SetPropertyStr(js, result.val, "format", JS_NewString(js, "f32")); + JS_SetPropertyStr(js, result.val, "decoded_bytes_per_frame", JS_NewInt32(js, (int)bytes_per_frame)); + + JS_SetPropertyStr(js, result.val, "pcm", js_new_blob_stoned_copy(js, pcm, total_bytes)); drmp3_free(pcm, NULL); - return result; + JS_RETURN(result.val); ) static const JSCFunctionListEntry js_mp3_funcs[] = { diff --git a/wav.c b/wav.c index 3360ccd..fdd4f73 100644 --- a/wav.c +++ b/wav.c @@ -23,14 +23,15 @@ static int wav_calc_size(drwav *wav, drwav_uint64 frames, size_t *out_bytes) static JSValue wav_make_info(JSContext *js, drwav *wav) { - JSValue obj = JS_NewObject(js); - JS_SetPropertyStr(js, obj, "channels", JS_NewInt32(js, wav->channels)); - JS_SetPropertyStr(js, obj, "sample_rate", JS_NewInt32(js, wav->sampleRate)); - JS_SetPropertyStr(js, obj, "bits_per_sample", JS_NewInt32(js, wav->bitsPerSample)); - JS_SetPropertyStr(js, obj, "format_tag", JS_NewInt32(js, wav->translatedFormatTag)); - JS_SetPropertyStr(js, obj, "total_pcm_frames", JS_NewFloat64(js, (double)wav->totalPCMFrameCount)); - JS_SetPropertyStr(js, obj, "bytes_per_frame", JS_NewInt32(js, (int)drwav_get_bytes_per_pcm_frame(wav))); - return obj; + JS_FRAME(js); + JS_ROOT(obj, JS_NewObject(js)); + JS_SetPropertyStr(js, obj.val, "channels", JS_NewInt32(js, wav->channels)); + JS_SetPropertyStr(js, obj.val, "sample_rate", JS_NewInt32(js, wav->sampleRate)); + JS_SetPropertyStr(js, obj.val, "bits_per_sample", JS_NewInt32(js, wav->bitsPerSample)); + JS_SetPropertyStr(js, obj.val, "format_tag", JS_NewInt32(js, wav->translatedFormatTag)); + JS_SetPropertyStr(js, obj.val, "total_pcm_frames", JS_NewFloat64(js, (double)wav->totalPCMFrameCount)); + JS_SetPropertyStr(js, obj.val, "bytes_per_frame", JS_NewInt32(js, (int)drwav_get_bytes_per_pcm_frame(wav))); + JS_RETURN(obj.val); } JSC_CCALL(wav_info, @@ -91,21 +92,21 @@ JSC_CCALL(wav_decode, bytes_read = (size_t)(frames_read * bytes_per_frame); } - JSValue result = wav_make_info(js, &wav); + JS_FRAME(js); + JS_ROOT(result, wav_make_info(js, &wav)); // Update format info to reflect f32 - JS_SetPropertyStr(js, result, "format", JS_NewString(js, "f32")); - JS_SetPropertyStr(js, result, "bytes_per_frame", JS_NewInt32(js, (int)bytes_per_frame)); - + JS_SetPropertyStr(js, result.val, "format", JS_NewString(js, "f32")); + JS_SetPropertyStr(js, result.val, "bytes_per_frame", JS_NewInt32(js, (int)bytes_per_frame)); + if (pcm_bytes > 0) { - JSValue blob = js_new_blob_stoned_copy(js, pcm, bytes_read); - JS_SetPropertyStr(js, result, "pcm", blob); + JS_SetPropertyStr(js, result.val, "pcm", js_new_blob_stoned_copy(js, pcm, bytes_read)); free(pcm); } else { - JS_SetPropertyStr(js, result, "pcm", js_new_blob_stoned_copy(js, NULL, 0)); + JS_SetPropertyStr(js, result.val, "pcm", js_new_blob_stoned_copy(js, NULL, 0)); } drwav_uninit(&wav); - return result; + JS_RETURN(result.val); ) static const JSCFunctionListEntry js_wav_funcs[] = {