Click here to Skip to main content
15,886,873 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
Hello im working on an audio project and i have 3 classes ( one for playing mp3 , classe for playing wav and classe for playing autogenerated soud) the problem is that in my project i must be able to choose many types of these thre sounds and play them at the same time ( maybé several sounds of same type ) the idea is creating linked chain but is it possible to create a linked chain of classes ?
thanks for help
Posted

1 solution

The best way to do this would be to have your 3 classes be subclasses of a GenericAudio class:

C++
class GenericAudio 
{ 
    virtual void play_audio() abstract;
}

class MP3Audio : public GenericAudio
{
    virtual void play_audio();
    ...
}

class WavAudio : public GenericAudio
{
    virtual void play_audio();
    ...
}

class SelfGenAudio : public GenericAudio
{
     virtual void play_audio();
     ...
}


Then your linked list can be a linked list of GenericAudio's and you can call play_audio() on each object without caring what specific sub-class it is.

Any other functionality that is common to the three classes you should also declare as virtual in GenericAudio, so you just write all the rest of your code to use a GenericAudio object.

If for some reason you think you need to know what sub-type some GenericAudio is, you can always test for it -- but probably that means you need to add another virtual function to GenericAudio so that you don't have to care.

Doing it that way, if you find you need to add another audio type later, you just add another sub-class of GenericAudio and all the rest of the code just works.
 
Share this answer
 
Comments
Sergey Chepurin 11-Jun-11 7:30am    
Very good solution indeed.
kinani 21-Jun-11 9:28am    
hello thanks fo the interesting solution another problem is that the play fuction is not the same for the 3 sound types ( different parameters) what's the solution to this problem ? thanks again
TRK3 21-Jun-11 15:07pm    
Depends on the parameters.

Let's say one of them is volume, but it's an integer between 0 and 100 for one type and a float between 0 and 1.0 for another, and it's not an option on the third.

Then play_audio you decide to have a volume parameter that is a float between 0 and 1.0. In the first class, you multiple that number by 100 and pass it to your underlying player. In your second class, you pass the number as is, and in the third class, you just ignore the parameter.

Let's say your MP3 play function takes a filename and your SelfGenAudio takes a buffer and a length. You just make those member variables of the respective classes that get set when you create that particular object:

GenericAudio *pAudio1 = new MP3Audio(filename);
GenericAudio *pAudio2 = new SelfGenAudio(buffer, length);

Then in play_audio, you use the filename or buffer/length member variables and pass them to the underlying audio play functin.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900