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.

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