|
| 1 | +# ****************************************************************************************** |
| 2 | +# |
| 3 | +# raylib [audio] example - Mixed audio processing |
| 4 | +# |
| 5 | +# Example originally created with raylib 4.2, last time updated with raylib 4.2 |
| 6 | +# |
| 7 | +# Example contributed by hkc (@hatkidchan) and reviewed by Ramon Santamaria (@raysan5) |
| 8 | +# |
| 9 | +# Example ported to Ruby by Wilson Silva (@wilsonsilva). Works partially with Raylib 4.5 |
| 10 | +# |
| 11 | +# Example licensed under an unmodified zlib/libpng license, which is an OSI-certified, |
| 12 | +# BSD-like license that allows static linking with closed source software |
| 13 | +# |
| 14 | +# Copyright (c) 2023 hkc (@hatkidchan) |
| 15 | +# |
| 16 | +# ****************************************************************************************** |
| 17 | + |
| 18 | +# FIXME: Attaching the audio mixed processor callback causes the program to slow down until the |
| 19 | +# music is no longer playing. Does not work even if I remove all the code from the callback. |
| 20 | + |
| 21 | +require 'bundler/setup' |
| 22 | +require 'raylib' |
| 23 | + |
| 24 | +# FIXME: Global variables can potentially be avoided |
| 25 | + |
| 26 | +$exponent = 1.0 # Audio exponentiation value |
| 27 | +$average_volume = Array.new(400, 0) # Average volume history |
| 28 | + |
| 29 | +# ------------------------------------------------------------------------------------ |
| 30 | +# Audio processing function |
| 31 | +# ------------------------------------------------------------------------------------ |
| 32 | + |
| 33 | +ProcessAudio = FFI::Function.new(:void, [:pointer, :uint]) do |buffer, frames| |
| 34 | + # samples = buffer.to_a |
| 35 | + # average = 0.0 # Temporary average volume |
| 36 | + # |
| 37 | + # frames.times do |frame| |
| 38 | + # left = samples[frame * 2] |
| 39 | + # right = samples[frame * 2 + 1] |
| 40 | + # |
| 41 | + # left = (left.abs ** $exponent) * (left < 0.0 ? -1.0 : 1.0) |
| 42 | + # right = (right.abs ** $exponent) * (right < 0.0 ? -1.0 : 1.0) |
| 43 | + # |
| 44 | + # average += (left.abs / frames) + (right.abs / frames) |
| 45 | + # end |
| 46 | + # |
| 47 | + # # Moving history to the left |
| 48 | + # $average_volume.shift(399) |
| 49 | + # $average_volume << average # Adding last average value |
| 50 | +end |
| 51 | + |
| 52 | +# Initialization |
| 53 | +# -------------------------------------------------------------------------------------- |
| 54 | +SCREEN_WIDTH = 800 |
| 55 | +SCREEN_HEIGHT = 450 |
| 56 | + |
| 57 | +Raylib.init_window(SCREEN_WIDTH, SCREEN_HEIGHT, "raylib [audio] example - processing mixed output") |
| 58 | + |
| 59 | +Raylib.init_audio_device # Initialize audio device |
| 60 | +# Raylib.attach_audio_mixed_processor(ProcessAudio) # FIXME: Uncomment when callbacks are supported |
| 61 | + |
| 62 | +music = Raylib.load_music_stream(File.join(__dir__, "resources/country.mp3")) # FIXME: Should work without File.join |
| 63 | +sound = Raylib.load_sound(File.join(__dir__, "resources/coin.wav")) # FIXME: Should work without File.join |
| 64 | + |
| 65 | +Raylib.play_music_stream(music) |
| 66 | + |
| 67 | +Raylib.set_target_fps(60) # Set our game to run at 60 frames-per-second |
| 68 | +# -------------------------------------------------------------------------------------- |
| 69 | + |
| 70 | +# Main game loop |
| 71 | +until Raylib.window_should_close # Detect window close button or ESC key |
| 72 | + # Update |
| 73 | + # ---------------------------------------------------------------------------------- |
| 74 | + Raylib.update_music_stream(music) # Update music buffer with new stream data |
| 75 | + |
| 76 | + # Modify processing variables |
| 77 | + # ---------------------------------------------------------------------------------- |
| 78 | + $exponent -= 0.05 if Raylib.is_key_pressed(Raylib::KEY_LEFT) |
| 79 | + $exponent += 0.05 if Raylib.is_key_pressed(Raylib::KEY_RIGHT) |
| 80 | + |
| 81 | + $exponent = 0.5 if $exponent <= 0.5 |
| 82 | + $exponent = 3.0 if $exponent >= 3.0 |
| 83 | + |
| 84 | + Raylib.play_sound(sound) if Raylib.is_key_pressed(Raylib::KEY_SPACE) |
| 85 | + |
| 86 | + # Draw |
| 87 | + # ---------------------------------------------------------------------------------- |
| 88 | + Raylib.begin_drawing |
| 89 | + |
| 90 | + Raylib.clear_background(Raylib::RAYWHITE) |
| 91 | + |
| 92 | + Raylib.draw_text("MUSIC SHOULD BE PLAYING!", 255, 150, 20, Raylib::LIGHTGRAY) |
| 93 | + Raylib.draw_text(Raylib.text_format("EXPONENT = %.2f", :float, $exponent), 215, 180, 20, Raylib::LIGHTGRAY) |
| 94 | + |
| 95 | + Raylib.draw_rectangle(199, 199, 402, 34, Raylib::LIGHTGRAY) |
| 96 | + 400.times do |i| |
| 97 | + Raylib.draw_line(201 + i, 232 - $average_volume[i] * 32, 201 + i, 232, Raylib::MAROON) |
| 98 | + end |
| 99 | + |
| 100 | + Raylib.draw_rectangle_lines(199, 199, 402, 34, Raylib::GRAY) |
| 101 | + |
| 102 | + Raylib.draw_text("PRESS SPACE TO PLAY OTHER SOUND", 200, 250, 20, Raylib::LIGHTGRAY) |
| 103 | + Raylib.draw_text("USE LEFT AND RIGHT ARROWS TO ALTER DISTORTION", 140, 280, 20, Raylib::LIGHTGRAY) |
| 104 | + |
| 105 | + Raylib.end_drawing |
| 106 | + # ---------------------------------------------------------------------------------- |
| 107 | +end |
| 108 | + |
| 109 | +# De-Initialization |
| 110 | +# -------------------------------------------------------------------------------------- |
| 111 | +Raylib.unload_music_stream(music) # Unload music stream buffers from RAM |
| 112 | +# Raylib.detach_audio_mixed_processor(ProcessAudio) # Disconnect audio processor # FIXME: Uncomment when callbacks are supported |
| 113 | +Raylib.close_audio_device # Close audio device (music streaming is automatically stopped) |
| 114 | +Raylib.close_window # Close window and OpenGL context |
| 115 | +# -------------------------------------------------------------------------------------- |
0 commit comments