Jump to content

  • Log In with Google      Sign In   
  • Create Account


#Actualjohnmarinelli

Posted 29 January 2013 - 07:36 PM

Hi everyone,

 

So I've got a Piano that I'd like to be able to have the player use when they're near it.  The goal is to have them play a certain melody that unlocks the next piece of the puzzle.  Here's the code:

 

/*in c_Stereo.cpp*/

void c_Stereo::loadSFX(const char* fileName, std::string SFXname)
{
	Mix_Chunk *SFX = Mix_LoadWAV( fileName );
	SFX_list.insert( std::pair<std::string, Mix_Chunk*>(SFXname, SFX) );
}

void c_Stereo::play_loadedSFX(std::string SFXname)
{
	Mix_Chunk *SFX;

	std::map<std::string, Mix_Chunk*>::const_iterator results = SFX_list.find(SFXname);
	if ( results == SFX_list.end() ){
		SFX = NULL;
	}
	else
		SFX = results->second;

	Mix_PlayChannel(-1, SFX, 0);
}

/*in c_Piano.cpp*/
c_Piano::c_Piano(int x, int y){
...
/*piano is the name of the c_Stereo object inside c_Piano, sorry for any confusion! it just made sense to me*/
        piano.loadSFX("A.wav", "A");
	piano.loadSFX("B.wav", "B");
	piano.loadSFX("C.wav", "C");
	piano.loadSFX("D.wav", "D");
	piano.loadSFX("E.wav", "E");
	piano.loadSFX("F.wav", "F");
	piano.loadSFX("G.wav", "G");
}

void c_Piano::handleInput()
{
	if ( event.type == SDL_KEYDOWN )
	{
		switch (event.key.keysym.sym)
		{ 
		case SDLK_a: piano.play_loadedSFX( "A" ); break;
		case SDLK_b: piano.play_loadedSFX( "B" ); break;
		case SDLK_c: piano.play_loadedSFX( "C" ); break;
		case SDLK_d: piano.play_loadedSFX( "D" ); break;
		case SDLK_e: piano.play_loadedSFX( "E" ); break;
		case SDLK_f: piano.play_loadedSFX( "F" ); break;
		case SDLK_g: piano.play_loadedSFX( "G" ); break;
		}
	}
}


however, for some reason the program only allows any note to be played eight times.  for example, if I played "A" eight times, the piano wouldn't play any more notes.

I do know that c_Piano.handle_input() is still being performed after the 8 note limit  through some simple debugging.  Also, it seems like if I wait a few seconds, the piano will let me play another eight notes.

I have a suspicion it has something to do with the SDL_Mixer's channels, but I have no idea how to fix it.

 

Any insight would be appreciated, thanks!


#2johnmarinelli

Posted 29 January 2013 - 07:26 PM

Hi everyone,

 

So I've got a Piano that I'd like to be able to have the player use when they're near it.  The goal is to have them play a certain melody that unlocks the next piece of the puzzle.  Here's the code:

 

/*in c_Stereo.cpp*/

void c_Stereo::loadSFX(const char* fileName, std::string SFXname)
{
	Mix_Chunk *SFX = Mix_LoadWAV( fileName );
	SFX_list.insert( std::pair<std::string, Mix_Chunk*>(SFXname, SFX) );
}

void c_Stereo::play_loadedSFX(std::string SFXname)
{
	Mix_Chunk *SFX;

	std::map<std::string, Mix_Chunk*>::const_iterator results = SFX_list.find(SFXname);
	if ( results == SFX_list.end() ){
		SFX = NULL;
	}
	else
		SFX = results->second;

	Mix_PlayChannel(-1, SFX, 0);
}

/*in c_Piano.cpp*/
c_Piano::c_Piano(int x, int y){
...
/*piano is the name of the c_Stereo object inside c_Piano, sorry for any confusion! it just made sense to me*/
        piano.loadSFX("A.wav", "A");
	piano.loadSFX("B.wav", "B");
	piano.loadSFX("C.wav", "C");
	piano.loadSFX("D.wav", "D");
	piano.loadSFX("E.wav", "E");
	piano.loadSFX("F.wav", "F");
	piano.loadSFX("G.wav", "G");
}

void c_Piano::handleInput()
{
	if ( event.type == SDL_KEYDOWN )
	{
		switch (event.key.keysym.sym)
		{ 
		case SDLK_a: piano.play_loadedSFX( "A" ); break;
		case SDLK_b: piano.play_loadedSFX( "B" ); break;
		case SDLK_c: piano.play_loadedSFX( "C" ); break;
		case SDLK_d: piano.play_loadedSFX( "D" ); break;
		case SDLK_e: piano.play_loadedSFX( "E" ); break;
		case SDLK_f: piano.play_loadedSFX( "F" ); break;
		case SDLK_g: piano.play_loadedSFX( "G" ); break;
		}
	}
}


however, for some reason the program only allows any note to be played eight times.  for example, if I played "A" eight times, the piano wouldn't play any more notes.

I do know that c_Piano.handle_input() is still being performed through some simple debugging.  Also, it seems like if I wait a few seconds, the piano will let me play another eight notes.

I have a suspicion it has something to do with the SDL_Mixer's channels, but I have no idea how to fix it.

 

Any insight would be appreciated, thanks!


#1johnmarinelli

Posted 29 January 2013 - 07:24 PM

Hi everyone,

 

So I've got a Piano that I'd like to be able to have the player use when they're near it.  The goal is to have them play a certain melody that unlocks the next piece of the puzzle.  Here's the code:

 

/*in c_Stereo.cpp*/

void c_Stereo::loadSFX(const char* fileName, std::string SFXname)
{
	Mix_Chunk *SFX = Mix_LoadWAV( fileName );
	SFX_list.insert( std::pair<std::string, Mix_Chunk*>(SFXname, SFX) );
}

void c_Stereo::play_loadedSFX(std::string SFXname)
{
	Mix_Chunk *SFX;

	std::map<std::string, Mix_Chunk*>::const_iterator results = SFX_list.find(SFXname);
	if ( results == SFX_list.end() ){
		SFX = NULL;
	}
	else
		SFX = results->second;

	Mix_PlayChannel(-1, SFX, 0);
}

/*in c_Piano.cpp*/
c_Piano::c_Piano(int x, int y){
...
/*piano is the name of the c_Stereo object inside c_Piano, sorry for any confusion! it just made sense to me*/
        piano.loadSFX("A.wav", "A");
	piano.loadSFX("B.wav", "B");
	piano.loadSFX("C.wav", "C");
	piano.loadSFX("D.wav", "D");
	piano.loadSFX("E.wav", "E");
	piano.loadSFX("F.wav", "F");
	piano.loadSFX("G.wav", "G");
}

void c_Piano::handleInput()
{
	if ( event.type == SDL_KEYDOWN )
	{
		switch (event.key.keysym.sym)
		{ 
		case SDLK_a: piano.play_loadedSFX( "A" ); break;
		case SDLK_b: piano.play_loadedSFX( "B" ); break;
		case SDLK_c: piano.play_loadedSFX( "C" ); break;
		case SDLK_d: piano.play_loadedSFX( "D" ); break;
		case SDLK_e: piano.play_loadedSFX( "E" ); break;
		case SDLK_f: piano.play_loadedSFX( "F" ); break;
		case SDLK_g: piano.play_loadedSFX( "G" ); break;
		}
	}
}


however, for some reason the program only allows any note to be played eight times. 

I have a suspicion it has something to do with the SDL_Mixer's channels, but I have no idea how to fix it.

 

Any insight would be appreciated, thanks!


PARTNERS