Skip to content

Commit

Permalink
Audio Stream Fix - Load from Memory (#26)
Browse files Browse the repository at this point in the history
Didn't load audio streams from memory on exported projects.
  • Loading branch information
Chukobyte authored Jan 8, 2022
1 parent 8e7ed3e commit 560a119
Show file tree
Hide file tree
Showing 8 changed files with 138 additions and 13 deletions.
2 changes: 1 addition & 1 deletion _version.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
"version": "0.45.0"
"version": "0.46.0"
}
4 changes: 4 additions & 0 deletions assets/game_projects/2d_test/project.scfg
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
"width": 800,
"height": 600
},
"window_size": {
"width": 800,
"height": 600
},
"colliders_visible": true,
"pixel_snap": true,
"target_fps": 66,
Expand Down
4 changes: 4 additions & 0 deletions assets/game_projects/3d_test/project.scfg
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
"width": 800,
"height": 600
},
"window_size": {
"width": 800,
"height": 600
},
"colliders_visible": false,
"target_fps": 66,
"background_color": {
Expand Down
4 changes: 4 additions & 0 deletions assets/game_projects/input_test/project.scfg
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
"width": 800,
"height": 600
},
"window_size": {
"width": 800,
"height": 600
},
"colliders_visible": false,
"target_fps": 66,
"background_color": {
Expand Down
4 changes: 4 additions & 0 deletions project.scfg
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
"width": 800,
"height": 600
},
"window_size": {
"width": 800,
"height": 600
},
"colliders_visible": false,
"target_fps": 66,
"background_color": {
Expand Down
11 changes: 10 additions & 1 deletion src/core/asset_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -130,8 +130,17 @@ std::map<std::string, Mix_Chunk*> AssetManager::GetAllSounds() {
return sounds;
}

// TODO: Has to load wav from memory
void AssetManager::LoadAudioStream(const std::string& audioStreamId, const std::string& audioStreamPath, float pitch, float gain, bool loops) {
AudioStream* audioStream = AudioStreamHelper::LoadWav(audioStreamPath, pitch, gain, loops);
AudioStream* audioStream = nullptr;
if (projectProperties->IsAssetsInMemory()) {
logger->Debug("Loading audio stream '" + audioStreamId + "' into memory!");
Archive audioStreamArchive = archiveLoader->Load(audioStreamPath);
audioStream = AudioStreamHelper::LoadWavFromMemory(audioStreamArchive.fileBuffer, audioStreamArchive.fileBufferSize, pitch, gain, loops);
} else {
logger->Debug("Loading audio stream '" + audioStreamId + "' from local!");
audioStream = AudioStreamHelper::LoadWav(audioStreamPath, pitch, gain, loops);
}
audioStreams.emplace(audioStreamId, audioStream);
}

Expand Down
108 changes: 97 additions & 11 deletions src/core/audio/audio_stream_helper.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "audio_stream_helper.h"
#include "../utils/logger.h"

#include <cstring>
#include <fstream>
Expand Down Expand Up @@ -141,29 +142,114 @@ bool AudioStreamHelper::LoadWavFileHeader(std::ifstream& file, AudioStream* audi
return true;
}

// TODO: implement...
bool AudioStreamHelper::LoadWavFileHeaderFromMemory(unsigned char* fileBuffer, size_t fileBufferSize, AudioStream* audioFileData) {
char buffer[4];
unsigned int index = 0;

// the RIFF
READ_CHAR_ARRAY4(buffer, fileBuffer, index)
if(std::strncmp(buffer, "RIFF", 4) != 0) {
std::cerr << "ERROR: file is not a valid WAVE file (header doesn't begin with RIFF)" << std::endl;
return false;
}

// the size of the file
READ_CHAR_ARRAY4(buffer, fileBuffer, index)
audioFileData->fileSize = ConvertToInt(buffer, 4);

// the WAVE
READ_CHAR_ARRAY4(buffer, fileBuffer, index)
if(std::strncmp(buffer, "WAVE", 4) != 0) {
std::cerr << "ERROR: file is not a valid WAVE file (header doesn't contain WAVE)" << std::endl;
return false;
}

// "fmt/0"
READ_CHAR_ARRAY4(buffer, fileBuffer, index)

// this is always 16, the size of the fmt data chunk
READ_CHAR_ARRAY4(buffer, fileBuffer, index)

// PCM should be 1?
READ_CHAR_ARRAY2(buffer, fileBuffer, index)

// the number of channels
READ_CHAR_ARRAY2(buffer, fileBuffer, index)
audioFileData->channels = ConvertToInt(buffer, 2);

// sample rate
READ_CHAR_ARRAY4(buffer, fileBuffer, index)
audioFileData->sampleRate = ConvertToInt(buffer, 4);

// byte rate
READ_CHAR_ARRAY4(buffer, fileBuffer, index)

// block align
READ_CHAR_ARRAY2(buffer, fileBuffer, index)

// bitsPerSample
READ_CHAR_ARRAY2(buffer, fileBuffer, index)
audioFileData->bitsPerSample = ConvertToInt(buffer, 2);

// sub chunk id
READ_CHAR_ARRAY4(buffer, fileBuffer, index)
if(std::strncmp(buffer, "data", 4) != 0) {
std::cerr << "ERROR: file is not a valid WAVE file (doesn't have 'data' tag)" << std::endl;
return false;
}

// size of data
READ_CHAR_ARRAY4(buffer, fileBuffer, index)
audioFileData->dataSize = ConvertToInt(buffer, 4);

return true;
}

AudioStream* AudioStreamHelper::LoadWav(const std::string& filename, float pitch, float gain, bool loops) {
AudioStream *audioFileData = new AudioStream(pitch, gain, loops);
AudioStream *audioStream = new AudioStream(pitch, gain, loops);
std::ifstream fileStream(filename, std::ios::binary);
if(!fileStream.is_open()) {
std::cerr << "ERROR: Could not open \"" << filename << "\"" << std::endl;
return audioFileData;
return audioStream;
}

if(!LoadWavFileHeader(fileStream, audioFileData)) {
if(!LoadWavFileHeader(fileStream, audioStream)) {
std::cerr << "ERROR: Could not load wav header of \"" << filename << "\"" << std::endl;
return audioFileData;
return audioStream;
}

fileStream.clear();
fileStream.seekg(0, std::ios::beg);
audioFileData->data = {std::istreambuf_iterator<char>(fileStream), std::istreambuf_iterator<char>()};
audioStream->data = {std::istreambuf_iterator<char>(fileStream), std::istreambuf_iterator<char>()};
// TODO: Fix/clean up wave file loading
audioFileData->data.erase(
audioFileData->data.begin(),
audioFileData->data.begin() + (audioFileData->data.size() - audioFileData->dataSize)
audioStream->data.erase(
audioStream->data.begin(),
audioStream->data.begin() + (audioStream->data.size() - audioStream->dataSize)
);
audioStream->data.resize(audioStream->dataSize);
audioStream->Initialize();

return audioStream;
}

AudioStream* AudioStreamHelper::LoadWavFromMemory(void* fileBuffer, size_t fileBufferSize, float pitch, float gain, bool loops) {
AudioStream *audioStream = new AudioStream(pitch, gain, loops);
static Logger *logger = Logger::GetInstance();

if (!LoadWavFileHeaderFromMemory((unsigned char*) fileBuffer, fileBufferSize, audioStream)) {
std::cerr << "Error loading wave from memory!" << std::endl;
}

char* audioStreamData = static_cast<char*>(fileBuffer);
audioStream->data = std::vector<char>(audioStreamData, audioStreamData + audioStream->dataSize);
logger->Debug("Loaded trimming data...");
audioStream->data.erase(
audioStream->data.begin(),
audioStream->data.begin() + (audioStream->data.size() - audioStream->dataSize)
);
audioFileData->data.resize(audioFileData->dataSize);
audioFileData->Initialize();
audioStream->data.resize(audioStream->dataSize);
audioStream->Initialize();

return audioFileData;
return audioStream;
}
14 changes: 14 additions & 0 deletions src/core/audio/audio_stream_helper.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,25 @@

#include "audio_stream.h"

#define READ_CHAR_ARRAY2(Buffer, InArray, Index) \
Buffer[0] = InArray[Index]; \
Buffer[1] = InArray[Index+1]; \
Index += 2; \

#define READ_CHAR_ARRAY4(Buffer, InArray, Index) \
Buffer[0] = InArray[Index]; \
Buffer[1] = InArray[Index+1]; \
Buffer[2] = InArray[Index+2]; \
Buffer[3] = InArray[Index+3]; \
Index += 4; \

class AudioStreamHelper {
private:
static std::int32_t ConvertToInt(char* buffer, std::size_t len);
static bool LoadWavFileHeader(std::ifstream& file, AudioStream* audioFileData);
static bool LoadWavFileHeaderFromMemory(unsigned char* fileBuffer, size_t fileBufferSize, AudioStream* audioFileData);

public:
static AudioStream* LoadWav(const std::string& filename, float pitch = 1.0f, float gain = 1.0f, bool loops = false);
static AudioStream* LoadWavFromMemory(void* fileBuffer, size_t fileBufferSize, float pitch = 1.0f, float gain = 1.0f, bool loops = false);
};

0 comments on commit 560a119

Please sign in to comment.