From c90255bb076468aa5f8e82361ebae5765facae47 Mon Sep 17 00:00:00 2001 From: realtradam Date: Thu, 11 May 2023 23:11:40 -0400 Subject: implemented some more audio functions --- include/rodeo/audio.h | 18 ++++++++-- scan.rb | 44 ----------------------- src/audio/rodeo_audio.c | 94 ++++++++++++++++++++++++++++++++++++++++++------- 3 files changed, 97 insertions(+), 59 deletions(-) delete mode 100644 scan.rb diff --git a/include/rodeo/audio.h b/include/rodeo/audio.h index 2d03250..d9f7a95 100644 --- a/include/rodeo/audio.h +++ b/include/rodeo/audio.h @@ -10,19 +10,31 @@ typedef struct rodeo_audio_sound_t rodeo_audio_sound_t; void rodeo_audio_initialize( - int32_t num_sound_pools, - int32_t size_sound_pools + uint32_t num_sound_pools, + uint32_t size_sound_pools ); void rodeo_audio_deinitialize(void); +uint32_t +irodeo_audio_channelPool_num_get(void); + +uint32_t +irodeo_audio_channelPool_size_get(void); + void rodeo_audio_masterVolume_set(float volume_level); float rodeo_audio_masterVolume_get(void); +void +rodeo_audio_channelPool_volume_set(uint32_t channel_pool_id, float volume_level); + +float +rodeo_audio_channelPool_volume_get(uint32_t channel_pool_id); + rodeo_audio_sound_t* rodeo_audio_sound_create_from_path(cstr path); @@ -30,4 +42,4 @@ void rodeo_audio_sound_destroy(rodeo_audio_sound_t* sound); void -rodeo_audio_sound_play(rodeo_audio_sound_t *sound, int32_t pool_id); +rodeo_audio_sound_play(rodeo_audio_sound_t *sound, uint32_t pool_id); diff --git a/scan.rb b/scan.rb deleted file mode 100644 index 75c7959..0000000 --- a/scan.rb +++ /dev/null @@ -1,44 +0,0 @@ -require "rexml/document" - -class CodeModule - attr_accessor :title, :color, :files, :code - - def initialize(title:, files:, color:) - self.title = title - self.color = color - self.files = files - self.code = "" - end -end - -code_modules = [ - CodeModule.new(title: "Core", files: ['rodeo_8h.xml', 'common_8h.xml'], color: "stone"), - CodeModule.new(title: "Audio", files: ['audio_8h.xml'], color: "blue"), - CodeModule.new(title: "Input", files: ['input_8h.xml'], color: "red"), - CodeModule.new(title: "Log", files: ['log_8h.xml'], color: "yellow"), -] -code_modules.each_with_index do |mod, index| - mod.files.each do |file| - file = File.new("doxygen/xml/#{file}") - doc = REXML::Document.new file - root = doc.root - - defines = root.get_elements('//sectiondef').filter { |attr| attr.attributes['kind'] == "func" } - - defines.each do |func| - func.get_elements('//definition').each_with_index do |defi, i| - mod.code += "#{defi.text}#{func.get_elements('//argsstring')[i].text};\n" - end - end - end - File.write("./docs/#{index}#{mod.title.downcase}.html", - "--- -title: #{mod.title} -id: #{mod.title.downcase} -color: #{mod.color} ---- -{% highlight c %} -#{mod.code} -{% endhighlight %} -") -end diff --git a/src/audio/rodeo_audio.c b/src/audio/rodeo_audio.c index 0adb3eb..79cdac4 100644 --- a/src/audio/rodeo_audio.c +++ b/src/audio/rodeo_audio.c @@ -10,10 +10,12 @@ #include "SDL.h" #include "SDL_mixer.h" -static int32_t **irodeo_audio_channelPool; +static uint32_t **irodeo_audio_channelPool; +static uint32_t irodeo_audio_channelPool_num; +static uint32_t irodeo_audio_channelPool_size; void -rodeo_audio_initialize(int32_t num_sound_pools, int32_t size_sound_pools) +rodeo_audio_initialize(uint32_t num_sound_pools, uint32_t size_sound_pools) { if(Mix_OpenAudio(44100, MIX_DEFAULT_FORMAT, 2, 2048) < 0) { @@ -25,20 +27,59 @@ rodeo_audio_initialize(int32_t num_sound_pools, int32_t size_sound_pools) } else { - irodeo_audio_channelPool = malloc((uint32_t)(num_sound_pools * size_sound_pools) * sizeof(int32_t)); - Mix_AllocateChannels(num_sound_pools * size_sound_pools); + irodeo_audio_channelPool_num = num_sound_pools; + irodeo_audio_channelPool_size = size_sound_pools; - int32_t temp_channel_id = 0; - for(int i = 0; i < num_sound_pools; ++i) + void *mem_alloc = + malloc( + ((irodeo_audio_channelPool_num) * sizeof(intptr_t)) + + ((irodeo_audio_channelPool_num * irodeo_audio_channelPool_size) * sizeof(uint32_t) + )); + + irodeo_audio_channelPool = mem_alloc; + + uint32_t *channelPool = (uint32_t*)(irodeo_audio_channelPool + ((irodeo_audio_channelPool_num) * sizeof(intptr_t))); + + { + int32_t allocated_channels = + Mix_AllocateChannels(( + int32_t)irodeo_audio_channelPool_num * (int32_t)irodeo_audio_channelPool_size + ); + + rodeo_log( + rodeo_logLevel_info, + "Number of channels opened: %d", + allocated_channels + ); + } + + uint32_t temp_channel_id = 0; + for(uint32_t i = 0; i < irodeo_audio_channelPool_num; ++i) { - //irodeo_audio_channelPool[i] = - for(int j = 0; j < size_sound_pools; ++j) + irodeo_audio_channelPool[i] = + channelPool + (i * sizeof(uint32_t)); + + for(uint32_t j = 0; j < irodeo_audio_channelPool_size; ++j) { irodeo_audio_channelPool[i][j] = temp_channel_id; temp_channel_id += 1; } + + int32_t assigning_tag_status = Mix_GroupChannel( + (int32_t)temp_channel_id, + (int32_t)i + ); + if(0 == assigning_tag_status) + { + rodeo_log( + rodeo_logLevel_error, + "Failed to assign tag to sound channel. Mix_Error: %s", + Mix_GetError() + ); + } } + // Lets not explode player's ears, yea? rodeo_audio_masterVolume_set(0.5f); } } @@ -46,14 +87,27 @@ rodeo_audio_initialize(int32_t num_sound_pools, int32_t size_sound_pools) void rodeo_audio_deinitialize(void) { + free(irodeo_audio_channelPool); Mix_Quit(); } +uint32_t +irodeo_audio_channelPool_num_get(void) +{ + return irodeo_audio_channelPool_num; +} + +uint32_t +irodeo_audio_channelPool_size_get(void) +{ + return irodeo_audio_channelPool_size; +} + void rodeo_audio_masterVolume_set(float volume_level) { - Mix_MasterVolume((int32_t)(volume_level * 128)); - Mix_VolumeMusic((int32_t)(volume_level * 128)); + Mix_MasterVolume((int32_t)(volume_level * (float)MIX_MAX_VOLUME)); + Mix_VolumeMusic((int32_t)(volume_level * (float)MIX_MAX_VOLUME)); } float @@ -62,6 +116,22 @@ rodeo_audio_masterVolume_get(void) return ((float)Mix_MasterVolume(-1)) / (float)MIX_MAX_VOLUME; } +void +rodeo_audio_channelPool_volume_set(uint32_t channel_pool_id, float volume_level) +{ + for(uint32_t i = 0; i < irodeo_audio_channelPool_size; i += 1) + { + Mix_Volume((int32_t)irodeo_audio_channelPool[channel_pool_id][i], (int32_t)(volume_level * (float)MIX_MAX_VOLUME)); + } +} + +float +rodeo_audio_channelPool_volume_get(uint32_t channel_pool_id) +{ + return ((float)Mix_Volume((int32_t)channel_pool_id, -1) / + (float)MIX_MAX_VOLUME); +} + rodeo_audio_sound_t* rodeo_audio_sound_create_from_path(cstr path) { @@ -86,7 +156,7 @@ rodeo_audio_sound_destroy(rodeo_audio_sound_t* sound) } void -rodeo_audio_sound_play(rodeo_audio_sound_t *sound, int32_t pool_id) +rodeo_audio_sound_play(rodeo_audio_sound_t *sound, uint32_t channel_pool_id) { - Mix_PlayChannel(pool_id, sound->sdl_sound, 0); + Mix_PlayChannel((int32_t)channel_pool_id, sound->sdl_sound, 0); } -- cgit v1.2.3