MCPcopy Create free account
hub / github.com/adamstark/AudioFile / decodeWaveFile

Method decodeWaveFile

AudioFile.h:549–704  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

547//=============================================================
548template <class T>
549bool AudioFile<T>::decodeWaveFile (const std::vector<uint8_t>& fileData)
550{
551 // -----------------------------------------------------------
552 // HEADER CHUNK
553 std::string headerChunkID (fileData.begin(), fileData.begin() + 4);
554 //int32_t fileSizeInBytes = fourBytesToInt (fileData, 4) + 8;
555 std::string format (fileData.begin() + 8, fileData.begin() + 12);
556
557 // -----------------------------------------------------------
558 // try and find the start points of key chunks
559 int indexOfDataChunk = getIndexOfChunk (fileData, "data", 12);
560 int indexOfFormatChunk = getIndexOfChunk (fileData, "fmt ", 12);
561 int indexOfXMLChunk = getIndexOfChunk (fileData, "iXML", 12);
562
563 // if we can't find the data or format chunks, or the IDs/formats don't seem to be as expected
564 // then it is unlikely we'll able to read this file, so abort
565 if (indexOfDataChunk == -1 || indexOfFormatChunk == -1 || headerChunkID != "RIFF" || format != "WAVE")
566 {
567 reportError ("ERROR: this doesn't seem to be a valid .WAV file");
568 return false;
569 }
570
571 // -----------------------------------------------------------
572 // FORMAT CHUNK
573 int f = indexOfFormatChunk;
574 std::string formatChunkID (fileData.begin() + f, fileData.begin() + f + 4);
575 //int32_t formatChunkSize = fourBytesToInt (fileData, f + 4);
576 uint16_t audioFormat = twoBytesToInt (fileData, f + 8);
577 uint16_t numChannels = twoBytesToInt (fileData, f + 10);
578 sampleRate = static_cast<uint32_t> (fourBytesToInt (fileData, f + 12));
579 uint32_t numBytesPerSecond = fourBytesToInt (fileData, f + 16);
580 uint16_t numBytesPerBlock = twoBytesToInt (fileData, f + 20);
581 bitDepth = static_cast<int> (twoBytesToInt (fileData, f + 22));
582
583 if (bitDepth > sizeof (T) * 8)
584 {
585 std::string message = "ERROR: you are trying to read a ";
586 message += std::to_string (bitDepth);
587 message += "-bit file using a ";
588 message += std::to_string (sizeof (T) * 8);
589 message += "-bit sample type";
590 reportError (message);
591 return false;
592 }
593
594 uint16_t numBytesPerSample = static_cast<uint16_t> (bitDepth) / 8;
595
596 // check that the audio format is PCM or Float or extensible
597 if (audioFormat != WavAudioFormat::PCM && audioFormat != WavAudioFormat::IEEEFloat && audioFormat != WavAudioFormat::Extensible)
598 {
599 reportError ("ERROR: this .WAV file is encoded in a format that this library does not support at present");
600 return false;
601 }
602
603 // check the number of channels is mono or stereo
604 if (numChannels < 1 || numChannels > 128)
605 {
606 reportError ("ERROR: this WAV file seems to be an invalid number of channels (or corrupted?)");

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected