The Man
The Man

Reputation: 427

How can I capture audio signal from two channels with QAudioInput

I want to capture sound from my audio device with QAudioInput.I have a stereo (Two channels) input signal, normally I just have call the function setChannelCount() with the number of my channels, in this case 2. my goal is to save the signal from channel1 in file 1 and signal from channel 2 in file 2. my problem is how to separate the both signals.

Upvotes: 1

Views: 689

Answers (1)

jaket
jaket

Reputation: 9341

Judging from the documentation of QAudioInput, you would need your own specialization of QIODevice in which you would override writeData in order to fan the data out into 2 files. I haven't tried to compile this but it should give you the general idea.

class MyIODevice : public QIODevice
{
public:
    MyIODevice(QIODevice* file1, QIODevice* file2)
      : m_file1(file1), m_file2(file2) {}

    qint64 writeData(const char* data, qint64 maxSize)
    {
        // for 32-bits with the channels interleaved.
        // I'm also making an assumption that QAudioInput will call this with
        // a complete frame of data.
        for (int i = 0; i < maxSize / 8; ++i)
        {
            m_file1->writeData(data, 4);
            data += 4;
            m_file2->writeData(data, 4);
            data += 4;
        }
    }
private:
    QIODevice* m_file1;
    QIODevice* m_file2;
};

As written this would only work with one audio format but could easily be made more general by passing some parameters about the format to the constructor, etc...

Upvotes: 2

Related Questions