home *** CD-ROM | disk | FTP | other *** search
/ ring.yamanashi.ac.jp/pub/pc/freem/action/ / action.zip / a7xpg0_11.zip / a7xpg / src / abagames / util / sdl / Sound.d < prev    next >
Text File  |  2003-09-20  |  3KB  |  130 lines

  1. /*
  2.  * $Id: Sound.d,v 1.1.1.1 2003/09/19 14:55:49 kenta Exp $
  3.  *
  4.  * Copyright 2003 Kenta Cho. All rights reserved.
  5.  */
  6. module abagames.util.sdl.Sound;
  7.  
  8. import string;
  9. import SDL;
  10. import SDL_mixer;
  11. import abagames.util.sdl.SDLInitFailedException;
  12.  
  13. /**
  14.  * BGM/SE.
  15.  */
  16. public class Sound {
  17.  public:
  18.   static bool noSound = false;
  19.   static int fadeOutSpeed = 1280;
  20.   static char[] soundsDir = "sounds/";
  21.   static char[] chunksDir = "sounds/";
  22.  
  23.   public static void init() {
  24.     if (noSound) return;
  25.  
  26.     int audio_rate;
  27.     Uint16 audio_format;
  28.     int audio_channels;
  29.     int audio_buffers;
  30.  
  31.     if (SDL_InitSubSystem(SDL_INIT_AUDIO) < 0) {
  32.       noSound = 1;
  33.       throw new SDLInitFailedException
  34.     ("Unable to initialize SDL_AUDIO: " ~ string.toString(SDL_GetError()));
  35.     }
  36.  
  37.     audio_rate = 44100;
  38.     audio_format = AUDIO_S16;
  39.     audio_channels = 1;
  40.     audio_buffers = 4096;
  41.     if (Mix_OpenAudio(audio_rate, audio_format, audio_channels, audio_buffers) < 0) {
  42.       noSound = 1;
  43.       throw new SDLInitFailedException
  44.     ("Couldn't open audio: " ~ string.toString(SDL_GetError()));
  45.     }
  46.     Mix_QuerySpec(&audio_rate, &audio_format, &audio_channels);
  47.   }
  48.  
  49.   public static void close() {
  50.     if (noSound) return;
  51.     if (Mix_PlayingMusic()) {
  52.       Mix_HaltMusic();
  53.     }
  54.     Mix_CloseAudio();
  55.   }
  56.  
  57.  protected:
  58.   Mix_Music* music;
  59.   Mix_Chunk* chunk;
  60.   int chunkChannel;
  61.  
  62.  private:
  63.  
  64.   // Load a sound or a chunk.
  65.  
  66.   public void loadSound(char[] name) {
  67.     if (noSound) return;
  68.     char[] fileName = soundsDir ~ name;
  69.     music = Mix_LoadMUS(string.toStringz(fileName));
  70.     if (!music) {
  71.       noSound = true;
  72.       throw new SDLInitFailedException("Couldn't load: " ~ fileName ~ 
  73.                        " (" ~ string.toString(Mix_GetError()) ~ ")");
  74.     }
  75.   }
  76.   
  77.   public void loadChunk(char[] name, int ch) {
  78.     if (noSound) return;
  79.     char[] fileName = chunksDir ~ name;
  80.     chunk = Mix_LoadWAV(string.toStringz(fileName));
  81.     if (!chunk) {
  82.       noSound = true;
  83.       throw new SDLInitFailedException("Couldn't load: " ~ fileName ~ 
  84.                        " (" ~ string.toString(Mix_GetError()) ~ ")");
  85.     }
  86.     chunkChannel = ch;
  87.   }
  88.  
  89.   // Free a music or a chunk.
  90.   public void free() {
  91.     if (music) {
  92.       stopMusic();
  93.       Mix_FreeMusic(music);
  94.     }
  95.     if (chunk) {
  96.       haltChunk();
  97.       Mix_FreeChunk(chunk);
  98.     }
  99.   }
  100.  
  101.   // Play/Stop the music/chunk.
  102.  
  103.   public void playMusic() {
  104.     if (noSound) return;
  105.     Mix_PlayMusic(music, -1);
  106.   }
  107.  
  108.   public static void fadeMusic() {
  109.     if (noSound) return;
  110.     Mix_FadeOutMusic(fadeOutSpeed);
  111.   }
  112.  
  113.   public static void stopMusic() {
  114.     if (noSound) return;
  115.     if ( Mix_PlayingMusic() ) {
  116.       Mix_HaltMusic();
  117.     }
  118.   }
  119.  
  120.   public void playChunk() {
  121.     if (noSound) return;
  122.     Mix_PlayChannel(chunkChannel, chunk, 0);
  123.   }
  124.  
  125.   public void haltChunk() {
  126.     if (noSound) return;
  127.     Mix_HaltChannel(chunkChannel);
  128.   }
  129. }
  130.