Advertisement
Guest User

Untitled

a guest
Dec 12th, 2012
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.26 KB | None | 0 0
  1.  
  2. // code to convert little inferno sound resources to ogg files
  3. // the version of libogg that you use here must be version 1.1.4
  4. // dec 12, 2012 - allan blomquist
  5.  
  6. #define _CRT_SECURE_NO_WARNINGS
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9.  
  10. // MUST use libogg-1.1.4 - not any other version!
  11. #include <ogg/ogg.h>
  12.  
  13. struct SoundHeader
  14. {
  15.     int totalSampleCountPerChannel;
  16.     int oggTotalDataSizeBytes;
  17.     int vorbisMarkersSizeBytes;
  18.     int vorbisWorkingSetSizeBytes;
  19.     int vorbisPacketsSizeBytes;
  20.     int samplesPerSec;
  21.     int channels;
  22. };
  23.  
  24. struct StreamMarker
  25. {
  26.     int packetStreamByteOffset;
  27.     int pcmSampleOffset;
  28.     int packetIdx;
  29. };
  30.  
  31. int main( int argc, const char *argv[] )
  32. {
  33.     FILE *fSrc = fopen( argv[1], "rb" );
  34.     FILE *fDest = fopen( "out.ogg", "wb" );
  35.  
  36.     // read header
  37.     SoundHeader hdr;
  38.     fread( &hdr, 1, sizeof(hdr), fSrc );
  39.  
  40.     // read stream markers
  41.     int numMarkers = hdr.vorbisMarkersSizeBytes / sizeof(StreamMarker);
  42.     StreamMarker *pMarkers = new StreamMarker[ numMarkers ];
  43.     fread( pMarkers, sizeof(StreamMarker), numMarkers, fSrc );
  44.  
  45.     // setup for making an ogg stream - serial number isn't going to matter
  46.     ogg_stream_state os;
  47.     const int kOggStreamSerialNo = 0x00001234;
  48.     ogg_stream_init( &os, kOggStreamSerialNo );
  49.  
  50.     int doneBytes = 0;
  51.     int donePackets = 0;
  52.  
  53.     // always skip the first marker
  54.     int doneMarkers = 1;
  55.  
  56.     // convert raw vorbis packets to ogg stream
  57.     while( doneBytes < hdr.vorbisPacketsSizeBytes )
  58.     {
  59.         // packet size comes first
  60.         short packetSizeBytes;
  61.         fread( &packetSizeBytes, 1, sizeof(packetSizeBytes), fSrc );
  62.  
  63.         // followed by packet data
  64.         unsigned char *pPacketData = new unsigned char[ packetSizeBytes ];
  65.         fread( pPacketData, 1, packetSizeBytes, fSrc );
  66.  
  67.         // set up an ogg packet with the data we just read
  68.         ogg_packet op;
  69.         op.packet = pPacketData;
  70.         op.bytes = packetSizeBytes;
  71.  
  72.         // granule pos mostly maps 1:1 with markers but there are a few special cases for the first few packets
  73.         if( (donePackets == 0) || (donePackets == 2) )
  74.         {
  75.             op.granulepos = 0;
  76.         }
  77.         else
  78.         {
  79.             op.granulepos = -1;
  80.  
  81.             StreamMarker *pNextMarker = pMarkers + doneMarkers;
  82.             if( (donePackets >= 4) && (pNextMarker->packetStreamByteOffset == doneBytes) )
  83.             {
  84.                 op.granulepos = pNextMarker->pcmSampleOffset;
  85.                 ++doneMarkers;
  86.             }
  87.         }
  88.  
  89.         int packetStreamAdvanceBytes = sizeof(packetSizeBytes) + packetSizeBytes;
  90.         bool lastPacket = (doneBytes + packetStreamAdvanceBytes) >= hdr.vorbisPacketsSizeBytes;
  91.  
  92.         // these flag values are what come out of libogg when decoding the original files
  93.         op.b_o_s = (donePackets == 0) ? 256 : 0;
  94.         op.e_o_s = (lastPacket) ? 512 : 0;
  95.  
  96.         op.packetno = donePackets;
  97.  
  98.         // give packet to ogg stream
  99.         ogg_stream_packetin( &os, &op );
  100.         delete [] pPacketData;
  101.  
  102.         doneBytes += packetStreamAdvanceBytes;
  103.         ++donePackets;
  104.  
  105.         // flush ogg stream after first 3 packets (as required by spec) and at end of stream
  106.         if( (donePackets == 3) || lastPacket )
  107.         {
  108.             while( true )
  109.             {
  110.                 ogg_page og;
  111.                 int result = ogg_stream_flush( &os, &og );
  112.                 if( result == 0 ) break;
  113.                 fwrite( og.header, 1, og.header_len, fDest );
  114.                 fwrite( og.body, 1, og.body_len, fDest );
  115.             }
  116.         }
  117.     }
  118.  
  119.     // cleanup
  120.     ogg_stream_clear(&os);
  121.  
  122.     delete [] pMarkers;
  123.  
  124.     fclose( fDest );
  125.     fclose( fSrc );
  126.  
  127.     return 0;
  128. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement