You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

152 lines
5.4 KiB

  1. /*******************************************************************************************
  2. *
  3. * raylib [audio] example - Using raudio module as standalone module
  4. *
  5. * NOTE: This example does not require any graphic device, it can run directly on console.
  6. *
  7. * DEPENDENCIES:
  8. * miniaudio.h - Audio device management lib (https://github.com/dr-soft/miniaudio)
  9. * stb_vorbis.h - Ogg audio files loading (http://www.nothings.org/stb_vorbis/)
  10. * dr_wav.h - WAV audio file loading (https://github.com/mackron/dr_libs)
  11. * dr_mp3.h - MP3 audio file loading (https://github.com/mackron/dr_libs)
  12. * dr_flac.h - FLAC audio file loading (https://github.com/mackron/dr_libs)
  13. * jar_xm.h - XM module file loading
  14. * jar_mod.h - MOD audio file loading
  15. *
  16. * COMPILATION:
  17. * gcc -o raudio_standalone.exe raudio_standalone.c ..\..\src\raudio.c /
  18. * -I..\..\src -I..\..\src\external -L. -Wall -std=c99 -DRAUDIO_STANDALONE /
  19. * -DSUPPORT_FILEFORMAT_WAV -DSUPPORT_FILEFORMAT_OGG -DSUPPORT_FILEFORMAT_MP3
  20. *
  21. * LICENSE: zlib/libpng
  22. *
  23. * This example is licensed under an unmodified zlib/libpng license, which is an OSI-certified,
  24. * BSD-like license that allows static linking with closed source software:
  25. *
  26. * Copyright (c) 2014-2020 Ramon Santamaria (@raysan5)
  27. *
  28. * This software is provided "as-is", without any express or implied warranty. In no event
  29. * will the authors be held liable for any damages arising from the use of this software.
  30. *
  31. * Permission is granted to anyone to use this software for any purpose, including commercial
  32. * applications, and to alter it and redistribute it freely, subject to the following restrictions:
  33. *
  34. * 1. The origin of this software must not be misrepresented; you must not claim that you
  35. * wrote the original software. If you use this software in a product, an acknowledgment
  36. * in the product documentation would be appreciated but is not required.
  37. *
  38. * 2. Altered source versions must be plainly marked as such, and must not be misrepresented
  39. * as being the original software.
  40. *
  41. * 3. This notice may not be removed or altered from any source distribution.
  42. *
  43. ********************************************************************************************/
  44. #include "raudio.h" // raylib audio library
  45. #include <stdio.h> // Required for: printf()
  46. #if defined(_WIN32)
  47. #include <conio.h> // Windows only, no stardard library
  48. #else
  49. // Required for kbhit() function in non-Windows platforms
  50. #include <stdio.h>
  51. #include <termios.h>
  52. #include <unistd.h>
  53. #include <fcntl.h>
  54. #endif
  55. #define KEY_ESCAPE 27
  56. //----------------------------------------------------------------------------------
  57. // Module Functions Declaration
  58. //----------------------------------------------------------------------------------
  59. #if !defined(_MSC_VER)
  60. static int kbhit(void); // Check if a key has been pressed
  61. static char getch(); // Get pressed character
  62. #else
  63. #define kbhit _kbhit
  64. #define getch _getch
  65. #endif
  66. //------------------------------------------------------------------------------------
  67. // Program main entry point
  68. //------------------------------------------------------------------------------------
  69. int main(int argc, char *argv[])
  70. {
  71. // Initialization
  72. //--------------------------------------------------------------------------------------
  73. static unsigned char key = 0;
  74. InitAudioDevice();
  75. Sound fxWav = LoadSound("resources/audio/weird.wav"); // Load WAV audio file
  76. Sound fxOgg = LoadSound("resources/audio/target.ogg"); // Load OGG audio file
  77. Music music = LoadMusicStream("resources/audio/country.mp3");
  78. PlayMusicStream(music);
  79. printf("\nPress s or d to play sounds, ESC to stop...\n");
  80. //--------------------------------------------------------------------------------------
  81. // Main loop
  82. while (key != KEY_ESCAPE)
  83. {
  84. if (kbhit()) key = getch();
  85. if ((key == 's') || (key == 'S')) PlaySound(fxWav);
  86. if ((key == 'd') || (key == 'D')) PlaySound(fxOgg);
  87. key = 0;
  88. UpdateMusicStream(music);
  89. }
  90. // De-Initialization
  91. //--------------------------------------------------------------------------------------
  92. UnloadSound(fxWav); // Unload sound data
  93. UnloadSound(fxOgg); // Unload sound data
  94. UnloadMusicStream(music); // Unload music stream data
  95. CloseAudioDevice();
  96. //--------------------------------------------------------------------------------------
  97. return 0;
  98. }
  99. //----------------------------------------------------------------------------------
  100. // Module Functions Definition
  101. //----------------------------------------------------------------------------------
  102. #if !defined(_WIN32)
  103. // Check if a key has been pressed
  104. static int kbhit(void)
  105. {
  106. struct termios oldt, newt;
  107. int ch;
  108. int oldf;
  109. tcgetattr(STDIN_FILENO, &oldt);
  110. newt = oldt;
  111. newt.c_lflag &= ~(ICANON | ECHO);
  112. tcsetattr(STDIN_FILENO, TCSANOW, &newt);
  113. oldf = fcntl(STDIN_FILENO, F_GETFL, 0);
  114. fcntl(STDIN_FILENO, F_SETFL, oldf | O_NONBLOCK);
  115. ch = getchar();
  116. tcsetattr(STDIN_FILENO, TCSANOW, &oldt);
  117. fcntl(STDIN_FILENO, F_SETFL, oldf);
  118. if (ch != EOF)
  119. {
  120. ungetc(ch, stdin);
  121. return 1;
  122. }
  123. return 0;
  124. }
  125. // Get pressed character
  126. static char getch() { return getchar(); }
  127. #endif