Sign in to follow this  

SDL mixer channels

This topic is 3594 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hey I'm using SDL mixer to play sounds. I wrote up my own class to deal with sounds I'll post it at the bottom. .cpp first then .h file Basically I know nothing about sound channels and can't figure it out. At first I had these two pointers before I ever worried about channels: Mix_Chunk *chunk[8]; Mix_Music *music; All I have to do is pass a string to Playsong(char rgcsong[]) and it plays just fine. For short sounds I used chunk[8]. It's an 8 element array of Mix_Chunk pointers. That way I can store 8 different sounds and play them whenever I want without having to load them again. You can free a sound by calling Killsound(int ival) (ival should be 0-7) or if you're loading a different sound you can skip that and call Loadsound(char rgcsound[],int ival) and it will free the old sound and load the new one. Whenever you want to play one of the sounds just call Playsound(int ival,int iloop). ival says which of the 8 sounds and iloop says how many times to loop it. But then I added thunderstorms to my game so sound 0 was rain looped infinitely and sound 1 was thunder that clapped every time lightning struck and lit up the screen. But then I made it so thunderstorms could clear and when I killed the sounds the rain sound would stop abruptly and if thunder was clapping it would instantly stop and it sounds terrible. So I want to fade sounds. How do I do that? I started writing Fadesound(int ival,int ims) but gave up because I don't understand channels. I don't know what a channel is or how many I have. Can only one sound play in a channel? I've been passing -1 for channel all the time so are my sounds all automatically getting their own channel or do they all play in the same channel? If I specify a channel what does that even mean? Is there a channel 0 or channel 1? Is there a channel 10,000? I have no idea. Do I need to specify which channel thunder and rain play in instead of passing -1 and then fade out that specific channel? If so then what channel do I pick? Also does anyone know what this does? -> Mix_HaltChannel( 1 ) It's in all the examples but why is it halting channel 1? Can I just erase that line? I never messed with a channel 1 anyway. ///////////////.cpp file///////////////////////////////////////////
#include "soundmaster.h"

AudioBox::AudioBox()
{
  SDL_Init(SDL_INIT_AUDIO);
  music=NULL;
  for(ix=0;ix<8;ix++)
     chunk[ix]=NULL;

  // Initialize Audio sub system
  if ( SDL_InitSubSystem( SDL_INIT_AUDIO ) == -1 )
  {
    fprintf( stderr, "Could not initialize audio subsystem: %s\n", SDL_GetError( ) );
  }
  // Open the sound device
  //if ( Mix_OpenAudio( 22060, AUDIO_S16SYS, 2, 512 ) < 0 )
  if( Mix_OpenAudio( 44100, AUDIO_S16SYS, 2, 512 ) < 0 )
  {
     fprintf( stderr, "Unable to open audio: %s\n", SDL_GetError( ) );
  }
}

AudioBox::~AudioBox()
{
  //kill sounds
  Mix_HaltMusic( );
  Mix_FreeMusic( music );
  for(ix=0;ix<8;ix++)
  {
     if(chunk[ix]!=NULL)
        Mix_FreeChunk(chunk[ix]);
     chunk[ix]=NULL;
  }
  Mix_CloseAudio( );
  SDL_QuitSubSystem( SDL_INIT_AUDIO );
}

void AudioBox::Playsong(char rgcsong[])
{
   if(music!=NULL)
   {
      Mix_HaltChannel( 1 );  //what the hell is this
      Mix_FreeMusic(music);
      music = NULL;
   }
   music = Mix_LoadMUS(rgcsong);
   Mix_PlayMusic(music,-1);
}

//this is responsible for the slow startups?
void AudioBox::Loadsound(char rgcsound[],int ival)
{
      if (chunk[ival]!=NULL)
      {
         Mix_HaltChannel( 1 );  //???
         Mix_FreeChunk( chunk[ival] );
         chunk[ival]=NULL;
      }
      chunk[ival]=Mix_LoadWAV(rgcsound);
}

void AudioBox::Playsound(int  ival, int iloop)
{
  Mix_PlayChannel( -1, chunk[ival],iloop);
}

void AudioBox::Killsound(int ival)
{
   if(chunk[ival]!=NULL)
   {
      Mix_HaltChannel( 1 );  //what the hell is this
      Mix_FreeChunk( chunk[ival] );
      chunk[ival]=NULL;
   }
}

void AudioBox::Fadesound(int ival,int ims)
{
   Mix_FadeOutChannel(ival,ims);
}

////////////////////.h file///////////////////////
#ifndef SOUNDMASTER
#define SOUNDMASTER

//#include <stdlib.h>
//#include <GL/gl.h>
//#include <GL/glu.h>
//#include <fstream>
//#include <iostream>
#include "SDL.h"
#include "SDL_mixer.h"
//#include "Iceinteger.h"
//#include "Gizbox.h"
//#include "Picstack.h"
//#include "weather.h"
//#include "switchmaster.h"

extern int ix;

struct AudioBox
{
   AudioBox();
   ~AudioBox();
   void Playsong(char rgcsong[]);
   void Loadsound(char rgcsound[],int ival);
   void Playsound(int ival,int iloop);
   void Killsound(int ival);
   void Fadesound(int ival,int ims);

   //0 weather
   //1 weather
   Mix_Chunk *chunk[8];
   //Mix_Chunk *chunk2;
   Mix_Music *music;
};

#endif

Share this post


Link to post
Share on other sites
Have you read the documentation?

You call Mix_PlayChannel(-1, ....), and the -1 means 'any available channel'. If you changed that to use specific channels, then you could use HaltChannel to stop that sound. At any time, each channel can be playing one sound, and they are mixed together to create your output. There are supposedly 8 stereo channels.

To fade out a channel, you can use Mix_FadeOutChannel.

Share this post


Link to post
Share on other sites

This topic is 3594 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this