Skip to content

Commit

Permalink
Add wav error message string function to dsp::wav (#42)
Browse files Browse the repository at this point in the history
* Add dsp::wav::GetMsgForLoadReturnCode()

* Add missing includes for wav.h
  • Loading branch information
olilarkin authored May 7, 2023
1 parent 2aafede commit 2dd049e
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 0 deletions.
37 changes: 37 additions & 0 deletions dsp/wav.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include <fstream>
#include <iostream>
#include <string>
#include <sstream>
#include <unordered_set>
#include <vector>

Expand Down Expand Up @@ -37,6 +38,42 @@ bool ReadChunkAndSkipJunk(std::ifstream& file, char* chunkID)
return file.good();
}

std::string dsp::wav::GetMsgForLoadReturnCode(LoadReturnCode retCode)
{
std::stringstream message;

switch (retCode)
{
case (LoadReturnCode::ERROR_OPENING):
message << "Failed to open file (is it being used by another "
"program?)";
break;
case (LoadReturnCode::ERROR_NOT_RIFF): message << "File is not a WAV file."; break;
case (LoadReturnCode::ERROR_NOT_WAVE): message << "File is not a WAV file."; break;
case (LoadReturnCode::ERROR_MISSING_FMT):
message << "File is missing expected format chunk.";
break;
case (LoadReturnCode::ERROR_INVALID_FILE): message << "WAV file contents are invalid."; break;
case (LoadReturnCode::ERROR_UNSUPPORTED_FORMAT_ALAW):
message << "Unsupported file format \"A-law\"";
break;
case (LoadReturnCode::ERROR_UNSUPPORTED_FORMAT_MULAW):
message << "Unsupported file format \"mu-law\"";
break;
case (LoadReturnCode::ERROR_UNSUPPORTED_FORMAT_EXTENSIBLE):
message << "Unsupported file format \"extensible\"";
break;
case (LoadReturnCode::ERROR_NOT_MONO): message << "File is not mono."; break;
case (LoadReturnCode::ERROR_UNSUPPORTED_BITS_PER_SAMPLE):
message << "Unsupported bits per sample";
break;
case (dsp::wav::LoadReturnCode::ERROR_OTHER): message << "???"; break;
default: message << "???"; break;
}

return message.str();
}

dsp::wav::LoadReturnCode dsp::wav::Load(const char* fileName, std::vector<float>& audio, double& sampleRate)
{
// FYI: https://www.mmsp.ece.mcgill.ca/Documents/AudioFormats/WAVE/WAVE.html
Expand Down
8 changes: 8 additions & 0 deletions dsp/wav.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@

#pragma once

#include <fstream>
#include <string>
#include <vector>

namespace dsp
{
namespace wav
Expand All @@ -26,6 +30,10 @@ enum class LoadReturnCode
ERROR_NOT_MONO,
ERROR_OTHER
};

// Get a string describing the error
std::string GetMsgForLoadReturnCode(LoadReturnCode rc);

// Load a WAV file into a provided array of doubles,
// And note the sample rate.
//
Expand Down

0 comments on commit 2dd049e

Please sign in to comment.