Skip to content

Commit

Permalink
Merge pull request #864 from naftalimurgor/move-audio-buffer-hpp-to-b…
Browse files Browse the repository at this point in the history
…ase#816

Move `src/data/audio_buffer.hpp` to `src/base`
  • Loading branch information
lethal-guitar authored Jun 13, 2022
2 parents 4ad16a2 + e0d03ec commit 2dc22c5
Show file tree
Hide file tree
Showing 8 changed files with 27 additions and 27 deletions.
2 changes: 1 addition & 1 deletion src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ set(core_sources
audio/sound_system.hpp
base/array_view.cpp
base/array_view.hpp
base/audio_buffer.hpp
base/clock.hpp
base/container_utils.hpp
base/defer.hpp
Expand All @@ -52,7 +53,6 @@ set(core_sources
base/string_utils.hpp
base/warnings.hpp
data/actor_ids.hpp
data/audio_buffer.hpp
data/bonus.hpp
data/duke_script.hpp
data/game_options.cpp
Expand Down
4 changes: 2 additions & 2 deletions src/assets/resource_loader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -440,7 +440,7 @@ bool ResourceLoader::hasSoundBlasterSound(const data::SoundId id) const
}


data::AudioBuffer
base::AudioBuffer
ResourceLoader::loadSoundBlasterSound(const data::SoundId id) const
{
const auto digitizedSoundFileName = digitizedSoundFilenameForId(id);
Expand Down Expand Up @@ -493,7 +493,7 @@ std::vector<std::filesystem::path>
}


data::AudioBuffer ResourceLoader::loadSound(std::string_view name) const
base::AudioBuffer ResourceLoader::loadSound(std::string_view name) const
{
return assets::decodeVoc(file(name));
}
Expand Down
6 changes: 3 additions & 3 deletions src/assets/resource_loader.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@
#include "assets/duke_script_loader.hpp"
#include "assets/palette.hpp"
#include "base/array_view.hpp"
#include "base/audio_buffer.hpp"
#include "base/image.hpp"
#include "data/audio_buffer.hpp"
#include "data/movie.hpp"
#include "data/song.hpp"
#include "data/sound_ids.hpp"
Expand Down Expand Up @@ -102,7 +102,7 @@ class ResourceLoader

data::Song loadMusic(std::string_view name) const;
bool hasSoundBlasterSound(data::SoundId id) const;
data::AudioBuffer loadSoundBlasterSound(data::SoundId id) const;
base::AudioBuffer loadSoundBlasterSound(data::SoundId id) const;

std::vector<std::filesystem::path>
replacementSoundPaths(data::SoundId id) const;
Expand Down Expand Up @@ -133,7 +133,7 @@ class ResourceLoader
data::Image loadTiledFullscreenImage(
std::string_view name,
const data::Palette16& overridePalette) const;
data::AudioBuffer loadSound(std::string_view name) const;
base::AudioBuffer loadSound(std::string_view name) const;

std::filesystem::path mGamePath;
std::vector<std::filesystem::path> mModPaths;
Expand Down
4 changes: 2 additions & 2 deletions src/assets/voc_decoder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -375,15 +375,15 @@ void decodeAudio(
} // namespace


data::AudioBuffer decodeVoc(const ByteBuffer& data)
base::AudioBuffer decodeVoc(const ByteBuffer& data)
{
LeStreamReader reader(data);
if (!readAndValidateVocHeader(reader))
{
throw std::invalid_argument("Invalid VOC file header");
}

std::vector<data::Sample> decodedSamples;
std::vector<base::Sample> decodedSamples;
std::optional<int> sampleRate;

while (reader.hasData())
Expand Down
4 changes: 2 additions & 2 deletions src/assets/voc_decoder.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@
#pragma once

#include "assets/byte_buffer.hpp"
#include "data/audio_buffer.hpp"
#include "base/audio_buffer.hpp"


namespace rigel::assets
{

data::AudioBuffer decodeVoc(const ByteBuffer& data);
base::AudioBuffer decodeVoc(const ByteBuffer& data);

}
28 changes: 14 additions & 14 deletions src/audio/sound_system.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,8 @@ const auto COMBINED_SOUNDS_ADLIB_PERCENTAGE = 0.30f;
const auto DESIRED_SAMPLE_RATE = 44100;
const auto BUFFER_SIZE = 2048;

data::AudioBuffer
resampleAudio(const data::AudioBuffer& buffer, const int newSampleRate)
base::AudioBuffer
resampleAudio(const base::AudioBuffer& buffer, const int newSampleRate)
{
using ResamplerPtr =
std::unique_ptr<SpeexResamplerState, decltype(&speex_resampler_destroy)>;
Expand All @@ -82,7 +82,7 @@ data::AudioBuffer
base::integerDivCeil<spx_uint32_t>(inputLength, buffer.mSampleRate) *
newSampleRate);

std::vector<data::Sample> resampled(outputLength);
std::vector<base::Sample> resampled(outputLength);
speex_resampler_process_int(
pResampler.get(),
0,
Expand All @@ -95,7 +95,7 @@ data::AudioBuffer
}


void appendRampToZero(data::AudioBuffer& buffer, const int sampleRate)
void appendRampToZero(base::AudioBuffer& buffer, const int sampleRate)
{
// Roughly 10 ms of linear ramp
const auto rampLength = (sampleRate / 100);
Expand All @@ -107,16 +107,16 @@ void appendRampToZero(data::AudioBuffer& buffer, const int sampleRate)
{
const auto interpolation = i / static_cast<double>(rampLength);
const auto rampedValue = lastSample * (1.0 - interpolation);
buffer.mSamples.push_back(base::roundTo<data::Sample>(rampedValue));
buffer.mSamples.push_back(base::roundTo<base::Sample>(rampedValue));
}
}


// Prepares the given audio buffer to be loaded into a Mix_Chunk. This includes
// resampling to the given sample rate and making sure the buffer ends in a
// zero value to avoid clicks/pops.
data::AudioBuffer
prepareBuffer(const data::AudioBuffer& original, const int sampleRate)
base::AudioBuffer
prepareBuffer(const base::AudioBuffer& original, const int sampleRate)
{
auto buffer = resampleAudio(original, sampleRate);
if (buffer.mSamples.back() != 0)
Expand All @@ -133,7 +133,7 @@ data::AudioBuffer
// Converts the given audio buffer into the given audio format and returns it
// as a raw buffer
RawBuffer convertBuffer(
const data::AudioBuffer& buffer,
const base::AudioBuffer& buffer,
const std::uint16_t audioFormat,
const int numChannels)
{
Expand Down Expand Up @@ -163,8 +163,8 @@ RawBuffer convertBuffer(


void overlaySound(
data::AudioBuffer& base,
const data::AudioBuffer& overlay,
base::AudioBuffer& base,
const base::AudioBuffer& overlay,
const float overlayVolume)
{
assert(base.mSampleRate == overlay.mSampleRate);
Expand All @@ -181,7 +181,7 @@ void overlaySound(
reinterpret_cast<std::uint8_t*>(base.mSamples.data()),
reinterpret_cast<const std::uint8_t*>(overlay.mSamples.data()),
AUDIO_S16LSB,
static_cast<std::uint32_t>(overlay.mSamples.size() * sizeof(data::Sample)),
static_cast<std::uint32_t>(overlay.mSamples.size() * sizeof(base::Sample)),
overlayVolumeSdl);
}

Expand All @@ -191,7 +191,7 @@ auto idToIndex(const data::SoundId id)
return static_cast<int>(id);
}

data::AudioBuffer renderAdlibSound(
base::AudioBuffer renderAdlibSound(
const assets::AdlibSound& sound,
const AdlibEmulator::Type emulatorType)
{
Expand All @@ -215,7 +215,7 @@ data::AudioBuffer renderAdlibSound(
const auto octaveBits = static_cast<uint8_t>((sound.mOctave & 7) << 2);

const auto samplesPerTick = OPL2_SAMPLE_RATE / ADLIB_SOUND_RATE;
std::vector<data::Sample> renderedSamples;
std::vector<base::Sample> renderedSamples;
renderedSamples.reserve(sound.mSoundData.size() * samplesPerTick);

for (const auto byte : sound.mSoundData)
Expand All @@ -237,7 +237,7 @@ data::AudioBuffer renderAdlibSound(
}


data::AudioBuffer loadSoundForStyle(
base::AudioBuffer loadSoundForStyle(
const data::SoundId id,
const data::SoundStyle soundStyle,
const int sampleRate,
Expand Down
2 changes: 1 addition & 1 deletion src/audio/sound_system.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@

#pragma once

#include "base/audio_buffer.hpp"
#include "base/defer.hpp"
#include "data/audio_buffer.hpp"
#include "data/game_options.hpp"
#include "data/song.hpp"
#include "data/sound_ids.hpp"
Expand Down
4 changes: 2 additions & 2 deletions src/data/audio_buffer.hpp → src/base/audio_buffer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
#include <vector>


namespace rigel::data
namespace rigel::base
{

using Sample = std::int16_t;
Expand All @@ -33,4 +33,4 @@ struct AudioBuffer
};


} // namespace rigel::data
} // namespace rigel::base

0 comments on commit 2dc22c5

Please sign in to comment.