Skip to content

Commit

Permalink
Work in progress
Browse files Browse the repository at this point in the history
  • Loading branch information
skhaz committed Sep 29, 2024
1 parent 0607b0e commit 2aa6187
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 34 deletions.
6 changes: 3 additions & 3 deletions src/application.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ application::application(int argc, char **argv) {
UNUSED(argc);
UNUSED(argv);

SDL_Init(SDL_INIT_AUDIO | SDL_INIT_GAMECONTROLLER | SDL_INIT_VIDEO);
SDL_EventState(SDL_CONTROLLERDEVICEADDED, SDL_ENABLE);
SDL_EventState(SDL_CONTROLLERDEVICEREMOVED, SDL_ENABLE);
SDL_Init(SDL_INIT_GAMECONTROLLER | SDL_INIT_VIDEO);
// SDL_EventState(SDL_CONTROLLERDEVICEADDED, SDL_ENABLE);
// SDL_EventState(SDL_CONTROLLERDEVICEREMOVED, SDL_ENABLE);

PHYSFS_init(argv[0]);

Check warning on line 23 in src/application.cpp

View workflow job for this annotation

GitHub Actions / lint

src/application.cpp:23:15 [cppcoreguidelines-pro-bounds-pointer-arithmetic]

do not use pointer arithmetic
}
Expand Down
17 changes: 0 additions & 17 deletions src/audiodevice.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,19 +24,6 @@ audiodevice::audiodevice() : device(nullptr, [](ALCdevice *device) {
}

alcMakeContextCurrent(context.get());
std::cout << "ALC device and context initialized successfully." << std::endl;

// SDL_AudioSpec spec{};
// spec.format = AUDIO_S16SYS;
// spec.freq = 44100;
// spec.channels = 2;
// spec.samples = 4096;
// _id = SDL_OpenAudioDevice(nullptr, 0, &spec, nullptr, 0);

// if (_id == 0) {
// throw std::runtime_error(
// fmt::format("[SDL_OpenAudioDevice] error while opening audio device: {}", SDL_GetError()));
// }
}

audiodevice::~audiodevice() {
Expand All @@ -45,8 +32,4 @@ audiodevice::~audiodevice() {
alcMakeContextCurrent(nullptr);
std::cout << "ALC context unset successfully." << std::endl;
}

/* SDL_CloseAudioDevice(0); */
}

// uint32_t audiodevice::id() const { return _id; }
34 changes: 21 additions & 13 deletions src/soundfx.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,19 @@

using namespace audio;

static void callback(void *userdata, uint8_t *stream, int length) {
auto &buffer = static_cast<soundfx *>(userdata)->buffer;
// static void callback(void *userdata, uint8_t *stream, int length) {
// auto &buffer = static_cast<soundfx *>(userdata)->buffer;

if (buffer.empty()) {
SDL_memset(stream, 0, length);
return;
}
// if (buffer.empty()) {
// SDL_memset(stream, 0, length);
// return;
// }

const auto to_copy = std::min(static_cast<size_t>(length), buffer.size());
// SDL_memcpy(stream, buffer.data(), to_copy);
SDL_MixAudio(stream, buffer.data(), to_copy, SDL_MIX_MAXVOLUME);
buffer.erase(buffer.begin(), buffer.begin() + to_copy);
}
// const auto to_copy = std::min(static_cast<size_t>(length), buffer.size());
// // SDL_memcpy(stream, buffer.data(), to_copy);
// SDL_MixAudio(stream, buffer.data(), to_copy, SDL_MIX_MAXVOLUME);
// buffer.erase(buffer.begin(), buffer.begin() + to_copy);
// }

static size_t ovPHYSFS_read(void *ptr, size_t size, size_t nmemb,

Check warning on line 21 in src/soundfx.cpp

View workflow job for this annotation

GitHub Actions / lint

src/soundfx.cpp:21:15 [modernize-use-trailing-return-type]

use a trailing return type for this function
void *source) {
Expand Down Expand Up @@ -126,9 +126,17 @@ soundfx::soundfx(const std::shared_ptr<audiodevice> audiodevice,
}

int32_t offset{0};
const auto constexpr length = 1024 * 8;
const auto constexpr length = 1024 * 16;
std::array<uint8_t, length> array{0};

const auto info = ov_info(vf.get(), -1);
if (!info) {
throw std::runtime_error(std::fmt("[ov_info] failed to retrieve OggVorbis info, error: {}", ov_strerror(info)));
}

format = (info->channels == 1) ? AL_FORMAT_MONO16 : AL_FORMAT_STEREO16;
frequency = static_cast<ALsizei>(info->rate);

#if SDL_BYTEORDER == SDL_LIL_ENDIAN
const int bigendian = 0;
#else
Expand All @@ -145,7 +153,7 @@ soundfx::soundfx(const std::shared_ptr<audiodevice> audiodevice,
std::copy(array.begin(), array.begin() + offset, std::back_inserter(buffer));
} while (offset > 0);

UNUSED(callback);
// UNUSED(callback);
}

soundfx::~soundfx() {}
Expand Down
8 changes: 7 additions & 1 deletion src/soundfx.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,15 @@ class soundfx {

void play() const;

std::vector<uint8_t> buffer;
// std::vector<uint8_t> buffer;

private:
std::shared_ptr<audiodevice> _audiodevice;

// std::vector<uint8_t> data;
ALuint buffer{};
ALuint source{};
ALenum format{};
ALsizei frequency{};
};
}

0 comments on commit 2aa6187

Please sign in to comment.