草庐IT

c++ - Portaudio + 作品 : Horrible sound quality

coder 2024-02-22 原文

我目前正在开发一个 VOIP 应用程序。为此,我使用 PortAudio 库检索和播放声音,并使用 Opus 库编码和解码声音数据包。

目前,我成功地使用了 PortAudio。我的程序只是做:

  1. 从麦克风获取声音
  2. 播放声音

音质绝对好。

我现在正在尝试对声音数据包进行编码和解码。我编写了一个 EncodeManagerClass 来执行此操作,我的程序现在执行以下操作:

  1. 从麦克风获取声音
  2. 编码声音
  3. 解码
  4. 回放

但现在,音质绝对糟糕(而且在 VOIP 应用程序中显然是有问题的)。

这是我的 EncodeManager 类:

class EncodeManager {

    // ctor - dtor
    public:
        EncodeManager(void);
        ~EncodeManager(void);

    // coplien form
    private:
        EncodeManager(const EncodeManager &) {}
        const EncodeManager &operator=(const EncodeManager &) { return *this; }

    // encode - decode
    public:
        Sound::Encoded  encode(const Sound::Decoded &sound);
        Sound::Decoded  decode(const Sound::Encoded &sound);

    // attributes
    private:
        OpusEncoder *mEncoder;
        OpusDecoder *mDecoder;

};

这是源文件:

EncodeManager::EncodeManager(void) {
    int error;

    mEncoder = opus_encoder_create(Sound::SAMPLE_RATE, Sound::NB_CHANNELS, OPUS_APPLICATION_VOIP, &error);
    if (error != OPUS_OK)
        throw new SoundException("fail opus_encoder_create");

    mDecoder = opus_decoder_create(Sound::SAMPLE_RATE, Sound::NB_CHANNELS, &error);
    if (error != OPUS_OK)
        throw new SoundException("fail opus_decoder_create");
}

EncodeManager::~EncodeManager(void) {
    if (mEncoder)
        opus_encoder_destroy(mEncoder);

    if (mDecoder)
        opus_decoder_destroy(mDecoder);
}

Sound::Encoded  EncodeManager::encode(const Sound::Decoded &sound) {
    Sound::Encoded encoded;

    encoded.buffer = new unsigned char[sound.size];
    encoded.size = opus_encode_float(mEncoder, sound.buffer, Sound::FRAMES_PER_BUFFER, encoded.buffer, sound.size);

    if (encoded.size < 0)
        throw new SoundException("fail opus_encode_float");

    return encoded;
}

Sound::Decoded  EncodeManager::decode(const Sound::Encoded &sound) {
    Sound::Decoded decoded;

    decoded.buffer = new float[Sound::FRAMES_PER_BUFFER * Sound::NB_CHANNELS];
    decoded.size = opus_decode_float(mDecoder, sound.buffer, sound.size, decoded.buffer, Sound::FRAMES_PER_BUFFER, 0);

    if (decoded.size < 0)
        throw new SoundException("fail opus_decode_float");

    return decoded;
}

这是我的主要内容:

int main(void) {
    SoundInputDevice input;
    SoundOutputDevice output;
    EncodeManager encodeManager;

    input.startStream();
    output.startStream();

    while (true) {
        Sound::Decoded *sound;

        input >> sound;
        if (sound) {
            Sound::Encoded encodedSound = encodeManager.encode(*sound);
            Sound::Decoded decodedSound = encodeManager.decode(encodedSound);
            output << &decodedSound;
        }
    }

    return 0;
}

附加信息:

const int   SAMPLE_RATE = 48000;
const int   NB_CHANNELS = 2;
const int   FRAMES_PER_BUFFER = 480;

我已经尝试使用 opus_encode_ctl(带宽、比特率、VBR)配置 opus 编码器,但它根本不起作用:音质仍然很差。

即使我更改 SAMPLE_RATE 或 FRAME_PER_BUFFER,音质也不会提高...

我是否遗漏了有关 PortAudio/Opus 的内容?

最佳答案

我终于找到了解决办法。

问题不是来自 Opus,而是来自主要...

if (sound) {
  Sound::Encoded encodedSound = encodeManager.encode(*sound);
  Sound::Decoded decodedSound = encodeManager.decode(encodedSound);
  output << &decodedSound;
 }

在这里,我将一个局部变量传递到我的输出流。但是输出流是异步工作的:所以我的变量在播放声音打包之前就被销毁了。

使用指针是解决问题最简单的方法。我个人决定稍微重构一下我的代码。

关于c++ - Portaudio + 作品 : Horrible sound quality,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26401807/

有关c++ - Portaudio + 作品 : Horrible sound quality的更多相关文章

  1. ruby-on-rails - 如何优雅地重启 thin + nginx? - 2

    我的瘦服务器配置了nginx,我的ROR应用程序正在它们上运行。在我发布代码更新时运行thinrestart会给我的应用程序带来一些停机时间。我试图弄清楚如何优雅地重启正在运行的Thin实例,但找不到好的解决方案。有没有人能做到这一点? 最佳答案 #Restartjustthethinserverdescribedbythatconfigsudothin-C/etc/thin/mysite.ymlrestartNginx将继续运行并代理请求。如果您将Nginx设置为使用多个上游服务器,例如server{listen80;server

  2. ruby - 使用 `+=` 和 `send` 方法 - 2

    如何将send与+=一起使用?a=20;a.send"+=",10undefinedmethod`+='for20:Fixnuma=20;a+=10=>30 最佳答案 恐怕你不能。+=不是方法,而是语法糖。参见http://www.ruby-doc.org/docs/ProgrammingRuby/html/tut_expressions.html它说Incommonwithmanyotherlanguages,Rubyhasasyntacticshortcut:a=a+2maybewrittenasa+=2.你能做的最好的事情是:

  3. ruby - 如何计算 Liquid 中的变量 +1 - 2

    我对如何计算通过{%assignvar=0%}赋值的变量加一完全感到困惑。这应该是最简单的任务。到目前为止,这是我尝试过的:{%assignamount=0%}{%forvariantinproduct.variants%}{%assignamount=amount+1%}{%endfor%}Amount:{{amount}}结果总是0。也许我忽略了一些明显的东西。也许有更好的方法。我想要存档的只是获取运行的迭代次数。 最佳答案 因为{{incrementamount}}将输出您的变量值并且不会影响{%assign%}定义的变量,我

  4. arrays - Ruby 数组 += vs 推送 - 2

    我有一个数组数组,想将元素附加到子数组。+=做我想做的,但我想了解为什么push不做。我期望的行为(并与+=一起工作):b=Array.new(3,[])b[0]+=["apple"]b[1]+=["orange"]b[2]+=["frog"]b=>[["苹果"],["橙子"],["Frog"]]通过推送,我将推送的元素附加到每个子数组(为什么?):a=Array.new(3,[])a[0].push("apple")a[1].push("orange")a[2].push("frog")a=>[[“苹果”、“橙子”、“Frog”]、[“苹果”、“橙子”、“Frog”]、[“苹果”、“

  5. += 的 Ruby 方法 - 2

    有没有办法让Ruby能够做这样的事情?classPlane@moved=0@x=0defx+=(v)#thisiserror@x+=v@moved+=1enddefto_s"moved#{@moved}times,currentxis#{@x}"endendplane=Plane.newplane.x+=5plane.x+=10putsplane.to_s#moved2times,currentxis15 最佳答案 您不能在Ruby中覆盖复合赋值运算符。任务在内部处理。您应该覆盖+,而不是+=。plane.a+=b与plane.a=

  6. ruby - Sinatra + Heroku + Datamapper 使用 dm-sqlite-adapter 部署问题 - 2

    出于某种原因,heroku尝试要求dm-sqlite-adapter,即使它应该在这里使用Postgres。请注意,这发生在我打开任何URL时-而不是在gitpush本身期间。我构建了一个默认的Facebook应用程序。gem文件:source:gemcuttergem"foreman"gem"sinatra"gem"mogli"gem"json"gem"httparty"gem"thin"gem"data_mapper"gem"heroku"group:productiondogem"pg"gem"dm-postgres-adapter"endgroup:development,:t

  7. ruby - Ruby 中字符串运算符 + 和 << 的区别 - 2

    我是Ruby和这个网站的新手。下面两个函数是不同的,一个在函数外修改变量,一个不修改。defm1(x)x我想确保我理解正确-当调用m1时,对str的引用被复制并传递给将其视为x的函数。运算符当调用m2时,对str的引用被复制并传递给将其视为x的函数。运算符+创建一个新字符串,赋值x=x+"4"只是将x重定向到新字符串,而原始str变量保持不变。对吧?谢谢 最佳答案 String#+::str+other_str→new_strConcatenation—ReturnsanewStringcontainingother_strconc

  8. ruby - rails 3.2.2(或 3.2.1)+ Postgresql 9.1.3 + Ubuntu 11.10 连接错误 - 2

    我正在使用PostgreSQL9.1.3(x86_64-pc-linux-gnu上的PostgreSQL9.1.3,由gcc-4.6.real(Ubuntu/Linaro4.6.1-9ubuntu3)4.6.1,64位编译)和在ubuntu11.10上运行3.2.2或3.2.1。现在,我可以使用以下命令连接PostgreSQLsupostgres输入密码我可以看到postgres=#我将以下详细信息放在我的config/database.yml中并执行“railsdb”,它工作正常。开发:adapter:postgresqlencoding:utf8reconnect:falsedat

  9. ruby - 在 Ruby + Chef 中检查现有目录失败 - 2

    这是我在ChefRecipe中的一blockRuby:#ifdatadirdoesn'texist,moveoverthedefaultoneif!File.exist?("/vol/postgres/data")execute"mv/var/lib/postgresql/9.1/main/vol/postgres/data"end结果是:Executingmv/var/lib/postgresql/9.1/main/vol/postgres/datamv:inter-devicemovefailed:`/var/lib/postgresql/9.1/main'to`/vol/post

  10. iNFTnews | 周杰伦18年前未发布的作品Demo,藏在了区块链技术里 - 2

    当音乐碰上区块链技术,会擦出怎样的火花?或许周杰伦已经给了我们答案。8月29日下午,B站独家首发周杰伦限定珍藏Demo独家访谈VCR,周杰伦在VCR里分享了《晴天》《青花瓷》《搁浅》《爱在西元前》四首经典歌曲Demo背后的创作故事,并首次公布18年前未发布的神秘作品《纽约地铁》的Demo。在VCR中,方文山和杰威尔音乐提及到“多亏了区块链技术,现在我们可以将这些Demos,变成独一无二具有收藏价值的艺术品,这些Demos可以在薄盒(国内数藏平台)上听到。”如何将音乐与区块链技术相结合,薄盒方面称:“薄盒作为区块链技术服务方,打破传统对于区块链技术只能作为数字收藏的理解。聚焦于区块链技术赋能,在

随机推荐