Skip to content

Commit

Permalink
Add the audio sound loading example
Browse files Browse the repository at this point in the history
  • Loading branch information
wilsonsilva committed Oct 23, 2023
1 parent 1bf8b16 commit 7a51d54
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 0 deletions.
Binary file added examples/audio/audio_sound_loading.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
60 changes: 60 additions & 0 deletions examples/audio/audio_sound_loading.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
# ******************************************************************************************
#
# raylib [audio] example - Sound loading and playing
#
# Example originally created with raylib 1.1, last time updated with raylib 3.5
#
# Example ported to Ruby by Wilson Silva (@wilsonsilva). Works with Raylib 4.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-2023 Ramon Santamaria (@raysan5)
#
# ******************************************************************************************

require 'bundler/setup'
require 'raylib'

# Initialization
# --------------------------------------------------------------------------------------
SCREEN_WIDTH = 800
SCREEN_HEIGHT = 450

Raylib.init_window(SCREEN_WIDTH, SCREEN_HEIGHT, "raylib [audio] example - sound loading and playing")

Raylib.init_audio_device

fx_wav = Raylib.load_sound(File.join(__dir__, "resources/sound.wav")) # Load WAV audio file
fx_ogg = Raylib.load_sound(File.join(__dir__, "resources/target.ogg")) # Load OGG audio file

Raylib.set_target_fps(60)
# --------------------------------------------------------------------------------------

# Main game loop
until Raylib.window_should_close # Detect window close button or ESC key
# Update
# ----------------------------------------------------------------------------------
Raylib.play_sound(fx_wav) if Raylib.is_key_pressed(Raylib::KEY_SPACE) # Play WAV sound
Raylib.play_sound(fx_ogg) if Raylib.is_key_pressed(Raylib::KEY_ENTER) # Play OGG sound
# ----------------------------------------------------------------------------------

# Draw
# ----------------------------------------------------------------------------------
Raylib.begin_drawing
Raylib.clear_background(Raylib::RAYWHITE)
Raylib.draw_text("Press SPACE to PLAY the WAV sound!", 200, 180, 20, Raylib::LIGHTGRAY)
Raylib.draw_text("Press ENTER to PLAY the OGG sound!", 200, 220, 20, Raylib::LIGHTGRAY)
Raylib.end_drawing
# ----------------------------------------------------------------------------------
end

# De-Initialization
# --------------------------------------------------------------------------------------
Raylib.unload_sound(fx_wav) # Unload sound data
Raylib.unload_sound(fx_ogg) # Unload sound data

Raylib.close_audio_device # Close audio device

Raylib.close_window # Close window and OpenGL context
# --------------------------------------------------------------------------------------
2 changes: 2 additions & 0 deletions examples/audio/resources/LICENSE.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
| resource | author | licence | notes |
| :------------------- | :---------: | :------ | :---- |
| country.mp3 | [@emegeme](https://github.com/emegeme) | [CC0](https://creativecommons.org/publicdomain/zero/1.0/) | Originally created for "DART that TARGET" game |
| target.ogg | [@emegeme](https://github.com/emegeme) | [CC0](https://creativecommons.org/publicdomain/zero/1.0/) | Originally created for "DART that TARGET" game |
| coin.wav | [@raysan5](https://github.com/raysan5) | [CC0](https://creativecommons.org/publicdomain/zero/1.0/) | Made with [rFXGen](https://raylibtech.itch.io/rfxgen) |
| sound.wav | [@raysan5](https://github.com/raysan5) | [CC0](https://creativecommons.org/publicdomain/zero/1.0/) | Made with [rFXGen](https://raylibtech.itch.io/rfxgen) |
| mini1111.xm | [tPORt](https://modarchive.org/index.php?request=view_by_moduleid&query=51891) | [Mod Archive Distribution license](https://modarchive.org/index.php?terms-upload) | - |
Binary file added examples/audio/resources/sound.wav
Binary file not shown.
Binary file added examples/audio/resources/target.flac
Binary file not shown.

0 comments on commit 7a51d54

Please sign in to comment.