vendredi 19 avril 2019

Passing data on events between classes in C++

I'm trying to write voice communicator in C++ as a school project. So far I've written two classes - one to record sound (Recorder) and one to play it (Player). At this point Recorder writes to file, which I later pass to Player. But now I would like to try to get it to work in real time. Inside Recorder there is loop that repeatedly reads 480 bytes of audio from sound card. Now each time audio data is ready I would like to send it to Player class to play it. Player just adds that data to sound card buffer, so it's as easy as calling playerObject.play(char* buffer, int bufferSize). I don't want to put one class inside another. How can I pass that data between them? Or call function of Player inside Recorder? (Later I will write Encoder and Decoder and will need to pass data between all those classes)

class Recorder {
    void play(char* audioData, int audioSize)
    {
        sendAudioToSoundCard(audioData, audioSize)
    }
};

class Player {
    void record()
    {
        const int dataSize = 480;
        char buffer[dataSize]
        while (1)
        {
            readAudioFromSoundCard(buffer, dataSize);
            sendAudioToPlay(buffer, dataSize); // how do I make this call play from Recorder? 
        }
    }
}

Aucun commentaire:

Enregistrer un commentaire