-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample_generate_sound_at_runtime.c
72 lines (60 loc) · 2.53 KB
/
example_generate_sound_at_runtime.c
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
/*******************************************************************************************
*
* raylib example - generate a sound at runtime
*
*
********************************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include "raylib.h"
//------------------------------------------------------------------------------------
// Program main entry point
//------------------------------------------------------------------------------------
int main(void)
{
// Initialization
//--------------------------------------------------------------------------------------
const int screenWidth = 800;
const int screenHeight = 450;
InitWindow(screenWidth, screenHeight, "raylib example - generating sounds at runtime");
SetTargetFPS(60); // Set our game to run at 60 frames-per-second
//--------------------------------------------------------------------------------------
InitAudioDevice();
Wave my_wave;
my_wave.frameCount=10000;
my_wave.channels=1;
my_wave.data=malloc(my_wave.frameCount*sizeof(float));
my_wave.sampleRate=48000;
my_wave.sampleSize=32;
float* tmp_pointer=(float*)my_wave.data;
float volume=1.0;
// FILL IN THE SOUND's AUDIO BUFFER WITH SAMPLES
for (int i=0;i<my_wave.frameCount;i++)
{
tmp_pointer[i]=sin((float)i/10.0)*volume;
volume-=1.0/(float)my_wave.frameCount;
}
Sound my_sound=LoadSoundFromWave(my_wave);
PlaySound(my_sound);
while (!WindowShouldClose()) // Detect window close button or ESC key
{
// Update
//----------------------------------------------------------------------------------
// TODO: Update your variables here
//----------------------------------------------------------------------------------
// Draw
//----------------------------------------------------------------------------------
BeginDrawing();
ClearBackground(BLACK);
DrawText("Congrats! You created your first sound!", 190, 200, 20, LIGHTGRAY);
EndDrawing();
//----------------------------------------------------------------------------------
}
// De-Initialization
//--------------------------------------------------------------------------------------
UnloadSound(my_sound);
CloseWindow(); // Close window and OpenGL context
//--------------------------------------------------------------------------------------
return 0;
}