Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

149 lignes
5.3 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(_WIN32)
  60. static int kbhit(void); // Check if a key has been pressed
  61. static char getch(); // Get pressed character
  62. #endif
  63. //------------------------------------------------------------------------------------
  64. // Program main entry point
  65. //------------------------------------------------------------------------------------
  66. int main(int argc, char *argv[])
  67. {
  68. // Initialization
  69. //--------------------------------------------------------------------------------------
  70. static unsigned char key = 0;
  71. InitAudioDevice();
  72. Sound fxWav = LoadSound("resources/audio/weird.wav"); // Load WAV audio file
  73. Sound fxOgg = LoadSound("resources/audio/target.ogg"); // Load OGG audio file
  74. Music music = LoadMusicStream("resources/audio/country.mp3");
  75. PlayMusicStream(music);
  76. printf("\nPress s or d to play sounds, ESC to stop...\n");
  77. //--------------------------------------------------------------------------------------
  78. // Main loop
  79. while (key != KEY_ESCAPE)
  80. {
  81. if (kbhit()) key = getch();
  82. if ((key == 's') || (key == 'S')) PlaySound(fxWav);
  83. if ((key == 'd') || (key == 'D')) PlaySound(fxOgg);
  84. key = 0;
  85. UpdateMusicStream(music);
  86. }
  87. // De-Initialization
  88. //--------------------------------------------------------------------------------------
  89. UnloadSound(fxWav); // Unload sound data
  90. UnloadSound(fxOgg); // Unload sound data
  91. UnloadMusicStream(music); // Unload music stream data
  92. CloseAudioDevice();
  93. //--------------------------------------------------------------------------------------
  94. return 0;
  95. }
  96. //----------------------------------------------------------------------------------
  97. // Module Functions Definition
  98. //----------------------------------------------------------------------------------
  99. #if !defined(_WIN32)
  100. // Check if a key has been pressed
  101. static int kbhit(void)
  102. {
  103. struct termios oldt, newt;
  104. int ch;
  105. int oldf;
  106. tcgetattr(STDIN_FILENO, &oldt);
  107. newt = oldt;
  108. newt.c_lflag &= ~(ICANON | ECHO);
  109. tcsetattr(STDIN_FILENO, TCSANOW, &newt);
  110. oldf = fcntl(STDIN_FILENO, F_GETFL, 0);
  111. fcntl(STDIN_FILENO, F_SETFL, oldf | O_NONBLOCK);
  112. ch = getchar();
  113. tcsetattr(STDIN_FILENO, TCSANOW, &oldt);
  114. fcntl(STDIN_FILENO, F_SETFL, oldf);
  115. if (ch != EOF)
  116. {
  117. ungetc(ch, stdin);
  118. return 1;
  119. }
  120. return 0;
  121. }
  122. // Get pressed character
  123. static char getch() { return getchar(); }
  124. #endif