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

Add Sound support #55

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 commits
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
83 changes: 83 additions & 0 deletions examples/audio_sound_loading.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
/*******************************************************************************************
*
* raylib [audio] example - Sound loading and playing
*
* Example originally created with raylib 1.1, last time updated with raylib 3.5
*
* Example licensed under an unmodified zlib/libpng license, which is an OSI-certified,
* BSD-like license that allows static linking with closed source software
*
* Copyright (c) 2014-2024 Ramon Santamaria (@raysan5)
*
********************************************************************************************/

#include "raylib.h"

void raylib_js_set_entry(void (*entry)(void));

Sound fxWav;
Sound fxOgg;

void GameFrame(void)
{
// Update
//----------------------------------------------------------------------------------
if (IsKeyPressed(KEY_SPACE)) PlaySound(fxWav); // Play WAV sound
if (IsKeyPressed(KEY_ENTER)) PlaySound(fxOgg); // Play OGG sound
//----------------------------------------------------------------------------------

// Draw
//----------------------------------------------------------------------------------
BeginDrawing();

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);

EndDrawing();
//----------------------------------------------------------------------------------
}

//------------------------------------------------------------------------------------
// Program main entry point
//------------------------------------------------------------------------------------
int main(void)
{
// Initialization
//--------------------------------------------------------------------------------------
const int screenWidth = 800;
const int screenHeight = 450;

InitWindow(screenWidth, screenHeight, "raylib [audio] example - sound loading and playing");

InitAudioDevice(); // Initialize audio device

fxWav = LoadSound("resources/sound.wav"); // Load WAV audio file
fxOgg = LoadSound("resources/target.ogg"); // Load OGG audio file

SetTargetFPS(60); // Set desired framerate (frames-per-second)
//--------------------------------------------------------------------------------------

#ifdef PLATFORM_WEB
raylib_js_set_entry(GameFrame);
#else
// Main game loop
while (!WindowShouldClose()) // Detect window close button or ESC key
{
GameFrame();
}

// De-Initialization
//--------------------------------------------------------------------------------------
UnloadSound(fxWav); // Unload sound data
UnloadSound(fxOgg); // Unload sound data

CloseAudioDevice(); // Close audio device

CloseWindow(); // Close window and OpenGL context
//--------------------------------------------------------------------------------------
#endif

return 0;
}
1 change: 1 addition & 0 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@
<script>
const wasmPaths = {
"tsoding": ["tsoding_ball", "tsoding_snake",],
"audio": ["audio_sound_loading"],
"core": ["core_basic_window", "core_basic_screen_manager", "core_input_keys", "core_input_mouse_wheel",],
"shapes": ["shapes_colors_palette"],
"text": ["text_writing_anim"],
Expand Down
5 changes: 5 additions & 0 deletions nob.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ typedef struct {
} Example;

Example examples[] = {
{
.src_path = "./examples/audio_sound_loading.c",
.bin_path = "./build/audio_sound_loading",
.wasm_path = "./wasm/audio_sound_loading.wasm",
},
{
.src_path = "./examples/core_basic_window.c",
.bin_path = "./build/core_basic_window",
Expand Down
73 changes: 73 additions & 0 deletions raylib.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ class RaylibJs {
this.currentMousePosition = {x: 0, y: 0};
this.images = [];
this.quit = false;
this.sounds = [];
}

constructor() {
Expand Down Expand Up @@ -344,6 +345,78 @@ class RaylibJs {
this.ctx.fillText(text, posX, posY + fontSize);
}

// RLAPI void InitAudioDevice(void); // Initialize audio device and context
InitAudioDevice() {
if (!this.sounds) {
this.sounds = [];
}
}

// RLAPI void CloseAudioDevice(void); // Close the audio device and context
CloseAudioDevice() {
this.sounds = [];
}

// RLAPI bool IsAudioDeviceReady(void); // Check if audio device has been initialized successfully
IsAudioDeviceReady() {
return this.sounds && Array.isArray(this.sounds);
}

// RLAPI Sound LoadSound(const char *fileName); // Load sound from file
LoadSound(result_ptr, fileName_ptr) {
const buffer = this.wasm.instance.exports.memory.buffer;
const filename = cstr_by_ptr(buffer, fileName_ptr);

let result = new Uint32Array(buffer, result_ptr, 6);
const sound = new Audio(filename);
this.sounds.push(sound);

result[0] = this.sounds.length; // AudioStream::buffer
result[1] = 1; // AudioStream::processor
result[2] = 1; // AudioStream::sampleRate
result[3] = 1; // AudioStream::sampleSize
result[4] = 2; // AudioStream::channels
result[5] = 1; // AudioStream::frameCount;
RobLoach marked this conversation as resolved.
Show resolved Hide resolved

return result;
}

// RLAPI bool IsSoundReady(Sound sound); // Checks if a sound is ready
IsSoundReady(sound_ptr) {
const buffer = this.wasm.instance.exports.memory.buffer;
const [id, processor, sampleRate, sampleSize, channels, frameCount] = new Uint32Array(buffer, sound_ptr, 6);
if (id <= 0 || id > this.sounds.length) {
return false;
}
return this.sounds[id - 1] != null;
}

// RLAPI void UnloadSound(Sound sound); // Unload sound
UnloadSound(sound_ptr) {
const buffer = this.wasm.instance.exports.memory.buffer;
const [id, processor, sampleRate, sampleSize, channels, frameCount] = new Uint32Array(buffer, sound_ptr, 6);
if (id <= 0 || id > this.sounds.length) {
return;
}
this.sounds[id - 1] = null;
}

// RLAPI void PlaySound(Sound sound); // Play a sound
PlaySound(sound_ptr) {
const buffer = this.wasm.instance.exports.memory.buffer;
const [id, processor, sampleRate, sampleSize, channels, frameCount] = new Uint32Array(buffer, sound_ptr, 6);
if (id <= 0 || id > this.sounds.length) {
return;
}
const audio = this.sounds[id - 1];
if (!audio) {
return;
}
audio.loop = false;
audio.currentTime = 0;
audio.play();
}

raylib_js_set_entry(entry) {
this.entryFunction = this.wasm.instance.exports.__indirect_function_table.get(entry);
}
Expand Down
Binary file added resources/sound.wav
Binary file not shown.
Binary file added resources/target.ogg
Binary file not shown.
Binary file added wasm/audio_sound_loading.wasm
Binary file not shown.