79 lines
2.2 KiB
C++
79 lines
2.2 KiB
C++
// Copyright (c) 2023 Dominic Masters
|
|
//
|
|
// This software is released under the MIT License.
|
|
// https://opensource.org/licenses/MIT
|
|
|
|
#include "AudioAsset.hpp"
|
|
|
|
using namespace Dawn;
|
|
|
|
AudioAsset::AudioAsset(AssetManager *man, std::string name) :
|
|
Asset(man, name),
|
|
loader(name + ".audio")
|
|
{
|
|
|
|
}
|
|
|
|
void AudioAsset::updateSync() {
|
|
if(this->state != AUDIO_ASSET_LOAD_STATE_DATA_READ) return;
|
|
|
|
// THIS WILL DEFINITELY CHANGE
|
|
this->state = AUDIO_ASSET_LOAD_STATE_COMPLETE;
|
|
this->loaded = true;
|
|
}
|
|
|
|
void AudioAsset::updateAsync() {
|
|
if(this->state != AUDIO_ASSET_LOAD_STATE_INITIAL) return;
|
|
char *start, *end;
|
|
char buffer[1024];
|
|
|
|
// Open asset for reading
|
|
this->state = AUDIO_ASSET_LOAD_STATE_OPENING;
|
|
this->loader.open();
|
|
|
|
// Parse header data.
|
|
this->state = AUDIO_ASSET_LOAD_STATE_READING_HEADER;
|
|
this->loader.read((uint8_t *)buffer, 1024);
|
|
|
|
// Channel count
|
|
this->state = AUDIO_ASSET_LOAD_STATE_READING_CHANNEL_COUNT;
|
|
start = buffer;
|
|
end = strchr(start, '|');
|
|
*end = '\0';
|
|
this->channelCount = atoi(start);
|
|
assertTrue(this->channelCount > 0, "Channel count must be greater than 0");
|
|
|
|
// Sample Rate
|
|
this->state = AUDIO_ASSET_LOAD_STATE_READING_SAMPLE_RATE;
|
|
start = end + 1;
|
|
end = strchr(start, '|');
|
|
*end = '\0';
|
|
this->sampleRate = (uint32_t)strtoul(start, NULL, 10);
|
|
assertTrue(this->sampleRate > 0, "Sample rate must be greater than 0");
|
|
|
|
// Number of samples per channel
|
|
this->state = AUDIO_ASSET_LOAD_STATE_READING_SAMPLES_PER_CHANNEL;
|
|
start = end + 1;
|
|
end = strchr(start, '|');
|
|
*end = '\0';
|
|
this->samplesPerChannel = atoi(start);
|
|
assertTrue(
|
|
this->samplesPerChannel > 0,
|
|
"Samples per channel must be greater than 0"
|
|
);
|
|
|
|
// Total Data Length
|
|
this->state = AUDIO_ASSET_LOAD_STATE_READING_DATA_LENGTH;
|
|
start = end + 1;
|
|
end = strchr(start, '|');
|
|
*end = '\0';
|
|
this->bufferSize = (size_t)atoll(start);
|
|
assertTrue(this->bufferSize > 0, "Buffer size must be greater than 0");
|
|
|
|
// Determine frame size
|
|
this->frameSize = sizeof(int16_t) * this->channelCount;
|
|
|
|
// Indicated start of data
|
|
this->state = AUDIO_ASSET_LOAD_STATE_DATA_READ;
|
|
this->bufferStart = end - buffer;
|
|
} |