Advertisement
Guest User

Untitled

a guest
Mar 13th, 2018
418
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 8.41 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <allegro5/allegro.h>
  3. #include <allegro5/allegro_audio.h>
  4. #include <allegro5/allegro_acodec.h>
  5.  
  6.  
  7. int musicVol = 255;
  8. int musicEnable = 1;
  9. int soundVol = 255;
  10. int soundEnable = 1;
  11.  
  12.  
  13. ALLEGRO_DISPLAY *d;
  14.  
  15. //audio stuff
  16. //Opening Theme
  17. ALLEGRO_SAMPLE *roetitle = NULL;
  18. ALLEGRO_SAMPLE_INSTANCE *music = NULL;
  19.  
  20. //Sound Effects
  21. ALLEGRO_SAMPLE *smpl_tick = NULL;
  22. ALLEGRO_SAMPLE *smpl_hits = NULL;
  23. ALLEGRO_SAMPLE *smpl_hitns = NULL;
  24. ALLEGRO_SAMPLE *smpl_beam = NULL;
  25. ALLEGRO_SAMPLE *smpl_miss = NULL;
  26. ALLEGRO_SAMPLE *smpl_alert = NULL;
  27. ALLEGRO_SAMPLE *smpl_open= NULL;
  28. ALLEGRO_SAMPLE *smpl_error = NULL;
  29. ALLEGRO_SAMPLE *smpl_destr = NULL;
  30. ALLEGRO_SAMPLE *smpl_send = NULL;
  31. ALLEGRO_SAMPLE *smpl_ping = NULL;
  32. ALLEGRO_SAMPLE *smpl_shields = NULL;
  33. ALLEGRO_SAMPLE *smpl_received = NULL;
  34. ALLEGRO_SAMPLE *smpl_panel = NULL;
  35. ALLEGRO_SAMPLE *smpl_sdown = NULL;
  36.  
  37. ALLEGRO_SAMPLE_INSTANCE *sfx_tick = NULL;
  38. ALLEGRO_SAMPLE_INSTANCE *sfx_hits = NULL;
  39. ALLEGRO_SAMPLE_INSTANCE *sfx_hitns = NULL;
  40. ALLEGRO_SAMPLE_INSTANCE *sfx_beam = NULL;
  41. ALLEGRO_SAMPLE_INSTANCE *sfx_miss = NULL;
  42. ALLEGRO_SAMPLE_INSTANCE *sfx_alert = NULL;
  43. ALLEGRO_SAMPLE_INSTANCE *sfx_open = NULL;
  44. ALLEGRO_SAMPLE_INSTANCE *sfx_error = NULL;
  45. ALLEGRO_SAMPLE_INSTANCE *sfx_destr = NULL;
  46. ALLEGRO_SAMPLE_INSTANCE *sfx_send = NULL;
  47. ALLEGRO_SAMPLE_INSTANCE *sfx_ping = NULL;
  48. ALLEGRO_SAMPLE_INSTANCE *sfx_shields = NULL;
  49. ALLEGRO_SAMPLE_INSTANCE *sfx_received = NULL;
  50. ALLEGRO_SAMPLE_INSTANCE *sfx_panel = NULL;
  51. ALLEGRO_SAMPLE_INSTANCE *sfx_sdown = NULL;
  52.  
  53.  
  54. ALLEGRO_MIXER *master_mixer = NULL;
  55. ALLEGRO_MIXER *music_mixer = NULL;
  56. ALLEGRO_MIXER *sfx_mixer = NULL;
  57. ALLEGRO_VOICE *voice = NULL;
  58.  
  59. void startup()
  60. {
  61.     if (!al_init()) {
  62.         fprintf(stderr, "failed to initialize allegro!\n");
  63.         exit(1);
  64.     }
  65.     if (!al_install_keyboard()) {
  66.         fprintf(stderr, "Error installing keyboard.\n");
  67.         exit(1);
  68.     }
  69.  
  70.     if (!al_init_primitives_addon()) {
  71.         fprintf(stderr, "Error installing prims.\n");
  72.         exit(1);
  73.     }
  74.  
  75.     if (!al_init_image_addon()) {
  76.         fprintf(stderr, "Error installing image addon.\n");
  77.         exit(1);
  78.     }
  79.  
  80.  
  81.     d = al_create_display(1024, 768);
  82.  
  83.  
  84. }
  85.  
  86. /*!
  87. * \brief Loads music to be played
  88. */
  89. void MusicInit(void)
  90.  
  91. {
  92.     roetitle = al_load_sample("res/roetitle.ogg");
  93.     music = al_create_sample_instance(roetitle);
  94.     al_attach_sample_instance_to_mixer(music, music_mixer);
  95.     al_attach_mixer_to_mixer(music_mixer, master_mixer);
  96. }
  97.  
  98.  
  99. /*!
  100. * \brief Terminate music driver
  101. * \note just stops the music - driver is unloded at game termination
  102. */
  103. void MusicTerm(void)
  104.  
  105. {
  106.     //  al_set_sample_instance_playing(music, false);
  107. }
  108.  
  109.  
  110. /*!
  111. * \brief Stops music playback
  112. */
  113. void MusicStop(void)
  114. {
  115.     al_set_sample_instance_playing(music, false);
  116. }
  117.  
  118.  
  119. /*!
  120. * \brief Returns MIDI mark
  121. * \return 0
  122. * \warning Not implmented - not used in game
  123. */
  124. int MusicMark(void)
  125. {
  126.     return (0);
  127. }
  128.  
  129.  
  130. /*!
  131. * \brief Waits for music to stop playing
  132. */
  133. void MusicWait(void)
  134. {
  135.     while (al_get_sample_instance_playing(music));
  136. }
  137.  
  138.  
  139. /*!
  140. * \brief Plays MIDI song
  141. * \param i Music number to play
  142. */
  143. void MusicPlay(int i)
  144.  
  145. {
  146.     float volume = 1;
  147.     volume = musicVol / 255;
  148.     if (musicEnable == 0) { volume = 0; }
  149.     al_set_mixer_gain(music_mixer, volume);
  150.  
  151.     switch (i) {
  152.     case 1:
  153.         al_set_sample_instance_playing(music, true);
  154.     }
  155. }
  156.  
  157.  
  158. /*!
  159. * \brief Stops MIDI song
  160. * \note Needs to be migrated to allegro
  161. * \warning Not implmented
  162. */
  163. void SoundStop(void)
  164.  
  165. {
  166.     printf("TODO: SoundStop()\n");
  167. }
  168.  
  169.  
  170. /*!
  171. * \brief Initialzes sound driver
  172. * \note Terminates if driver fails
  173. */
  174. void SoundInit(void)
  175.  
  176. {
  177.     if (!al_install_audio()) {
  178.         fprintf(stderr, "failed to initialize audio!\n");
  179.         exit(-1);
  180.     }
  181.  
  182.     if (!al_init_acodec_addon()) {
  183.         fprintf(stderr, "failed to initialize audio codecs!\n");
  184.         exit(-1);
  185.     }
  186.  
  187.     if (!al_reserve_samples(1)) {
  188.         fprintf(stderr, "failed to reserve samples!\n");
  189.         exit(-1);
  190.     }
  191.  
  192.     music_mixer = al_create_mixer(44100, ALLEGRO_AUDIO_DEPTH_FLOAT32, ALLEGRO_CHANNEL_CONF_2);
  193.     sfx_mixer = al_create_mixer(44100, ALLEGRO_AUDIO_DEPTH_FLOAT32, ALLEGRO_CHANNEL_CONF_2);
  194.     master_mixer = al_create_mixer(44100, ALLEGRO_AUDIO_DEPTH_FLOAT32, ALLEGRO_CHANNEL_CONF_2);
  195.     voice = al_create_voice(44100, ALLEGRO_AUDIO_DEPTH_INT16, ALLEGRO_CHANNEL_CONF_2);
  196.     al_attach_mixer_to_voice(master_mixer, voice);
  197.  
  198. }
  199.  
  200. /*!
  201. * \brief loads sound effects from HDD
  202. */
  203. void SoundLoad(void)
  204.  
  205. {
  206.     smpl_beam = al_load_sample("res/beam.ogg");
  207.     sfx_beam = al_create_sample_instance(smpl_beam);
  208.     al_attach_sample_instance_to_mixer(sfx_beam, sfx_mixer);
  209.  
  210.     smpl_tick = al_load_sample("res/tick.ogg");
  211.     sfx_tick = al_create_sample_instance(smpl_tick);
  212.     al_attach_sample_instance_to_mixer(sfx_tick, sfx_mixer);
  213.  
  214.     smpl_hits = al_load_sample("res\hits.ogg");
  215.     sfx_hits = al_create_sample_instance(smpl_hits);
  216.     al_attach_sample_instance_to_mixer(sfx_hits, sfx_mixer);
  217.  
  218.     smpl_hitns = al_load_sample("res\hitns.ogg");
  219.     sfx_hitns = al_create_sample_instance(smpl_hitns);
  220.     al_attach_sample_instance_to_mixer(sfx_hitns, sfx_mixer);
  221.  
  222.     smpl_miss = al_load_sample("res\miss.ogg");
  223.     sfx_miss = al_create_sample_instance(smpl_miss);
  224.     al_attach_sample_instance_to_mixer(sfx_miss, sfx_mixer);
  225.  
  226.     smpl_alert = al_load_sample("res\alert.ogg");
  227.     sfx_alert = al_create_sample_instance(smpl_alert);
  228.     al_attach_sample_instance_to_mixer(sfx_alert, sfx_mixer);
  229.  
  230.     smpl_open = al_load_sample("res\open.ogg");
  231.     sfx_open = al_create_sample_instance(smpl_open);
  232.     al_attach_sample_instance_to_mixer(sfx_open, sfx_mixer);
  233.  
  234.     smpl_error = al_load_sample("res\error.ogg");
  235.     sfx_error = al_create_sample_instance(smpl_error);
  236.     al_attach_sample_instance_to_mixer(sfx_tick, sfx_mixer);
  237.  
  238.     smpl_destr = al_load_sample("res\destr.ogg");
  239.     sfx_destr = al_create_sample_instance(smpl_destr);
  240.     al_attach_sample_instance_to_mixer(sfx_error, sfx_mixer);
  241.  
  242.     smpl_send = al_load_sample("res\send.ogg");
  243.     sfx_send = al_create_sample_instance(smpl_send);
  244.     al_attach_sample_instance_to_mixer(sfx_send, sfx_mixer);
  245.  
  246.     smpl_ping = al_load_sample("res\ping.ogg");
  247.     sfx_ping = al_create_sample_instance(smpl_ping);
  248.     al_attach_sample_instance_to_mixer(sfx_ping, sfx_mixer);
  249.  
  250.     smpl_shields = al_load_sample("res\shields.ogg");
  251.     sfx_shields = al_create_sample_instance(smpl_shields);
  252.     al_attach_sample_instance_to_mixer(sfx_shields, sfx_mixer);
  253.  
  254.     smpl_received = al_load_sample("res\received.ogg");
  255.     sfx_received = al_create_sample_instance(smpl_received);
  256.     al_attach_sample_instance_to_mixer(sfx_received, sfx_mixer);
  257.  
  258.     smpl_panel = al_load_sample("res\panel.ogg");
  259.     sfx_panel = al_create_sample_instance(smpl_panel);
  260.     al_attach_sample_instance_to_mixer(sfx_panel, sfx_mixer);
  261.  
  262.     smpl_sdown = al_load_sample("res\sdown.ogg");
  263.     sfx_sdown = al_create_sample_instance(smpl_sdown);
  264.     al_attach_sample_instance_to_mixer(sfx_sdown, sfx_mixer);
  265.  
  266.     al_attach_mixer_to_mixer(sfx_mixer, master_mixer);
  267. }
  268.  
  269.  
  270. /*!
  271. * \brief Plays back sound
  272. * \param i Sound to playback
  273. */
  274. void SoundPlay(int i)
  275. {
  276.     float volume;
  277.     volume = soundVol / 255;
  278.     if (soundEnable == 0) { volume = 0; }
  279.     al_set_mixer_gain(sfx_mixer, volume);
  280.  
  281.     switch (i) {
  282.     case 2:
  283.         al_set_sample_instance_playing(sfx_tick, true);
  284.         break;
  285.     case 3:
  286.         al_set_sample_instance_playing(sfx_hits, true);
  287.         break;
  288.     case 4:
  289.         al_set_sample_instance_playing(sfx_hitns, true);
  290.         break;
  291.     case 5:
  292.         al_set_sample_instance_playing(sfx_beam, true);
  293.         break;
  294.     case 6:
  295.         al_set_sample_instance_playing(sfx_miss, true);
  296.         break;
  297.     case 7:
  298.         al_set_sample_instance_playing(sfx_alert, true);
  299.         break;
  300.     case 8:
  301.         al_set_sample_instance_playing(sfx_open, true);
  302.         break;
  303.     case 9:
  304.         al_set_sample_instance_playing(sfx_error, true);
  305.         break;
  306.     case 10:
  307.         al_set_sample_instance_playing(sfx_destr, true);
  308.         break;
  309.     case 11:
  310.         al_set_sample_instance_playing(sfx_send, true);
  311.         break;
  312.     case 13:
  313.         al_set_sample_instance_playing(sfx_ping, true);
  314.         break;
  315.     case 14:
  316.         al_set_sample_instance_playing(sfx_shields, true);
  317.         break;
  318.     case 15:
  319.         al_set_sample_instance_playing(sfx_received, true);
  320.         break;
  321.     case 16:
  322.         al_set_sample_instance_playing(sfx_panel, true);
  323.         break;
  324.     case 17:
  325.         al_set_sample_instance_playing(sfx_sdown, true);
  326.         break;
  327.     default:
  328.         return;
  329.     }
  330. }
  331.  
  332. int main(int argc, char **argv) {
  333.  
  334.     int test = 0;
  335.  
  336.     startup();
  337.     SoundInit();
  338.     SoundLoad();
  339.     MusicInit();
  340.     MusicPlay(1);
  341.     al_rest(1);
  342.     MusicStop();
  343.     al_rest(1);
  344.  
  345.     for (test = 0; test < 20; test++)
  346.     {
  347.         SoundPlay(test);
  348.         printf("Playing sound %d\n", test);
  349.         al_rest(2);
  350.     }
  351.  
  352.  
  353.  
  354.     while (1);
  355.  
  356. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement