Created
April 23, 2015 17:01
-
-
Save n-at/fd8f453fbe3898c42dff to your computer and use it in GitHub Desktop.
Qt Audio
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "audioanalyzer.h" | |
AudioAnalyzer::AudioAnalyzer(QObject *parent) : QObject(parent) { | |
this->file = new QFile("c:\\Users\\User\\audioData.txt"); | |
if(this->file->open(QFile::WriteOnly | QFile::Truncate)) { | |
this->txtStream = new QTextStream(this->file); | |
} | |
} | |
void AudioAnalyzer::analyze(const QAudioBuffer &buf) | |
{ | |
qDebug() << "analyzing..."; | |
qDebug() << "duration: " << buf.duration() << "microsec"; | |
qDebug() << "sample count: " << buf.sampleCount(); | |
qDebug() << "start time: " << buf.startTime(); | |
qDebug() << "valid: " << (buf.isValid() ? "yes" : "no"); | |
QAudioFormat format = buf.format(); | |
qDebug() << "sample rate: " << format.sampleRate(); | |
qDebug() << "sample type: " << format.sampleType(); | |
qDebug() << "sample size: " << format.sampleSize(); | |
const qint16 *data = buf.constData<qint16>(); | |
for(int i = 0; i < buf.sampleCount(); i++) { | |
qint16 value = (qint16)(*(data + sizeof(qint16) * i)); | |
qDebug() << value; | |
*(this->txtStream) << value << endl; | |
} | |
qDebug() << endl; | |
//exit(0); | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ifndef AUDIOANALYZER_H | |
#define AUDIOANALYZER_H | |
#include <QObject> | |
#include <QtMultimedia> | |
class AudioAnalyzer : public QObject | |
{ | |
Q_OBJECT | |
QFile *file; | |
QTextStream *txtStream; | |
public: | |
explicit AudioAnalyzer(QObject *parent = 0); | |
signals: | |
public slots: | |
void analyze(const QAudioBuffer &buf); | |
}; | |
#endif // AUDIOANALYZER_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
foreach (const QAudioDeviceInfo &deviceInfo, QAudioDeviceInfo::availableDevices(QAudio::AudioInput)) { | |
qDebug() << "Device name: " << deviceInfo.deviceName(); | |
qDebug() << "Codecs:" << endl; | |
foreach(QString codec, deviceInfo.supportedCodecs()) { | |
qDebug() << "* " << codec << endl; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <QtCore> | |
#include <QtMultimedia> | |
#include <iostream> | |
using std::cout; | |
using std::endl; | |
#include "audioanalyzer.h" | |
int main(int argc, char *argv[]) | |
{ | |
QCoreApplication a(argc, argv); | |
QAudioRecorder *recorder = new QAudioRecorder; | |
QAudioEncoderSettings audioSettings; | |
audioSettings.setCodec("audio/pcm"); | |
audioSettings.setQuality(QMultimedia::VeryHighQuality); | |
recorder->setAudioSettings(audioSettings); | |
recorder->setOutputLocation(QUrl::fromLocalFile("test.wav")); | |
AudioAnalyzer *analyzer = new AudioAnalyzer(&a); | |
QAudioProbe *audioProbe = new QAudioProbe(&a); | |
if(audioProbe->setSource(recorder)) { | |
QObject::connect( | |
audioProbe, | |
SIGNAL(audioBufferProbed(QAudioBuffer)), | |
analyzer, | |
SLOT(analyze(QAudioBuffer)) | |
); | |
} | |
recorder->record(); | |
return a.exec(); | |
//return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment