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.3 KiB

7 years ago
7 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. * 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. #include "audio.h"
  29. #if defined(_WIN32)
  30. #include <conio.h> // Windows only, no stardard library
  31. #else
  32. #include <stdio.h>
  33. #include <termios.h>
  34. #include <unistd.h>
  35. #include <fcntl.h>
  36. static int kbhit(void)
  37. {
  38. struct termios oldt, newt;
  39. int ch;
  40. int oldf;
  41. tcgetattr(STDIN_FILENO, &oldt);
  42. newt = oldt;
  43. newt.c_lflag &= ~(ICANON | ECHO);
  44. tcsetattr(STDIN_FILENO, TCSANOW, &newt);
  45. oldf = fcntl(STDIN_FILENO, F_GETFL, 0);
  46. fcntl(STDIN_FILENO, F_SETFL, oldf | O_NONBLOCK);
  47. ch = getchar();
  48. tcsetattr(STDIN_FILENO, TCSANOW, &oldt);
  49. fcntl(STDIN_FILENO, F_SETFL, oldf);
  50. if(ch != EOF)
  51. {
  52. ungetc(ch, stdin);
  53. return 1;
  54. }
  55. return 0;
  56. }
  57. static char getch()
  58. {
  59. return getchar();
  60. }
  61. #endif
  62. #define KEY_ESCAPE 27
  63. int main()
  64. {
  65. // Initialization
  66. //--------------------------------------------------------------------------------------
  67. static unsigned char key;
  68. InitAudioDevice();
  69. Sound fxWav = LoadSound("resources/audio/weird.wav"); // Load WAV audio file
  70. Sound fxOgg = LoadSound("resources/audio/tanatana.ogg"); // Load OGG audio file
  71. Music music = LoadMusicStream("resources/audio/guitar_noodling.ogg");
  72. PlayMusicStream(music);
  73. printf("\nPress s or d to play sounds...\n");
  74. //--------------------------------------------------------------------------------------
  75. // Main loop
  76. while (key != KEY_ESCAPE)
  77. {
  78. if (kbhit()) key = getch();
  79. if (key == 's')
  80. {
  81. PlaySound(fxWav);
  82. key = 0;
  83. }
  84. if (key == 'd')
  85. {
  86. PlaySound(fxOgg);
  87. key = 0;
  88. }
  89. UpdateMusicStream(music);
  90. }
  91. // De-Initialization
  92. //--------------------------------------------------------------------------------------
  93. UnloadSound(fxWav); // Unload sound data
  94. UnloadSound(fxOgg); // Unload sound data
  95. UnloadMusicStream(music); // Unload music stream data
  96. CloseAudioDevice();
  97. //--------------------------------------------------------------------------------------
  98. return 0;
  99. }