Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Changeset 3179 in orxonox.OLD for orxonox/branches/sound/src/sound_control.cc


Ignore:
Timestamp:
Dec 15, 2004, 3:40:54 AM (19 years ago)
Author:
bensch
Message:

orxonox/branches/sound: added files to source again, made it compile for widnows again.
Simon, if you arrive at a point where it is doing something usefull, we inject it into the trunk.

File:
1 copied

Legend:

Unmodified
Added
Removed
  • orxonox/branches/sound/src/sound_control.cc

    r2972 r3179  
    1818using namespace std;
    1919
    20 // global variables
    21 SoundControl* SoundControl::instance = NULL; // singleton reference
     20int sfx_channel1 = -1;
     21int sfx_channel2 = -1;
     22int finished = 0;
     23SoundControl* SoundControl::sound = SoundControl::getInstance();
     24SoundControl* SoundControl::instance = 0;
    2225int volume = SDL_MIX_MAXVOLUME;
    23 int done = 0;
    2426int track_number = 1;
    25 static Mix_Music* music = NULL;
    26 int audio_rate = MIX_DEFAULT_FREQUENCY, audio_channels = MIX_DEFAULT_CHANNELS, audio_buffers = 16384, bits = 0;
     27Mix_Music* music = NULL;
     28int audio_rate = 44100, audio_channels = MIX_DEFAULT_CHANNELS,
     29audio_buffers = 16384, bits = 0;
    2730Uint16 audio_format = MIX_DEFAULT_FORMAT;
     31SDL_Event event;
    2832
    2933
    3034/**
    3135    \brief standard constructor
    32    
    33     This constructor builds a SoundControl Object, which waits for callers.
     36    This constructor builds a SoundControl Object and initialises it .
    3437    All sound output is handled by this singleton object.
    3538*/
    36 SoundControl::SoundControl () {
    37 
    38   /*
    39   initializing sound and calling Mix_OpenAudio
    40   if(SDL_Init(SDL_INIT_AUDIO)<0){
     39SoundControl::SoundControl() {
     40  if(SDL_Init(SDL_INIT_AUDIO)<0) {
    4141    printf("SDL_Init: INIT_AUDIO error.\n");
    4242  }
    43   */
    44  
    45   if(Mix_OpenAudio(audio_rate, audio_format, audio_channels, audio_buffers)){
     43  if(Mix_OpenAudio(audio_rate, audio_format, audio_channels, audio_buffers)) {
    4644    printf("Mix_OpenAudio: Failed to open audio!\n");
    4745  }
    48  
    4946  initialise();
    5047}
    5148
    52 
    5349/**
    5450    \brief Default destructor
    5551*/
    56 SoundControl::~SoundControl () {
     52SoundControl::~SoundControl() {
    5753}
    5854
    5955/**
    60    \brief Returns a reference to the singleton
     56   \brief Returns a reference to the SoundControl singleton
    6157*/
    6258SoundControl* SoundControl::getInstance() {
    63   if (instance == NULL) {
     59  if (instance == 0) {
    6460    instance = new SoundControl;
    6561  }
     
    6864
    6965void SoundControl::deleteInstance() {
    70   delete instance;
    71   instance = NULL;
    72 }
    73 
    74 /**
    75     \brief Is called by SoundControl object to initiate all values
     66}
     67
     68/**
     69    \brief Is called by SoundControl object to initiate all values and to output some text
    7670*/
    7771void SoundControl::initialise() {
    78  
    79   // Print some info
    8072  Mix_QuerySpec(&audio_rate, &audio_format, &audio_channels);
    8173  bits=audio_format&0xFF;
     
    8476}
    8577
    86 
    87 /**
    88     \brief Sets the number of output Channels (should not be used)
    89 */
    90 void SoundControl::setNumberOfChannels (int number_of_channels) {
     78/**
     79    \brief Sets the number of output Channels
     80*/
     81void SoundControl::setNumberOfChannels(int number_of_channels) {
    9182  Mix_AllocateChannels(number_of_channels);
    9283}
    9384
    94 
    95 /**
    96     \brief May be called from any WorldEntity to play a .xm file
     85/**
     86    \brief Static function to play a .xm file
    9787    \param filename: self-explanatory
    9888*/
    99 int SoundControl::playMod (char* fileName) {
     89void SoundControl::playMod(char* fileName) {
    10090  Mix_Chunk* chunk = NULL;
    10191  chunk = Mix_LoadWAV(fileName);
     
    10595}
    10696
    107 
    108 /**
    109     \brief May be called from any WorldEntity to play a .wav file
     97/**
     98    \brief Static function to play a .wav file
    11099    \param filename: self-explanatory
    111100*/
    112 int SoundControl::playWav (char* fileName) {
     101void SoundControl::playWav(char* fileName) {
    113102  Mix_Chunk* chunk = NULL;
    114103  chunk = Mix_LoadWAV(fileName);
     
    118107}
    119108
    120 
    121 /**
    122     \brief May be called from any WorldEntity to play a .ogg file
     109/**
     110    \brief Static function to play an .ogg file
    123111    \param filename: self-explanatory
    124112*/
    125 int SoundControl::playOgg (char* fileName) {
     113void SoundControl::playOgg(char* fileName) {
    126114  Mix_Music* music = NULL;
    127115  music = Mix_LoadMUS(fileName);
    128   if(Mix_PlayMusic(music, 1) == -1){
     116  if(Mix_PlayMusic(music, 1) == -1) {
    129117    printf("Mix_PlayMusic: %s\n",Mix_GetError());
    130118  }
     
    132120}
    133121
    134 
    135122/**
    136123    \brief Heightens the overall volume of output
    137124*/
    138 void SoundControl::volumeUp () {
     125void SoundControl::volumeUp() {
    139126  volume = (volume + 1) << 1;
    140127  if(volume > SDL_MIX_MAXVOLUME)
     
    147134    \brief Lowers the overall volume of output
    148135*/ 
    149 void SoundControl::volumeDown () {
     136void SoundControl::volumeDown() {
    150137  volume >>= 1;
    151138  Mix_VolumeMusic(volume);
    152139}
    153140
    154 
    155141/**
    156142    \brief Rewinds music to the beginning
    157143*/ 
    158 void SoundControl::trackRewind () {
     144void SoundControl::trackRewind() {
    159145  Mix_RewindMusic();
    160146}
    161147
    162 
    163148/**
    164149    \brief Rewinds the music 5 seconds
    165150*/ 
    166 void SoundControl::forwardMusic () {
     151void SoundControl::forwardMusic() {
    167152  Mix_SetMusicPosition(+5);
    168153}
    169 
    170154
    171155/**
     
    176160}
    177161
    178 
    179162/**
    180163    \brief Pauses music output
    181164*/
    182 void SoundControl::pauseMusic () {
     165void SoundControl::pauseMusic() {
    183166  Mix_PauseMusic();
    184167}
    185168
    186 
    187 /**
    188     \brief this function pauses music output
    189 */
    190 void SoundControl::resumeMusic () {
     169/**
     170    \brief Pauses music output
     171*/
     172void SoundControl::resumeMusic() {
    191173  Mix_ResumeMusic();
    192174}
    193175
    194176/**
    195     \brief Selects the track of all orxonox tracks
    196 */
    197 void SoundControl::trackSelect() {
    198   switch (track_number) {
    199   case 1:
    200     music = Mix_LoadMUS("luke_grey_orxonox1.ogg");
    201     if(Mix_PlayMusic(music, 1) == -1){
    202       printf("Mix_PlayMusic: %s\n",Mix_GetError());
    203     }
    204     Mix_HookMusicFinished(musicDone);
    205     break;
    206   case 2:
    207     music = Mix_LoadMUS("luke_grey_orxonox2.ogg");
    208     if(Mix_PlayMusic(music, 1) == -1){
    209       printf("Mix_PlayMusic: %s\n",Mix_GetError());
    210     }
    211     Mix_HookMusicFinished(musicDone);
    212     break;
    213   case 3:
    214     music = Mix_LoadMUS("luke_grey_orxonox3.ogg");
    215     if(Mix_PlayMusic(music, 1) == -1){
    216       printf("Mix_PlayMusic: %s\n",Mix_GetError());
    217     }
    218     Mix_HookMusicFinished(musicDone);   
    219     break;
    220   case 4:
    221     music = Mix_LoadMUS("luke_grey_and_aquarius_orxonox.ogg");
    222     if(Mix_PlayMusic(music, 1) == -1){
    223       printf("Mix_PlayMusic: %s\n",Mix_GetError());
    224     }
    225     Mix_HookMusicFinished(musicDone);   
    226     break;
    227   case 5:
    228     music = Mix_LoadMUS("nomenes_orxonox.ogg");
    229     if(Mix_PlayMusic(music, 1) == -1){
    230       printf("Mix_PlayMusic: %s\n",Mix_GetError());
    231     }
    232     Mix_HookMusicFinished(musicDone);   
    233     break;
    234   case 6:
    235     music = Mix_LoadMUS("nomenes_funkadudu.ogg");
    236     if(Mix_PlayMusic(music, 1) == -1){
    237       printf("Mix_PlayMusic: %s\n",Mix_GetError());
    238     }
    239     Mix_HookMusicFinished(musicDone);
    240     break;
    241   }
    242 }
    243 
     177    \brief Fades in music
     178*/
     179void fadeInMusic(int time) {
     180
     181}
     182
     183/**
     184    \brief Fades out music
     185*/
     186void SoundControl::fadeOutMusic(int time) {
     187
     188}
    244189
    245190/**
     
    251196  music = NULL;
    252197}
     198
     199/**
     200    \brief Handles input events
     201*/
     202void SoundControl::handleKey(SDL_KeyboardEvent key) {
     203  switch(key.keysym.sym) {
     204  case SDLK_a:
     205    if(key.type == SDL_KEYDOWN) {
     206      if(sfx_channel1 < 0) {
     207        sfx_channel1 = 1;
     208        sound->playWav("sound1.wav");
     209      }
     210    } else {
     211      Mix_HaltChannel(sfx_channel1);
     212      sfx_channel1 = -1;
     213    }
     214    break;
     215  case SDLK_s:
     216    if(key.type == SDL_KEYDOWN) {
     217      if(sfx_channel2 < 0) {
     218        sfx_channel2 = 1;
     219        sound->playWav("sound2.wav");
     220      }
     221    } else {
     222      Mix_HaltChannel(sfx_channel2);
     223      sfx_channel2 = -1;
     224    }
     225    break;
     226  case SDLK_m:
     227    if(key.state == SDL_PRESSED) {
     228      sound->playOgg("music.ogg");
     229    }
     230    break;
     231  case SDLK_q:
     232    finished = 1;
     233    break;
     234  default:
     235    break;
     236  }
     237}
     238
     239int SoundControl::main(int argc, char* argv[]) {
     240  SDL_Surface* screen;
     241  SDL_Init(SDL_INIT_VIDEO|SDL_INIT_AUDIO);
     242  screen = SDL_SetVideoMode(320, 240, 0, 0);
     243  while(!finished) {
     244    while(SDL_PollEvent(&event)) {
     245      switch(event.type) {
     246      case SDL_QUIT:
     247        finished = 1;
     248        break;
     249      case SDL_KEYDOWN:
     250      case SDL_KEYUP:
     251        SoundControl::handleKey(event.key);
     252        break;
     253      default:
     254        break;
     255      }
     256    }
     257    SDL_Delay(50);
     258  }
     259  deleteInstance();
     260  SDL_Quit();
     261  return 0;
     262}
Note: See TracChangeset for help on using the changeset viewer.