Implementing sample rate and stream mode considerations into the API

This commit is contained in:
2024-08-29 19:54:00 -04:00
parent bdbb5aff13
commit b5ce2070e3
3 changed files with 52 additions and 16 deletions

View File

@@ -6,10 +6,13 @@ namespace ReMixer {
RECORD,
};
/// We do not plan to support specifications such as 7.1 Surround.
/// 3D Positional Audio will be implemented later in software.
enum class StreamMode : bool {
MONO = false,
STEREO = true
};
unsigned operator ""_Hz(unsigned long long int frequency);
unsigned operator ""_kHz(long double frequency);

View File

@@ -13,18 +13,18 @@ namespace ReMixer
class Sound {
private:
std::vector<char> audio_data;
//SampleRate sample_rate;
StreamMode num_channels;
uint sample_rate;
StreamMode stream_mode;
public:
static Sound FromOGGVorbisFile(const std::filesystem::path& file_name);
static Sound FromPCMFile(const std::filesystem::path& file_name);
static Sound FromPCMBuffer(std::vector<char> buffer);
static Sound FromPCMFile(const std::filesystem::path& file_name, uint sampleRate = 44.1_kHz, StreamMode streamMode = StreamMode::STEREO);
static Sound FromPCMBuffer(std::vector<char> buffer, uint sampleRate = 44.1_kHz, StreamMode streamMode = StreamMode::STEREO);
void LoadOGGVorbisFile(const std::filesystem::path& file_name);
void LoadPCMFile(const std::filesystem::path& file_name);
void LoadPCMBuffer(std::vector<char> buffer);
void LoadPCMFile(const std::filesystem::path& file_name, uint sampleRate = 44.1_kHz, StreamMode streamMode = StreamMode::STEREO);
void LoadPCMBuffer(std::vector<char> buffer, uint sampleRate = 44.1_kHz, StreamMode streamMode = StreamMode::STEREO);
unsigned int DataSize() const { return audio_data.size();};
char* ptr() { return audio_data.data();}
@@ -32,12 +32,12 @@ namespace ReMixer
Sound() = default;
};
/// Class that holds a short sound that can fit in memory and requires no latency. (i.e. footsteps or gun shots).
/// Class that holds a short sound that can fit in memory and requires no latency. (i.e. footsteps or gun shots).
class SFX {
};
/// Music class is for longer sounds that should be streamed in.
/// Music class is for longer sounds that should be streamed in.
class Music {
};

View File

@@ -2,8 +2,7 @@
#include <vorbis/codec.h>
#include <vorbis/vorbisfile.h>
#include <fstream>
#include <format>
ReMixer::Sound ReMixer::Sound::FromOGGVorbisFile(const std::filesystem::path &file_name) {
@@ -12,19 +11,21 @@ ReMixer::Sound ReMixer::Sound::FromOGGVorbisFile(const std::filesystem::path &fi
return sound;
}
ReMixer::Sound ReMixer::Sound::FromPCMFile(const std::filesystem::path &file_name) {
ReMixer::Sound ReMixer::Sound::FromPCMFile(const std::filesystem::path &file_name, uint sample_rate, StreamMode stream_mode) {
Sound sound;
sound.LoadPCMFile(file_name);
sound.LoadPCMFile(file_name, sample_rate, stream_mode);
return sound;
}
ReMixer::Sound ReMixer::Sound::FromPCMBuffer(std::vector<char> buffer) {
ReMixer::Sound ReMixer::Sound::FromPCMBuffer(std::vector<char> buffer, uint sample_rate, StreamMode stream_mode) {
Sound sound;
sound.LoadPCMBuffer(buffer);
sound.LoadPCMBuffer(buffer, sample_rate, stream_mode);
return sound;
}
void ReMixer::Sound::LoadPCMFile(const std::filesystem::path &file_name) {
void ReMixer::Sound::LoadPCMFile(const std::filesystem::path &file_name, uint sampleRate, StreamMode streamMode) {
std::ifstream file(file_name, std::ios::binary | std::ios::ate);
file.unsetf(std::ios::skipws);
@@ -37,10 +38,15 @@ void ReMixer::Sound::LoadPCMFile(const std::filesystem::path &file_name) {
audio_data.insert(audio_data.begin(),
std::istream_iterator<char>(file),
std::istream_iterator<char>());
this->sample_rate = sampleRate;
this->stream_mode = streamMode;
}
void ReMixer::Sound::LoadPCMBuffer(std::vector<char> buffer) {
void ReMixer::Sound::LoadPCMBuffer(std::vector<char> buffer, uint sampleRate, StreamMode streamMode) {
audio_data = buffer;
this->sample_rate = sampleRate;
this->stream_mode = streamMode;
}
void ReMixer::Sound::LoadOGGVorbisFile(const std::filesystem::path &file_name) {
@@ -59,6 +65,33 @@ void ReMixer::Sound::LoadOGGVorbisFile(const std::filesystem::path &file_name) {
vorbis_info* vi = ov_info(&vf, -1);
if (vi->channels > 2)
{
std::cerr << "Warning: Our sound system currently only supports Mono and Stereo. "
<< std::format("However, we detect {} number of channels in this sound file. ", vi->channels)
<< "Information may be lost, but we will do our best to play it back anyway." << std::endl;
stream_mode = StreamMode::STEREO;
} else if (vi->channels == 2) {
stream_mode = StreamMode::STEREO;
} else if (vi->channels == 1) {
stream_mode = StreamMode::MONO;
} else if (vi->channels == 0) {
throw std::runtime_error("We don't know what to do with this file?");
}
sample_rate = vi->rate;
//std::cout << vi->bitrate_nominal << std::endl;
//std::cout << vi->bitrate_lower << std::endl;
//std::cout << vi->bitrate_upper << std::endl;
//std::cout << vi->bitrate_window << std::endl;
//std::cout << vi->rate << std::endl;
//std::cout << vi->version << std::endl;
//std::cout << vi->channels << std::endl;
char pcmout[4096];
int current_section;
long bytes;