Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use std::hash to generate a proper unique key in _SNDOPEN #533

Merged
merged 3 commits into from
Aug 26, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
5 changes: 3 additions & 2 deletions internal/c/parts/audio/audio.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1175,7 +1175,7 @@ struct SoundHandle {
ma_uint32 maFlags; // miniaudio flags that were used when initializing the sound
ma_decoder_config maDecoderConfig; // miniaudio decoder configuration
ma_decoder *maDecoder; // this is used for files that are loaded directly from memory
intptr_t bufferKey; // a key that will uniquely identify the data the decoder will use
uint64_t bufferKey; // a key that will uniquely identify the data the decoder will use
ma_audio_buffer_config maAudioBufferConfig; // miniaudio buffer configuration
ma_audio_buffer *maAudioBuffer; // this is used for user created audio buffers (memory is managed by miniaudio)
RawStream *rawStream; // Raw sample frame queue
Expand Down Expand Up @@ -1620,7 +1620,8 @@ int32_t func__sndopen(qbs *qbsFileName, qbs *qbsRequirements, int32_t passed) {
// Load the file from file or memory based on the requirements string
if (fromMemory) {
// Configure a miniaudio decoder to load the sound from memory
audioEngine.soundHandles[handle]->bufferKey = (intptr_t)qbsFileName->chr; // make a unique key and save it
audioEngine.soundHandles[handle]->bufferKey = std::hash<std::string_view>{}(
std::string_view(reinterpret_cast<const char *>(qbsFileName->chr), qbsFileName->len)); // make a unique key and save it
audioEngine.bufferMap.AddBuffer(qbsFileName->chr, qbsFileName->len, audioEngine.soundHandles[handle]->bufferKey); // make a copy of the buffer
auto [buffer, bufferSize] = audioEngine.bufferMap.GetBuffer(audioEngine.soundHandles[handle]->bufferKey); // get the buffer pointer and size
audioEngine.maResult = InitializeSoundFromMemory(buffer, bufferSize, handle); // create the ma_sound
Expand Down
12 changes: 7 additions & 5 deletions internal/c/parts/audio/framework.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@
#include <cstdint>
#include <cstdio>
#include <cstring>
#include <functional>
#include <stack>
#include <string_view>
#include <unordered_map>
#include <utility>
#include <vector>
Expand Down Expand Up @@ -68,7 +70,7 @@ class BufferMap {
size_t refCount;
};

std::unordered_map<intptr_t, Buffer> buffers;
std::unordered_map<uint64_t, Buffer> buffers;

public:
// Delete assignment operators
Expand All @@ -88,7 +90,7 @@ class BufferMap {
/// @param size The size of the data
/// @param key The unique key that should be used
/// @return True if successful
bool AddBuffer(const void *data, size_t size, intptr_t key) {
bool AddBuffer(const void *data, size_t size, uint64_t key) {
if (data && size && key && buffers.find(key) == buffers.end()) {
Buffer buf = {};

Expand All @@ -111,7 +113,7 @@ class BufferMap {

/// @brief Increments the buffer reference count
/// @param key The unique key for the buffer
void AddRef(intptr_t key) {
void AddRef(uint64_t key) {
const auto it = buffers.find(key);
if (it != buffers.end()) {
auto &buf = it->second;
Expand All @@ -124,7 +126,7 @@ class BufferMap {

/// @brief Decrements the buffer reference count and frees the buffer if the reference count reaches zero
/// @param key The unique key for the buffer
void Release(intptr_t key) {
void Release(uint64_t key) {
const auto it = buffers.find(key);
if (it != buffers.end()) {
auto &buf = it->second;
Expand All @@ -144,7 +146,7 @@ class BufferMap {
/// @brief Gets the raw pointer and size of the buffer with the given key
/// @param key The unique key for the buffer
/// @return An std::pair of the buffer raw pointer and size
std::pair<const void *, size_t> GetBuffer(intptr_t key) const {
std::pair<const void *, size_t> GetBuffer(uint64_t key) const {
const auto it = buffers.find(key);
if (it == buffers.end()) {
AUDIO_DEBUG_PRINT("Buffer not found");
Expand Down