/* Copyright (c) 2013 Alex Diener This software is provided 'as-is', without any express or implied warranty. In no event will the authors be held liable for any damages arising from the use of this software. Permission is granted to anyone to use this software for any purpose, including commercial applications, and to alter it and redistribute it freely, subject to the following restrictions: 1. The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required. 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software. 3. This notice may not be removed or altered from any source distribution. Alex Diener adiener@sacredsoftware.net */ #include "vorbisaudioio/VorbisAudioIO.h" #include "vorbisaudioio/VorbisAudioIOPrivate.h" #include "vorbis/codec.h" #include "vorbis/vorbisenc.h" #include "vorbis/vorbisfile.h" #include "utilities/IOUtilities.h" #include #include PCMAudio * VorbisAudioIO_loadOggVorbisFile(const char * filePath) { PCMAudio * audio; void * fileContents; size_t length = 0; fileContents = readFileSimple(filePath, &length); audio = VorbisAudioIO_loadOggVorbisData(fileContents, length); free(fileContents); return audio; } #define BUFFER_SIZE 16384 PCMAudio * VorbisAudioIO_loadOggVorbisData(const void * data, size_t length) { struct memreadContext readContext; ov_callbacks callbacks; OggVorbis_File file; int status; vorbis_info * info; unsigned int channelCount; unsigned int sampleRate; unsigned int sampleCount; long readResult; struct memwriteContext writeContext; char buffer[BUFFER_SIZE]; int currentSection; PCMAudio * audio; readContext = memreadContextInit(data, length); callbacks.read_func = VorbisAudioIO_memreadFunc; callbacks.seek_func = VorbisAudioIO_memseekFunc; callbacks.close_func = NULL; callbacks.tell_func = VorbisAudioIO_memtellFunc; status = ov_open_callbacks(&readContext, &file, NULL, 0, callbacks); if (status != 0) { #ifdef DEBUG fprintf(stderr, "Error: ov_open_callbacks returned %d\n", status); #endif return NULL; } info = ov_info(&file, -1); if (info == NULL) { #ifdef DEBUG fprintf(stderr, "Error: ov_info returned NULL\n"); #endif return NULL; } channelCount = info->channels; sampleRate = info->rate; sampleCount = ov_pcm_total(&file, -1); writeContext = memwriteContextInit(malloc(1), 0, 1, true); for (;;) { readResult = ov_read(&file, buffer, BUFFER_SIZE, OV_READ_ENDIANNESS, 2, 1, ¤tSection); if (readResult == 0) { break; } if (readResult < 0) { #ifdef DEBUG fprintf(stderr, "Error: ov_read returned %ld\n", readResult); #endif break; } memwrite(&writeContext, readResult, buffer); } ov_clear(&file); if (readResult < 0) { free(writeContext.data); return NULL; } audio = PCMAudio_create(2, channelCount, sampleRate, sampleCount, writeContext.data, true); free(writeContext.data); return audio; } #ifdef DEBUG #define checkStatus(function, label) \ status = function; \ if (status < 0) { \ fprintf(stderr, "Error: " #function " returned %d\n", status); \ goto label; \ } #else #define checkStatus(function, label) \ status = function; \ if (status < 0) { \ goto label; \ } #endif bool VorbisAudioIO_writeOggVorbisFile(PCMAudio * audio, const char * filePath, float quality) { FILE * outFile = fopen(filePath, "wb"); if (outFile == NULL) { #ifdef DEBUG fprintf(stderr, "Error: Unable to open \"%s\" for writing (errno = %d)\n", filePath, errno); #endif return false; } vorbis_info info; vorbis_info_init(&info); int status; checkStatus(vorbis_encode_init_vbr(&info, audio->channelCount, audio->sampleRate, quality), fail1); vorbis_dsp_state dspState; checkStatus(vorbis_analysis_init(&dspState, &info), fail1); vorbis_comment comment; vorbis_comment_init(&comment); ogg_packet headerPacket, commentPacket, codePacket; checkStatus(vorbis_analysis_headerout(&dspState, &comment, &headerPacket, &commentPacket, &codePacket), fail2); vorbis_block block; checkStatus(vorbis_block_init(&dspState, &block), fail3); ogg_stream_state streamState; checkStatus(ogg_stream_init(&streamState, 0), fail3); checkStatus(ogg_stream_packetin(&streamState, &headerPacket), fail4); checkStatus(ogg_stream_packetin(&streamState, &commentPacket), fail4); checkStatus(ogg_stream_packetin(&streamState, &codePacket), fail4); ogg_page page; while (ogg_stream_flush(&streamState, &page)) { fwrite(page.header, 1, page.header_len, outFile); fwrite(page.body, 1, page.body_len, outFile); } float ** channelOutBuffers = vorbis_analysis_buffer(&dspState, audio->frameCount); unsigned int channelCount = audio->channelCount; for (unsigned int channelIndex = 0; channelIndex < channelCount; channelIndex++) { if (audio->bytesPerSample == 4) { float * samplesTyped = audio->samples; for (size_t frameIndex = 0; frameIndex < audio->frameCount; frameIndex++) { channelOutBuffers[channelIndex][frameIndex] = samplesTyped[frameIndex * channelCount + channelIndex]; } } else if (audio->bytesPerSample == 2) { int16_t * samplesTyped = audio->samples; for (size_t frameIndex = 0; frameIndex < audio->frameCount; frameIndex++) { channelOutBuffers[channelIndex][frameIndex] = samplesTyped[frameIndex * channelCount + channelIndex] / 32768.0f; } } else { int8_t * samplesTyped = audio->samples; for (size_t frameIndex = 0; frameIndex < audio->frameCount; frameIndex++) { channelOutBuffers[channelIndex][frameIndex] = samplesTyped[frameIndex * channelCount + channelIndex] / 128.0f; } } } checkStatus(vorbis_analysis_wrote(&dspState, audio->frameCount), fail4); checkStatus(vorbis_analysis_wrote(&dspState, 0), fail4); bool blockoutDone = false; while (!blockoutDone) { checkStatus(vorbis_analysis_blockout(&dspState, &block), fail4); blockoutDone = status == 0; checkStatus(vorbis_analysis(&block, NULL), fail4); checkStatus(vorbis_bitrate_addblock(&block), fail4); bool flushpacketDone = false; while (!flushpacketDone) { ogg_packet audioPacket; checkStatus(vorbis_bitrate_flushpacket(&dspState, &audioPacket), fail4); flushpacketDone = status == 0; if (!flushpacketDone) { checkStatus(ogg_stream_packetin(&streamState, &audioPacket), fail4); while (ogg_stream_pageout(&streamState, &page)) { fwrite(page.header, 1, page.header_len, outFile); fwrite(page.body, 1, page.body_len, outFile); } } } } ogg_stream_clear(&streamState); vorbis_block_clear(&block); vorbis_comment_clear(&comment); vorbis_dsp_clear(&dspState); vorbis_info_clear(&info); fclose(outFile); return true; fail4: ogg_stream_clear(&streamState); fail3: vorbis_block_clear(&block); fail2: vorbis_comment_clear(&comment); vorbis_dsp_clear(&dspState); fail1: vorbis_info_clear(&info); fclose(outFile); return false; } void * VorbisAudioIO_writeOggVorbisData(PCMAudio * audio, size_t * outLength, float quality) { return NULL; }