Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/engine/audio/ALObjects.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ namespace AL {
ALuint format = Format(audioData.byteDepth, audioData.numberOfChannels);

CHECK_AL_ERROR();
alBufferData(alHandle, format, audioData.rawSamples.get(), audioData.size,
alBufferData(alHandle, format, audioData.rawSamples.data(), audioData.rawSamples.size(),
audioData.sampleRate);

return ClearALError();
Expand Down
9 changes: 6 additions & 3 deletions src/engine/audio/Audio.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -383,8 +383,10 @@ namespace Audio {

streams[streamNum]->SetGain(volume);

AudioData audioData(rate, width, channels, (width * numSamples * channels),
reinterpret_cast<const char*>(data));
AudioData audioData { rate, width, channels };
audioData.rawSamples.resize( width * numSamples * channels );
memcpy( audioData.rawSamples.data(), data, width * numSamples * channels * sizeof( char ) );

AL::Buffer buffer;

int feedError = buffer.Feed(audioData);
Expand Down Expand Up @@ -506,9 +508,10 @@ namespace Audio {
int numSamples = AvailableCaptureSamples();

if (numSamples > 0) {
uint16_t* buffer = new uint16_t[numSamples];
uint16_t* buffer = ( uint16_t* ) Hunk_AllocateTempMemory( numSamples * sizeof( uint16_t ) );
GetCapturedData(numSamples, buffer);
StreamData(N_STREAMS - 1, buffer, numSamples, 16000, 2, 1, 1.0, -1);
Hunk_FreeTempMemory( buffer );
}
}

Expand Down
11 changes: 3 additions & 8 deletions src/engine/audio/AudioData.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#ifndef AUDIO_DATA_H
#define AUDIO_DATA_H
#include <memory>
#include <vector>

namespace Audio {

Expand All @@ -39,23 +40,18 @@ struct AudioData {
: sampleRate{0}
, byteDepth{0}
, numberOfChannels{0}
, size{0}
, rawSamples{nullptr}
{}

AudioData(int sampleRate, int byteDepth, int numberOfChannels, int size, const char* rawSamples)
AudioData(int sampleRate, int byteDepth, int numberOfChannels )
: sampleRate{sampleRate}
, byteDepth{byteDepth}
, numberOfChannels{numberOfChannels}
, size{size}
, rawSamples{rawSamples}
{}

AudioData(AudioData&& that)
: sampleRate{that.sampleRate}
, byteDepth{that.byteDepth}
, numberOfChannels{that.numberOfChannels}
, size{that.size}
, rawSamples{std::move(that.rawSamples)}
{}

Expand All @@ -64,8 +60,7 @@ struct AudioData {
const int sampleRate;
const int byteDepth;
const int numberOfChannels;
const int size;
std::unique_ptr<const char[]> rawSamples;
std::vector<char> rawSamples;
};
} // namespace Audio
#endif
36 changes: 29 additions & 7 deletions src/engine/audio/OggCodec.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -151,20 +151,42 @@ AudioData LoadOggCodec(std::string filename)
int sampleRate = oggInfo->rate;
int numberOfChannels = oggInfo->channels;

char buffer[4096];
/* The largest ogg file we have so far is res-soundtrack/sound/ui/heartbeat.ogg
It uncompress to ~47mb. Use 64mb here just in case */
static constexpr uint32_t MAX_USED_FILE_SIZE = 64 * 1024 * 1024;
static constexpr uint32_t MAX_READ_SIZE = 1 * 1024 * 1024;

char* buffer = ( char* ) Hunk_AllocateTempMemory( MAX_USED_FILE_SIZE );
uint32_t bufferPointer = 0;
uint32_t rawSamplesPointer = 0;

int bytesRead = 0;
int bitStream = 0;

std::vector<char> samples;
AudioData out { sampleRate, sampleWidth, numberOfChannels };

while ((bytesRead = ov_read(vorbisFile.get(), buffer + bufferPointer, MAX_READ_SIZE, 0, sampleWidth, 1, &bitStream)) > 0) {
bufferPointer += bytesRead;

if ( MAX_USED_FILE_SIZE - bufferPointer < MAX_READ_SIZE ) {
out.rawSamples.resize( out.rawSamples.size() + bufferPointer );
std::copy_n( buffer, bufferPointer, out.rawSamples.data() + rawSamplesPointer );

while ((bytesRead = ov_read(vorbisFile.get(), buffer, 4096, 0, sampleWidth, 1, &bitStream)) > 0) {
std::copy_n(buffer, bytesRead, std::back_inserter(samples));
rawSamplesPointer += bufferPointer;
bufferPointer = 0;
}
}

if ( bufferPointer ) {
out.rawSamples.resize( out.rawSamples.size() + bufferPointer );
std::copy_n( buffer, bufferPointer, out.rawSamples.data() + rawSamplesPointer );
}

Hunk_FreeTempMemory( buffer );

ov_clear(vorbisFile.get());

char* rawSamples = new char[samples.size()];
std::copy_n(samples.data(), samples.size(), rawSamples);
return AudioData(sampleRate, sampleWidth, numberOfChannels, samples.size(), rawSamples);
return out;
}

} //namespace Audio
42 changes: 30 additions & 12 deletions src/engine/audio/OpusCodec.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -145,25 +145,43 @@ AudioData LoadOpusCodec(std::string filename)
int sampleRate = 48000;
int numberOfChannels = opusInfo->channel_count;

// The buffer is big enough to hold 120ms worth of samples per channel
opus_int16* buffer = new opus_int16[numberOfChannels * 5760];
/* The largest opus file we have so far is sound/thunder/weather.opus
It uncompress to ~13mb. Use 64mb here just in case */
static constexpr uint32_t MAX_USED_FILE_SIZE = 64 * 1024 * 1024;
static constexpr uint32_t MAX_READ_SIZE = 1 * 1024 * 1024;

char* buffer = ( char* ) Hunk_AllocateTempMemory( MAX_USED_FILE_SIZE );
uint32_t bufferPointer = 0;
uint32_t rawSamplesPointer = 0;

int samplesPerChannelRead = 0;

std::vector<opus_int16> samples;
AudioData out { sampleRate, sampleWidth, numberOfChannels };

while ( ( samplesPerChannelRead =
op_read( opusFile, ( opus_int16* ) ( buffer + bufferPointer ), MAX_READ_SIZE, nullptr )
) > 0 ) {
bufferPointer += samplesPerChannelRead * numberOfChannels * sizeof( opus_int16 );

if ( MAX_USED_FILE_SIZE - bufferPointer < MAX_READ_SIZE ) {
out.rawSamples.resize( out.rawSamples.size() + bufferPointer );
std::copy_n( buffer, bufferPointer, out.rawSamples.data() + rawSamplesPointer );

while ((samplesPerChannelRead =
op_read(opusFile, buffer, sampleWidth * numberOfChannels * 5760, nullptr)) > 0) {
std::copy_n(buffer, samplesPerChannelRead * numberOfChannels, std::back_inserter(samples));
rawSamplesPointer += bufferPointer;
bufferPointer = 0;
}
}

delete[] buffer;
op_free(opusFile);
if ( bufferPointer ) {
out.rawSamples.resize( out.rawSamples.size() + bufferPointer );
std::copy_n( buffer, bufferPointer, out.rawSamples.data() + rawSamplesPointer );
}

char* rawSamples = new char[sampleWidth * samples.size()];
std::copy_n(reinterpret_cast<char*>(samples.data()), sampleWidth * samples.size(), rawSamples);
Hunk_FreeTempMemory( buffer );

op_free(opusFile);

return AudioData(sampleRate, sampleWidth, numberOfChannels, samples.size() * sampleWidth,
rawSamples);
return out;
}

} //namespace Audio
4 changes: 2 additions & 2 deletions src/engine/audio/Sample.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,9 @@ namespace Audio {

bool Sample::Load() {
audioLogs.Debug("Loading Sample '%s'", GetName());
auto audioData = LoadSoundCodec(GetName());
AudioData audioData = LoadSoundCodec(GetName());

if (audioData.size == 0) {
if ( !audioData.rawSamples.size() ) {
audioLogs.Debug("Couldn't load sound %s, it's empty!", GetName());
return false;
}
Expand Down
9 changes: 4 additions & 5 deletions src/engine/audio/WavCodec.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -108,16 +108,15 @@ AudioData LoadWavCodec(std::string filename)

int size = PackChars(audioFile, dataOffset + 4, 4);

if (size <= 0 || sampleRate <=0 ){
if (size <= 0 || sampleRate <= 0 ) {
audioLogs.Warn("Error in reading %s.", filename);
return AudioData();
}

char* data = new char[size];
AudioData out { sampleRate, byteDepth, numChannels };
out.rawSamples.assign( audioFile.data() + dataOffset + 8, audioFile.data() + size );

std::copy_n(audioFile.data() + dataOffset + 8, size, data);

return AudioData(sampleRate, byteDepth, numChannels, size, data);
return out;
}

} // namespace Audio