Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // code to convert little inferno sound resources to ogg files
- // the version of libogg that you use here must be version 1.1.4
- // dec 12, 2012 - allan blomquist
- #define _CRT_SECURE_NO_WARNINGS
- #include <stdio.h>
- #include <stdlib.h>
- // MUST use libogg-1.1.4 - not any other version!
- #include <ogg/ogg.h>
- struct SoundHeader
- {
- int totalSampleCountPerChannel;
- int oggTotalDataSizeBytes;
- int vorbisMarkersSizeBytes;
- int vorbisWorkingSetSizeBytes;
- int vorbisPacketsSizeBytes;
- int samplesPerSec;
- int channels;
- };
- struct StreamMarker
- {
- int packetStreamByteOffset;
- int pcmSampleOffset;
- int packetIdx;
- };
- int main( int argc, const char *argv[] )
- {
- FILE *fSrc = fopen( argv[1], "rb" );
- FILE *fDest = fopen( "out.ogg", "wb" );
- // read header
- SoundHeader hdr;
- fread( &hdr, 1, sizeof(hdr), fSrc );
- // read stream markers
- int numMarkers = hdr.vorbisMarkersSizeBytes / sizeof(StreamMarker);
- StreamMarker *pMarkers = new StreamMarker[ numMarkers ];
- fread( pMarkers, sizeof(StreamMarker), numMarkers, fSrc );
- // setup for making an ogg stream - serial number isn't going to matter
- ogg_stream_state os;
- const int kOggStreamSerialNo = 0x00001234;
- ogg_stream_init( &os, kOggStreamSerialNo );
- int doneBytes = 0;
- int donePackets = 0;
- // always skip the first marker
- int doneMarkers = 1;
- // convert raw vorbis packets to ogg stream
- while( doneBytes < hdr.vorbisPacketsSizeBytes )
- {
- // packet size comes first
- short packetSizeBytes;
- fread( &packetSizeBytes, 1, sizeof(packetSizeBytes), fSrc );
- // followed by packet data
- unsigned char *pPacketData = new unsigned char[ packetSizeBytes ];
- fread( pPacketData, 1, packetSizeBytes, fSrc );
- // set up an ogg packet with the data we just read
- ogg_packet op;
- op.packet = pPacketData;
- op.bytes = packetSizeBytes;
- // granule pos mostly maps 1:1 with markers but there are a few special cases for the first few packets
- if( (donePackets == 0) || (donePackets == 2) )
- {
- op.granulepos = 0;
- }
- else
- {
- op.granulepos = -1;
- StreamMarker *pNextMarker = pMarkers + doneMarkers;
- if( (donePackets >= 4) && (pNextMarker->packetStreamByteOffset == doneBytes) )
- {
- op.granulepos = pNextMarker->pcmSampleOffset;
- ++doneMarkers;
- }
- }
- int packetStreamAdvanceBytes = sizeof(packetSizeBytes) + packetSizeBytes;
- bool lastPacket = (doneBytes + packetStreamAdvanceBytes) >= hdr.vorbisPacketsSizeBytes;
- // these flag values are what come out of libogg when decoding the original files
- op.b_o_s = (donePackets == 0) ? 256 : 0;
- op.e_o_s = (lastPacket) ? 512 : 0;
- op.packetno = donePackets;
- // give packet to ogg stream
- ogg_stream_packetin( &os, &op );
- delete [] pPacketData;
- doneBytes += packetStreamAdvanceBytes;
- ++donePackets;
- // flush ogg stream after first 3 packets (as required by spec) and at end of stream
- if( (donePackets == 3) || lastPacket )
- {
- while( true )
- {
- ogg_page og;
- int result = ogg_stream_flush( &os, &og );
- if( result == 0 ) break;
- fwrite( og.header, 1, og.header_len, fDest );
- fwrite( og.body, 1, og.body_len, fDest );
- }
- }
- }
- // cleanup
- ogg_stream_clear(&os);
- delete [] pMarkers;
- fclose( fDest );
- fclose( fSrc );
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement