aboutsummaryrefslogtreecommitdiff
path: root/examples/audio_raw_stream.lua
diff options
context:
space:
mode:
authorraysan5 <raysan5@gmail.com>2016-08-06 11:32:35 +0200
committerraysan5 <raysan5@gmail.com>2016-08-06 11:32:35 +0200
commit00c7e54d3c593dbddb036f2185e614e7e4b22a1f (patch)
tree3ef6a46031712ff9551c2711adcc9fa3045a618b /examples/audio_raw_stream.lua
parent5f1b4e94745303ab9df87421cdd9ffb9448fee01 (diff)
downloadraylib-00c7e54d3c593dbddb036f2185e614e7e4b22a1f.tar.gz
raylib-00c7e54d3c593dbddb036f2185e614e7e4b22a1f.zip
Add raylib lua examples
Diffstat (limited to 'examples/audio_raw_stream.lua')
-rw-r--r--examples/audio_raw_stream.lua97
1 files changed, 97 insertions, 0 deletions
diff --git a/examples/audio_raw_stream.lua b/examples/audio_raw_stream.lua
new file mode 100644
index 00000000..070984f9
--- /dev/null
+++ b/examples/audio_raw_stream.lua
@@ -0,0 +1,97 @@
+-------------------------------------------------------------------------------------------
+--
+-- raylib [audio] example - Raw audio streaming
+--
+-- NOTE: This example requires OpenAL Soft library installed
+--
+-- This example has been created using raylib 1.6 (www.raylib.com)
+-- raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details)
+--
+-- Copyright (c) 2014-2016 Ramon Santamaria (@raysan5)
+--
+-------------------------------------------------------------------------------------------
+
+MAX_SAMPLES = 20000
+DEG2RAD = math.pi/180.0
+
+-- Initialization
+-------------------------------------------------------------------------------------------
+local screenWidth = 800
+local screenHeight = 450
+
+InitWindow(screenWidth, screenHeight, "raylib [audio] example - raw audio streaming")
+
+InitAudioDevice() -- Initialize audio device
+
+-- Init raw audio stream (sample rate: 22050, sample size: 32bit-float, channels: 1-mono)
+local stream = InitAudioStream(22050, 32, 1)
+
+-- Fill audio stream with some samples (sine wave)
+local data = {}
+
+for i = 1, MAX_SAMPLES do
+ data[i] = math.sin(((2*math.pi*i)/2)*DEG2RAD)
+end
+
+-- NOTE: The generated MAX_SAMPLES do not fit to close a perfect loop
+-- for that reason, there is a clip everytime audio stream is looped
+
+PlayAudioStream(stream)
+
+local totalSamples = MAX_SAMPLES
+local samplesLeft = totalSamples
+
+local position = Vector2(0, 0)
+
+SetTargetFPS(30) -- Set our game to run at 30 frames-per-second
+-------------------------------------------------------------------------------------------
+
+-- Main game loop
+while not WindowShouldClose() do -- Detect window close button or ESC key
+ -- Update
+ ---------------------------------------------------------------------------------------
+
+ -- Refill audio stream if required
+ if (IsAudioBufferProcessed(stream)) then
+ local numSamples = 0
+
+ if (samplesLeft >= 4096) then numSamples = 4096
+ else numSamples = samplesLeft end
+
+ UpdateAudioStream(stream, data + (totalSamples - samplesLeft), numSamples)
+
+ samplesLeft = samplesLeft - numSamples
+
+ -- Reset samples feeding (loop audio)
+ if (samplesLeft <= 0) then samplesLeft = totalSamples end
+ end
+ ---------------------------------------------------------------------------------------
+
+ -- Draw
+ ---------------------------------------------------------------------------------------
+ BeginDrawing()
+
+ ClearBackground(RAYWHITE)
+
+ DrawText("SINE WAVE SHOULD BE PLAYING!", 240, 140, 20, LIGHTGRAY)
+
+ -- NOTE: Draw a part of the sine wave (only screen width)
+ for i = 1, GetScreenWidth() do
+ position.x = (i - 1)
+ position.y = 250 + 50*data[i]
+
+ DrawPixelV(position, RED)
+ end
+
+ EndDrawing()
+ ---------------------------------------------------------------------------------------
+end
+
+-- De-Initialization
+-------------------------------------------------------------------------------------------
+CloseAudioStream(stream) -- Close raw audio stream and delete buffers from RAM
+
+CloseAudioDevice() -- Close audio device (music streaming is automatically stopped)
+
+CloseWindow() -- Close window and OpenGL context
+------------------------------------------------------------------------------------------- \ No newline at end of file