summaryrefslogtreecommitdiffhomepage
path: root/examples/others/audio_standalone.c
blob: 6d7fe1f0fbe2d3196614a263865872ac46394bc3 (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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
/*******************************************************************************************
*
*   raylib [audio] example - Using audio module as standalone module
*
*   NOTE: This example does not require any graphic device, it can run directly on console.
*
*   [audio] module requires some external libs:
*       OpenAL Soft - Audio device management lib (http://kcat.strangesoft.net/openal.html)
*       stb_vorbis - Ogg audio files loading (http://www.nothings.org/stb_vorbis/)
*       jar_xm - XM module file loading
*       jar_mod - MOD audio file loading
*
*   Compile audio module using:
*       gcc -c audio.c stb_vorbis.c -Wall -std=c99 -DAUDIO_STANDALONE -DAL_LIBTYPE_STATIC
*
*   Compile example using:
*       gcc -o audio_standalone.exe audio_standalone.c audio.o stb_vorbis.o -lopenal32 -lwinmm /
*           -Wall -std=c99 -Wl,-allow-multiple-definition
*
*   This example has been created using raylib 1.7 (www.raylib.com)
*   raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details)
*
*   Copyright (c) 2017 Ramon Santamaria (@raysan5)
*
********************************************************************************************/

#include <stdio.h>
#if defined(_WIN32)
#include <conio.h>          // Windows only, no stardard library
#endif

#include "audio.h"

#if defined(__linux__)

#include <stdio.h>
#include <termios.h>
#include <unistd.h>
#include <fcntl.h>

static int kbhit(void)
{
	struct termios oldt, newt;
	int ch;
	int oldf;

	tcgetattr(STDIN_FILENO, &oldt);
	newt = oldt;
	newt.c_lflag &= ~(ICANON | ECHO);
	tcsetattr(STDIN_FILENO, TCSANOW, &newt);
	oldf = fcntl(STDIN_FILENO, F_GETFL, 0);
	fcntl(STDIN_FILENO, F_SETFL, oldf | O_NONBLOCK);

	ch = getchar();

	tcsetattr(STDIN_FILENO, TCSANOW, &oldt);
	fcntl(STDIN_FILENO, F_SETFL, oldf);

	if(ch != EOF)
	{
		ungetc(ch, stdin);
		return 1;
	}

	return 0;
}

static char getch()
{
	return getchar();
}

#endif

#define KEY_ESCAPE  27

int main()
{
    // Initialization
    //--------------------------------------------------------------------------------------
    unsigned char key;
    
    InitAudioDevice();
    
    Sound fxWav = LoadSound("resources/audio/weird.wav");         // Load WAV audio file
    Sound fxOgg = LoadSound("resources/audio/tanatana.ogg");      // Load OGG audio file
    
    Music music = LoadMusicStream("resources/audio/guitar_noodling.ogg");
    PlayMusicStream(music);

    printf("\nPress s or d to play sounds...\n");
    //--------------------------------------------------------------------------------------

    // Main loop
    while (key != KEY_ESCAPE)
    {
        if (kbhit()) key = getch();

        if (key == 's')
        {
            PlaySound(fxWav);
            key = 0;
        }
        
        if (key == 'd')
        {
            PlaySound(fxOgg);
            key = 0;
        }
        
        UpdateMusicStream(music);
    }
    
    // De-Initialization
    //--------------------------------------------------------------------------------------
    UnloadSound(fxWav);         // Unload sound data
    UnloadSound(fxOgg);         // Unload sound data
    
    UnloadMusicStream(music);   // Unload music stream data
    
    CloseAudioDevice();
    //--------------------------------------------------------------------------------------

    return 0;
}