开发者

Api for capturing voice from microphone with volume regulation

I trying to create application on qt with capability of making voice calls through GSM modem. Now i'm searching for library which can capture voice from micro and has ability to regulate voice volume. Qt's phonon have voice regulation, but does not have ability to capture data from microphone.

From the other side, there is QtMultimedia, which can capture such data but could not regulate volume.

Is there any lib for c++ which could do 开发者_StackOverflowboth tasks, and is portable through win|mac|linux?


finally I done this with QtMultimedia + subclassing QIODevice.

On input and output I have pcm format, signed, so I just multiply byte value of input\output stream with some float value to set volume level, after that I write data to device i need.

class VOICEIO_EXPORT VoiceIOAdapter : public QIODevice
{
    Q_OBJECT

public:
    VoiceIOAdapter(QObject *parent,QIODevice* dev);
    VoiceIOAdapter(QObject *parent);
    ~VoiceIOAdapter();
    void setUnderlyingDevice(QIODevice* dev);
    QIODevice* getUnderlyingDevice() const;
        ... /*some qiodevice virtual functions reimplemented*/
        ...
    float getOutVolume()const;
    float getInVolume() const;
public slots:
    void setOutVolume(float);
    void setInVolume(float);
protected:
    QIODevice* underlyingDevice;
    virtual qint64  readData ( char * data, qint64 maxSize );
    virtual qint64  writeData ( const char * data, qint64 maxSize );
    void applyVolume(float value, QByteArray& input);
    float outVolume;//1
    float inVolume;//1
};


void VoiceIOAdapter::applyVolume(float value, QByteArray& input)
{
    Q_ASSERT(!(input.size()%2)); //SHORT,Signed,LE
    qint16* data=reinterpret_cast<qint16*>(input.data());
    qint32 tmp;
    for(int i=0;i<input.size()/2;i++)
    {
        tmp=qint32(float(data[i])*value);
        if(tmp>std::numeric_limits<qint16>::max())
        {
            tmp=std::numeric_limits<qint16>::max();
        }
        else if (tmp<std::numeric_limits<qint16>::min())
        {
            tmp=std::numeric_limits<qint16>::min();
        }
        data[i]=tmp;
    }
}

qint64  VoiceIOAdapter::readData ( char * data, qint64 maxSize )
{
    if(!underlyingDevice)
    {
        return -1;
    }
        QByteArray decoded(maxSize ,0);
    qint64 result=underlyingDevice->read(decoded.data(),maxSize);
    if(result>0)
    {
                decoded.resize(result);
        applyVolume(inVolume,decoded);
        memcpy(data,decoded.data(),decoded.size());
    }
    return result;
}

qint64  VoiceIOAdapter::writeData ( const char * data, qint64 maxSize )
{
    if(!underlyingDevice)
    {
        return -1;
    }
    QByteArray encoded(data,maxSize);
    applyVolume(outVolume,encoded);
    qint64 result=underlyingDevice->write(encoded.data(),maxSize );
    return result;
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜