Advertisement
Guest User

Untitled

a guest
Nov 25th, 2022
37
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.71 KB | None | 0 0
  1. struct WavSample {
  2.   unsigned int Channels;
  3.   unsigned int BytesPerSample;
  4.   unsigned int DataSize;
  5.   void *sampleData;
  6. };
  7.  
  8. void SoundProcessor::parseWavFile(std::ifstream &rawWavFile) {
  9.   auto riff = ChunkInfo{};
  10.   auto wave= ChunkInfo{};
  11.   auto chunk= ChunkInfo{};
  12.  
  13.   read(rawWavFile, riff, nullptr);
  14.   if(riff.chunkId != CT_RIFF){
  15.     throw std::runtime_error("First chunk must be RIFF");
  16.   }
  17.   std::cout << "RIFF Size: " << riff.chunkSize << std::endl;
  18.  
  19.   read(rawWavFile, &wave.chunkId, uint32Size, &riff);
  20.   wave.chunkSize = riff.chunkSize - uint32Size;
  21.  
  22.  
  23.   if(wave.chunkId != CT_WAVE){
  24.     throw std::runtime_error("File is not a WAV file");
  25.   }
  26.   std::cout << "RIFF Type: " << convert4ByteUIntToString(wave.chunkId) << std::endl;
  27.  
  28.   while (rawWavFile) {
  29.     read(rawWavFile, chunk, &riff);
  30.     if(rawWavFile.eof()) break;
  31.     uint32_t next_chunk = static_cast<size_t>(rawWavFile.tellg()) + chunk.chunkSize + (chunk.chunkSize & 1);
  32.  
  33.     //std::cout << "Chunk: '" << convert4ByteUIntToString(chunk.chunkId) << "', Size: " << chunk.chunkSize << std::endl;
  34.  
  35.     if(chunk.chunkId == CT_fmt){
  36.       auto fmtChunk = WavFmtHeader{};
  37.       rawWavFile.read(reinterpret_cast<char *>(&fmtChunk), sizeof(fmtChunk));
  38.       std::cout << convert4ByteUIntToString(chunk.chunkId) << std::endl;
  39.     }
  40.  
  41.     if(chunk.chunkId == CT_data){
  42.       auto dataChunk = WavSample{};
  43.       rawWavFile.read(reinterpret_cast<char *>(&dataChunk), sizeof(dataChunk));
  44.       std::cout << convert4ByteUIntToString(chunk.chunkId) << std::endl;
  45.       std::cout << "Channels num: " << dataChunk.Channels << std::endl;
  46.     }
  47.  
  48.     if(rawWavFile.eof()) break;
  49.     rawWavFile.seekg(next_chunk);
  50.   }
  51.   std::cout << "asd";
  52. }
  53.  
  54.  
  55.  
  56.  
  57.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement