aboutsummaryrefslogtreecommitdiff
path: root/examples/audio_raw_stream.lua
blob: 070984f9bbd281bbc2ab85bcf6b118e0aaa513d6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
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
-------------------------------------------------------------------------------------------