THIS COMMIT IS FOR REVIEW: audio update functions clarity review in examples

This commit is contained in:
iann 2025-11-13 07:31:47 +09:00
commit b3c25186d9

View file

@ -4,7 +4,7 @@
*
* Example complexity rating: [★☆☆☆] 1/4
*
* Example originally created with raylib 1.1, last time updated with raylib 3.5
* Example originally created with raylib 1.1, last time updated with raylib 6.0
*
* Example licensed under an unmodified zlib/libpng license, which is an OSI-certified,
* BSD-like license that allows static linking with closed source software
@ -14,6 +14,8 @@
********************************************************************************************/
#include "raylib.h"
#include <stdlib.h> // Required for: malloc(), free()
#include <string.h> // Required for: memcpy()
//------------------------------------------------------------------------------------
// Program main entry point
@ -32,6 +34,17 @@ int main(void)
Sound fxWav = LoadSound("resources/sound.wav"); // Load WAV audio file
Sound fxOgg = LoadSound("resources/target.ogg"); // Load OGG audio file
bool soundReversed = false;
float *soundData = malloc(sizeof(float)*fxWav.frameCount*fxWav.stream.channels);
float *scratchSoundData = malloc(sizeof(float)*fxWav.frameCount*fxWav.stream.channels);
Wave wave = LoadWave("resources/sound.wav");
// Sounds always have 32bit sampleSize:
WaveFormat(&wave, fxWav.stream.sampleRate, 32, fxWav.stream.channels);
memcpy(soundData, wave.data, sizeof(float)*fxWav.frameCount*fxWav.stream.channels);
UnloadWave(wave);
SetTargetFPS(60); // Set our game to run at 60 frames-per-second
//--------------------------------------------------------------------------------------
@ -40,6 +53,18 @@ int main(void)
{
// Update
//----------------------------------------------------------------------------------
if (IsKeyPressed(KEY_R))
{
soundReversed = !soundReversed;
for (unsigned int i = 0; i < fxWav.frameCount; i++)
{
unsigned int src = (soundReversed)? fxWav.frameCount - 1 - i : i;
// Sounds always have STEREO channels:
scratchSoundData[i * fxWav.stream.channels + 0] = soundData[src * fxWav.stream.channels + 0];
scratchSoundData[i * fxWav.stream.channels + 1] = soundData[src * fxWav.stream.channels + 1];
}
UpdateSound(fxWav, scratchSoundData, fxWav.frameCount);
}
if (IsKeyPressed(KEY_SPACE)) PlaySound(fxWav); // Play WAV sound
if (IsKeyPressed(KEY_ENTER)) PlaySound(fxOgg); // Play OGG sound
//----------------------------------------------------------------------------------
@ -51,7 +76,9 @@ int main(void)
ClearBackground(RAYWHITE);
DrawText("Press SPACE to PLAY the WAV sound!", 200, 180, 20, LIGHTGRAY);
DrawText("Press ENTER to PLAY the OGG sound!", 200, 220, 20, LIGHTGRAY);
DrawText(TextFormat("Press R to REVERSE the WAV sound : "), 120, 220, 20, LIGHTGRAY);
DrawText((soundReversed)? "BACKWARDS" : "FORWARDS", 525, 220, 20, (soundReversed)? MAROON : DARKGREEN);
DrawText("Press ENTER to PLAY the OGG sound!", 200, 260, 20, LIGHTGRAY);
EndDrawing();
//----------------------------------------------------------------------------------
@ -59,6 +86,8 @@ int main(void)
// De-Initialization
//--------------------------------------------------------------------------------------
free(soundData);
free(scratchSoundData);
UnloadSound(fxWav); // Unload sound data
UnloadSound(fxOgg); // Unload sound data