audio_standalone.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /*******************************************************************************************
  2. *
  3. * raylib [audio] example - Using audio module as standalone module
  4. *
  5. * NOTE: This example does not require any graphic device, it can run directly on console.
  6. *
  7. * [audio] module requires some external libs:
  8. * OpenAL Soft - Audio device management lib (http://kcat.strangesoft.net/openal.html)
  9. * stb_vorbis - Ogg audio files loading (http://www.nothings.org/stb_vorbis/)
  10. * jar_xm - XM module file loading
  11. * jar_mod - MOD audio file loading
  12. * dr_flac - FLAC audio file loading
  13. *
  14. * Compile audio module using:
  15. * gcc -c audio.c stb_vorbis.c -Wall -std=c99 -DAUDIO_STANDALONE -DAL_LIBTYPE_STATIC
  16. *
  17. * Compile example using:
  18. * gcc -o audio_standalone.exe audio_standalone.c audio.o stb_vorbis.o -lopenal32 -lwinmm /
  19. * -s -Wall -std=c99 -Wl,-allow-multiple-definition
  20. *
  21. * This example has been created using raylib 1.7 (www.raylib.com)
  22. * raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details)
  23. *
  24. * Copyright (c) 2017 Ramon Santamaria (@raysan5)
  25. *
  26. ********************************************************************************************/
  27. #include <stdio.h>
  28. #if defined(_WIN32)
  29. #include <conio.h> // Windows only, no stardard library
  30. #endif
  31. #include "audio.h"
  32. #if defined(__linux__)
  33. #include <stdio.h>
  34. #include <termios.h>
  35. #include <unistd.h>
  36. #include <fcntl.h>
  37. static int kbhit(void)
  38. {
  39. struct termios oldt, newt;
  40. int ch;
  41. int oldf;
  42. tcgetattr(STDIN_FILENO, &oldt);
  43. newt = oldt;
  44. newt.c_lflag &= ~(ICANON | ECHO);
  45. tcsetattr(STDIN_FILENO, TCSANOW, &newt);
  46. oldf = fcntl(STDIN_FILENO, F_GETFL, 0);
  47. fcntl(STDIN_FILENO, F_SETFL, oldf | O_NONBLOCK);
  48. ch = getchar();
  49. tcsetattr(STDIN_FILENO, TCSANOW, &oldt);
  50. fcntl(STDIN_FILENO, F_SETFL, oldf);
  51. if(ch != EOF)
  52. {
  53. ungetc(ch, stdin);
  54. return 1;
  55. }
  56. return 0;
  57. }
  58. static char getch()
  59. {
  60. return getchar();
  61. }
  62. #endif
  63. #define KEY_ESCAPE 27
  64. int main()
  65. {
  66. // Initialization
  67. //--------------------------------------------------------------------------------------
  68. unsigned char key;
  69. InitAudioDevice();
  70. Sound fxWav = LoadSound("resources/audio/weird.wav"); // Load WAV audio file
  71. Sound fxOgg = LoadSound("resources/audio/tanatana.ogg"); // Load OGG audio file
  72. Music music = LoadMusicStream("resources/audio/guitar_noodling.ogg");
  73. PlayMusicStream(music);
  74. printf("\nPress s or d to play sounds...\n");
  75. //--------------------------------------------------------------------------------------
  76. // Main loop
  77. while (key != KEY_ESCAPE)
  78. {
  79. if (kbhit()) key = getch();
  80. if (key == 's')
  81. {
  82. PlaySound(fxWav);
  83. key = 0;
  84. }
  85. if (key == 'd')
  86. {
  87. PlaySound(fxOgg);
  88. key = 0;
  89. }
  90. UpdateMusicStream(music);
  91. }
  92. // De-Initialization
  93. //--------------------------------------------------------------------------------------
  94. UnloadSound(fxWav); // Unload sound data
  95. UnloadSound(fxOgg); // Unload sound data
  96. UnloadMusicStream(music); // Unload music stream data
  97. CloseAudioDevice();
  98. //--------------------------------------------------------------------------------------
  99. return 0;
  100. }