home *** CD-ROM | disk | FTP | other *** search
/ GameStar Special 2004 August / GSSH0804.iso / Action / TumikiFighters / tf0_2.exe / tf / src / abagames / util / sdl / sound.d < prev    next >
Text File  |  2004-05-15  |  4KB  |  168 lines

  1. /*
  2.  * $Id: sound.d,v 1.3 2004/05/14 14:35:39 kenta Exp $
  3.  *
  4.  * Copyright 2004 Kenta Cho. All rights reserved.
  5.  */
  6. module abagames.util.sdl.sound;
  7.  
  8. private import std.string;
  9. private import SDL;
  10. private import SDL_mixer;
  11. private import abagames.util.sdl.sdlexception;
  12.  
  13. /**
  14.  * BGM/SE.
  15.  */
  16. public abstract class Sound {
  17.  public:
  18.   static bool noSound = false;
  19.  private:
  20.  
  21.   public static void init() {
  22.     if (noSound)
  23.       return;
  24.     int audio_rate;
  25.     Uint16 audio_format;
  26.     int audio_channels;
  27.     int audio_buffers;
  28.     if (SDL_InitSubSystem(SDL_INIT_AUDIO) < 0) {
  29.       noSound = 1;
  30.       throw new SDLInitFailedException
  31.     ("Unable to initialize SDL_AUDIO: " ~ std.string.toString(SDL_GetError()));
  32.     }
  33.     audio_rate = 44100;
  34.     audio_format = AUDIO_S16;
  35.     audio_channels = 1;
  36.     audio_buffers = 4096;
  37.     if (Mix_OpenAudio(audio_rate, audio_format, audio_channels, audio_buffers) < 0) {
  38.       noSound = 1;
  39.       throw new SDLInitFailedException
  40.     ("Couldn't open audio: " ~ std.string.toString(SDL_GetError()));
  41.     }
  42.     Mix_QuerySpec(&audio_rate, &audio_format, &audio_channels);
  43.   }
  44.  
  45.   public static void close() {
  46.     if (noSound)
  47.       return;
  48.     if (Mix_PlayingMusic()) {
  49.       Mix_HaltMusic();
  50.     }
  51.     Mix_CloseAudio();
  52.   }
  53.  
  54.   public abstract void load(char[] name);
  55.   public abstract void load(char[] name, int ch);
  56.   public abstract void free();
  57.   public abstract void play();
  58.   public abstract void fade();
  59.   public abstract void halt();
  60. }
  61.  
  62. public class Music: Sound {
  63.  public:
  64.   static int fadeOutSpeed = 1280;
  65.   static char[] dir = "sounds/";
  66.  private:
  67.   Mix_Music* music;
  68.  
  69.   public void load(char[] name) {
  70.     if (noSound)
  71.       return;
  72.     char[] fileName = dir ~ name;
  73.     music = Mix_LoadMUS(std.string.toStringz(fileName));
  74.     if (!music) {
  75.       noSound = true;
  76.       throw new SDLInitFailedException("Couldn't load: " ~ fileName ~ 
  77.                        " (" ~ std.string.toString(Mix_GetError()) ~ ")");
  78.     }
  79.   }
  80.   
  81.   public void load(char[] name, int ch) {
  82.     load(name);
  83.   }
  84.  
  85.   public void free() {
  86.     if (music) {
  87.       halt();
  88.       Mix_FreeMusic(music);
  89.     }
  90.   }
  91.  
  92.   public void play() {
  93.     if (noSound) return;
  94.     Mix_PlayMusic(music, -1);
  95.   }
  96.  
  97.   public void playOnce() {
  98.     if (noSound) return;
  99.     Mix_PlayMusic(music, 1);
  100.   }
  101.  
  102.   public void fade() {
  103.     Music.fadeMusic();
  104.   }
  105.  
  106.   public void halt() {
  107.     Music.haltMusic();
  108.   }
  109.  
  110.   public static void fadeMusic() {
  111.     if (noSound) return;
  112.     Mix_FadeOutMusic(fadeOutSpeed);
  113.   }
  114.  
  115.   public static void haltMusic() {
  116.     if (noSound) return;
  117.     if (Mix_PlayingMusic()) {
  118.       Mix_HaltMusic();
  119.     }
  120.   }
  121. }
  122.  
  123. public class Chunk: Sound {
  124.  public:
  125.   static char[] dir = "sounds/";
  126.  private:
  127.   Mix_Chunk* chunk;
  128.   int chunkChannel;
  129.  
  130.   public void load(char[] name) {
  131.     load(name, 0);
  132.   }
  133.   
  134.   public void load(char[] name, int ch) {
  135.     if (noSound)
  136.       return;
  137.     char[] fileName = dir ~ name;
  138.     chunk = Mix_LoadWAV(std.string.toStringz(fileName));
  139.     if (!chunk) {
  140.       noSound = true;
  141.       throw new SDLInitFailedException("Couldn't load: " ~ fileName ~ 
  142.                        " (" ~ std.string.toString(Mix_GetError()) ~ ")");
  143.     }
  144.     chunkChannel = ch;
  145.   }
  146.  
  147.   public void free() {
  148.     if (chunk) {
  149.       halt();
  150.       Mix_FreeChunk(chunk);
  151.     }
  152.   }
  153.  
  154.   public void play() {
  155.     if (noSound) return;
  156.     Mix_PlayChannel(chunkChannel, chunk, 0);
  157.   }
  158.  
  159.   public void halt() {
  160.     if (noSound) return;
  161.     Mix_HaltChannel(chunkChannel);
  162.   }
  163.  
  164.   public void fade() {
  165.     halt();
  166.   }
  167. }
  168.