用于从麦克风捕获声音并调节音量的 API

发布于 2024-11-07 13:11:37 字数 205 浏览 0 评论 0原文

我尝试在 qt 上创建具有通过 GSM 调制解调器进行语音通话功能的应用程序。现在我正在寻找可以从微型捕获语音并能够调节语音音量的库。 Qt 的声子具有语音调节功能,但不具备从麦克风捕获数据的能力。

另一方面,QtMultimedia可以捕获此类数据,但无法调节音量。

是否有任何 C++ 库可以完成这两项任务,并且可以通过 win|mac|linux 进行移植?

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 both tasks, and is portable through win|mac|linux?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

西瓜 2024-11-14 13:11:37

最后我使用 QtMultimedia + 子类化 QIODevice 完成了此操作。

在输入和输出上,我具有带符号的 pcm 格式,因此我只需将输入\输出流的字节值与一些浮点值相乘即可设置音量级别,然后将数据写入我需要的设备。

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;
}

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;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文