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.

123 lines
3.2 KiB

8 years ago
  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. *
  13. * Compile audio module using:
  14. * gcc -c audio.c stb_vorbis.c -Wall -std=c99 -DAUDIO_STANDALONE
  15. *
  16. * Compile example using:
  17. * gcc -o $(NAME_PART).exe $(FILE_NAME) audio.o stb_vorbis.o -lopenal32 -std=c99
  18. *
  19. * This example has been created using raylib 1.5 (www.raylib.com)
  20. * raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details)
  21. *
  22. * Copyright (c) 2015 Ramon Santamaria (@raysan5)
  23. *
  24. ********************************************************************************************/
  25. #include <stdio.h>
  26. #if defined(_WIN32)
  27. #include <conio.h> // Windows only, no stardard library
  28. #endif
  29. #include "audio.h"
  30. #if defined(__linux__)
  31. #include <stdio.h>
  32. #include <termios.h>
  33. #include <unistd.h>
  34. #include <fcntl.h>
  35. static int kbhit(void)
  36. {
  37. struct termios oldt, newt;
  38. int ch;
  39. int oldf;
  40. tcgetattr(STDIN_FILENO, &oldt);
  41. newt = oldt;
  42. newt.c_lflag &= ~(ICANON | ECHO);
  43. tcsetattr(STDIN_FILENO, TCSANOW, &newt);
  44. oldf = fcntl(STDIN_FILENO, F_GETFL, 0);
  45. fcntl(STDIN_FILENO, F_SETFL, oldf | O_NONBLOCK);
  46. ch = getchar();
  47. tcsetattr(STDIN_FILENO, TCSANOW, &oldt);
  48. fcntl(STDIN_FILENO, F_SETFL, oldf);
  49. if(ch != EOF)
  50. {
  51. ungetc(ch, stdin);
  52. return 1;
  53. }
  54. return 0;
  55. }
  56. static char getch()
  57. {
  58. return getchar();
  59. }
  60. #endif
  61. #define KEY_ESCAPE 27
  62. int main()
  63. {
  64. // Initialization
  65. //--------------------------------------------------------------------------------------
  66. unsigned char key;
  67. InitAudioDevice();
  68. Sound fxWav = LoadSound("resources/audio/weird.wav"); // Load WAV audio file
  69. Sound fxOgg = LoadSound("resources/audio/tanatana.ogg"); // Load OGG audio file
  70. Music music = LoadMusicStream("resources/audio/guitar_noodling.ogg");
  71. PlayMusicStream(music);
  72. printf("\nPress s or d to play sounds...\n");
  73. //--------------------------------------------------------------------------------------
  74. // Main loop
  75. while (key != KEY_ESCAPE)
  76. {
  77. if (kbhit()) key = getch();
  78. if (key == 's')
  79. {
  80. PlaySound(fxWav);
  81. key = 0;
  82. }
  83. if (key == 'd')
  84. {
  85. PlaySound(fxOgg);
  86. key = 0;
  87. }
  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. }