raylib/examples/others/audio_standalone.c

124 lines
3.3 KiB
C
Raw Normal View History

2016-07-16 20:24:08 +03:00
/*******************************************************************************************
*
* 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)
2017-10-22 21:00:18 +03:00
* stb_vorbis - Ogg audio files loading (http://www.nothings.org/stb_vorbis/)
* jar_xm - XM module file loading
* jar_mod - MOD audio file loading
* dr_flac - FLAC audio file loading
2016-07-16 20:24:08 +03:00
*
* Compile audio module using:
2017-05-19 01:55:02 +03:00
* gcc -c audio.c stb_vorbis.c -Wall -std=c99 -DAUDIO_STANDALONE -DAL_LIBTYPE_STATIC
2016-07-16 20:24:08 +03:00
*
* Compile example using:
2017-05-19 01:55:02 +03:00
* gcc -o audio_standalone.exe audio_standalone.c audio.o stb_vorbis.o -lopenal32 -lwinmm /
2017-10-22 21:00:18 +03:00
* -s -Wall -std=c99 -Wl,-allow-multiple-definition
2016-07-16 20:24:08 +03:00
*
2017-05-19 01:55:02 +03:00
* This example has been created using raylib 1.7 (www.raylib.com)
2016-07-16 20:24:08 +03:00
* raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details)
*
2017-05-19 01:55:02 +03:00
* Copyright (c) 2017 Ramon Santamaria (@raysan5)
2016-07-16 20:24:08 +03:00
*
********************************************************************************************/
#include <stdio.h>
#include "audio.h"
#if defined(_WIN32)
2017-05-19 01:55:02 +03:00
#include <conio.h> // Windows only, no stardard library
#else
#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
2016-07-16 20:24:08 +03:00
#define KEY_ESCAPE 27
int main()
{
2016-11-18 16:05:49 +03:00
// Initialization
//--------------------------------------------------------------------------------------
static unsigned char key;
2016-07-16 20:24:08 +03:00
InitAudioDevice();
2016-07-16 20:24:08 +03:00
Sound fxWav = LoadSound("resources/audio/weird.wav"); // Load WAV audio file
Sound fxOgg = LoadSound("resources/audio/tanatana.ogg"); // Load OGG audio file
2016-08-01 22:37:45 +03:00
Music music = LoadMusicStream("resources/audio/guitar_noodling.ogg");
PlayMusicStream(music);
2016-07-16 20:24:08 +03:00
printf("\nPress s or d to play sounds...\n");
2016-11-18 16:05:49 +03:00
//--------------------------------------------------------------------------------------
// Main loop
2016-07-16 20:24:08 +03:00
while (key != KEY_ESCAPE)
{
if (kbhit()) key = getch();
if (key == 's')
{
PlaySound(fxWav);
key = 0;
}
2016-07-16 20:24:08 +03:00
if (key == 'd')
{
PlaySound(fxOgg);
key = 0;
}
2016-08-01 22:37:45 +03:00
UpdateMusicStream(music);
2016-07-16 20:24:08 +03:00
}
2016-11-18 16:05:49 +03:00
// De-Initialization
//--------------------------------------------------------------------------------------
2016-08-01 22:37:45 +03:00
UnloadSound(fxWav); // Unload sound data
UnloadSound(fxOgg); // Unload sound data
2016-08-01 22:37:45 +03:00
UnloadMusicStream(music); // Unload music stream data
2016-07-16 20:24:08 +03:00
CloseAudioDevice();
2016-11-18 16:05:49 +03:00
//--------------------------------------------------------------------------------------
2016-07-16 20:24:08 +03:00
return 0;
}