Qt調(diào)用aplay播放PCM

今天接到個任務(wù),讓我用Qt播放PCM,這個簡單,隨便一百度就有了。

#ifndef PLAYPCM_H
#define PLAYPCM_H

#include <QObject>
#include <QAudioOutput>
#include <QIODevice>

class PlayPCM : public QObject
{
    Q_OBJECT
    QAudioOutput *audioOutput = nullptr;
    QIODevice *streamOut = nullptr;
#endif

public:
    explicit PlayPCM(int sampleRate = 44100, int channels = 2, int sampleSize = 16, QObject *parent = nullptr);
    ~PlayPCM();
signals:

public slots:
    void writePCM(QByteArray array);
};
#endif // PLAYPCM_H

#include "playpcm.h"
#include <QDebug>
#include <QAudioFormat>
#include <QAudioDeviceInfo>

PlayPCM::PlayPCM(int sampleRate, int channels, int sampleSize, QObject *parent) : QObject(parent)
{
    //設(shè)置采樣格式
    QAudioFormat audioFormat;
    //設(shè)置采樣率
    audioFormat.setSampleRate(sampleRate);
    //設(shè)置通道數(shù)
    audioFormat.setChannelCount(channels);
    //設(shè)置采樣大小,一般為8位或16位
    audioFormat.setSampleSize(sampleSize);
    //設(shè)置編碼方式
    audioFormat.setCodec("audio/pcm");
    //設(shè)置字節(jié)序
    audioFormat.setByteOrder(QAudioFormat::LittleEndian);
    //設(shè)置樣本數(shù)據(jù)類型
    audioFormat.setSampleType(QAudioFormat::UnSignedInt);
    //音頻設(shè)備信息

    QList<QAudioDeviceInfo> list = QAudioDeviceInfo::availableDevices(QAudio::AudioOutput);
    foreach (QAudioDeviceInfo info, list) {
        if (info.isFormatSupported(audioFormat)) {
            audioOutput = new QAudioOutput(audioFormat, this);
            streamOut = audioOutput->start();
            break;
        }
    }
}

PlayPCM::~PlayPCM()
{
    delete audioOutput;
    audioOutput = nullptr;
    streamOut = nullptr;
}

void PlayPCM::writePCM(QByteArray array)
{
    if (streamOut)
        streamOut->write(array);
}

#endif

不過在運(yùn)行的時候出了問題

ALSA lib pcm_hw.c:1788:(_snd_pcm_hw_open) Unknown field slave

嘶~ 好像是alsa源碼出了問題,但是又不想去看源碼......

我記得arm板上有了aplay這個程序,試試它能不能播WAV,可以的話就參考它的源碼自己寫一個PCM播放

# aplay -D hw:0,0 a.wav
Playing WAVE 'a.wav' : Signed 16 bit Little Endian, Rate 44100 Hz, Stereo

可以! 再把WAV掐掉頭變成PCM

# aplay -D hw:0,0 a.pcm
Playing raw data 'a.pcm' : Unsigned 8 bit, Rate 8000 Hz, Mono
aplay: set_params:1297: Sample format non available
Available formats:
- S16_LE
- S24_LE

......看來需要指定參數(shù)

aplay -D hw:0,0 -r 44100 -c 2 -f s16 a.pcm
Playing raw data 'a.pcm' : Signed 16 bit Little Endian, Rate 44100 Hz, Stereo

可以播了,去參考參考(復(fù)制)aplay的源碼,然后發(fā)現(xiàn)了aplay輸入源可以是 stdin,這就舒服了啊,這下連代碼都不用參考了

#ifndef PLAYPCM_H
#define PLAYPCM_H

#include <QObject>
#include <QProcess>

class PlayPCM : public QObject
{
    Q_OBJECT
    QProcess process;

public:
    explicit PlayPCM(int sampleRate = 44100, int channels = 2, int sampleSize = 16, QObject *parent = nullptr);
    ~PlayPCM();
signals:

public slots:
    void writePCM(QByteArray array);
};

#endif // PLAYPCM_H
#include "playpcm.h"
#include <QDebug>
PlayPCM::PlayPCM(int sampleRate, int channels, int sampleSize, QObject *parent) : QObject(parent)
{
    char cmd[128];
    sprintf(cmd, "aplay -D hw:0,0 -r %d -c %d -f s%d", sampleRate, channels, sampleSize);
    process.start(cmd);
    if (process.waitForStarted()){
        //qDebug() << "PCM player is ready.";
    }
}

PlayPCM::~PlayPCM()
{
    process.kill();
}

void PlayPCM::writePCM(QByteArray array)
{
    if (process.isOpen())
        process.write(array);
}

參考:
一定要仔細(xì)看的help


aplay -h
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

友情鏈接更多精彩內(nèi)容