V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
moxiaowei
V2EX  ›  程序员

c++ mac 上使用 Q5.9 采集音频数据,然后采集到的是一堆杂音,不知道是什么情况

  •  
  •   moxiaowei · 2017-11-09 12:56:39 +08:00 · 1810 次点击
    这是一个创建于 2353 天前的主题,其中的信息可能已经有所发展或是发生改变。
    #include <iostream>

    #include "QtCore/QCoreApplication"

    #include "QtMultimedia/QAudioInput"

    extern "C"{

    #include "libswresample/swresample.h"

    #include "libavutil/channel_layout.h"//libavutil/channel_layout.h

    #include "libswscale/swscale.h"

    #include "libavcodec/avcodec.h"

    #include "libavutil/frame.h"

    #include "libavformat/avformat.h"

    }

    #include <iostream>

    #include <fstream>

    using namespace std;

    int main(int argc, const char * argv[]) {

    QAudioFormat fmt;

    fmt.setSampleRate(44100);

    fmt.setChannelCount(2);

    fmt.setSampleSize(16);

    fmt.setSampleType(QAudioFormat::UnSignedInt);

    fmt.setCodec("audio/pcm");

    fmt.setByteOrder(QAudioFormat::LittleEndian);

    QAudioDeviceInfo device = QAudioDeviceInfo::defaultInputDevice();

    if(!device.isFormatSupported(fmt)){

    std::cout << "设备不支持该类型的音频格式" << std:: endl;

    return -1;

    }

    QAudioInput *input = new QAudioInput(fmt);

    QIODevice *io = input->start();


    char *buf = new char[4096];

    while(true){

    if(input->bytesReady() < 4096) continue;

    int size = 0;

    while(size != 4096){

    int len = io->read(buf+size, 4096-size);

    if(len <0)break;

    size += len;

    }

    if(size != 4096) continue;


    std::fstream fsw("/Users/tangwei/Desktop/test.txt", std::ios::in | std::ios::out | std::ios::ate);//把原始数据写入到文件,判断原数据是否有问题

    if(!fsw)
    std::cout << "文件打开失败" << std::endl;


    fsw << buf << std::endl;


    fsw.close();

    }


    }
    代码如上
    1 条回复    2020-07-04 23:20:20 +08:00
    snana
        1
    snana  
       2020-07-04 23:20:20 +08:00
    解决了吗楼主,我 win 也是这样.
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2862 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 14:09 · PVG 22:09 · LAX 07:09 · JFK 10:09
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.