오디오 샘플 대 음량
이 예제는 마이크 샘플과 오디오 레벨 간의 관계를 그래프로 보여줍니다.

이 예제의 구현은 위젯(Widgets)을 기반으로 합니다. 이에 대한 구체적인 정보는 『 Qt Widgets 프로그래밍 시작하기』를 참조하십시오. 다음 섹션에서는 Qt Charts API를 사용하여 샘플 대 음량 그래프에 동적 데이터를 표시하는 방법을 다룹니다. 샘플링에 대한 자세한 내용은 『샘플링(신호 처리)』을 참조하십시오.
예제 실행
다음 위치에서 예제를 실행할 수 있습니다.
- Qt Creator
Welcome 모드를 열고 Examples 에서 예제를 선택하십시오. 자세한 내용은 Qt Creator: 튜토리얼: 빌드 및 실행을 참조하십시오.
- Qt Extension for Visual Studio Code
Command Palette 에서 Qt: Open Qt examples 명령을 실행하고 목록에서 예제를 선택합니다. 자세한 내용은 Qt Extension for Visual Studio Code: 튜토리얼: 빌드 및 실행을 참조하십시오.
오디오 데이터 가져오기
main.cpp 에 Qt Multimedia 모듈이 포함되어 있으므로, 대상 플랫폼의 기본 오디오 장치를 나타내는 새로운 QAudioDevice 를 생성합니다. 또한 오디오 입력 장치가 있는지 확인하고, 사용 가능한 입력 오디오 장치가 없는 경우 사용자에게 알립니다.
if (inputDevice.isNull()) {
QMessageBox::warning(nullptr, "audio",
"There is no audio input device available.");
return -1;
}그런 다음 오디오 장치를 생성자 인자 w 를 통해 Widget w 에 전달합니다. 이후 w.show() 를 호출하여 위젯을 표시합니다.
Widget w(inputDevice);
w.resize(800, 600);
w.show();차트 설정
이 섹션에서는 차트와 축을 설정하는 방법을 다룹니다.
widget.cpp 에서 재구현된 Widget 생성자는 애플리케이션 로직과 관련하여 주요 작업을 수행합니다. QChart, QLineSeries 및 QChartView 객체는 다음과 같이 선언됩니다:
: QWidget(parent)
, m_chart(new QChart)
, m_series(new QLineSeries)
{
auto chartView = new QChartView(m_chart);
m_chart->addSeries(m_series);m_series는 오디오 입력 데이터를 처리하기 위한 것입니다.
x축 "샘플"
x축의 최소값에서 최대값 범위를 0부터 XYSeriesIODevice::sampleCount까지로 설정합니다. (xyseriesiodevice.h에서 2000으로 선언됨). 그런 다음 x축의 제목 텍스트를 설정합니다.
auto axisX = new QValueAxis;
axisX->setRange(0, XYSeriesIODevice::sampleCount);
axisX->setLabelFormat("%g");
axisX->setTitleText("Samples");y축 "오디오 레벨"
QValueAxis axisY 를 생성하고, 범위와 제목 텍스트를 설정합니다.
auto axisY = new QValueAxis;
axisY->setRange(-1, 1);
axisY->setTitleText("Audio level");축 연결 및 차트 제목 설정
축을 연결하고, 범례를 숨기며, 차트 제목에 오디오 입력으로 사용 중인 마이크의 이름을 포함하도록 설정합니다.
m_chart->addAxis(axisX, Qt::AlignBottom);
m_series->attachAxis(axisX);
m_chart->addAxis(axisY, Qt::AlignLeft);
m_series->attachAxis(axisY);
m_chart->legend()->hide();차트 레이아웃 설정
여기서는 QVBoxLayout mainLayout 를 사용하고, 수직 레이아웃에 QChartview chartView 를 추가합니다.
m_chart->setTitle("Data from the microphone (" + deviceInfo.description() + ')');
auto mainLayout = new QVBoxLayout(this);오디오 입력 데이터 사용
이 섹션에서는 마이크 데이터가 QLineSeries( m_series)로 전달되는 방법을 보여줍니다. 앞서와 마찬가지로 이 과정은 Qt Multimedia 모듈에 의존합니다.
먼저 deviceInfo를 QAudioInput 생성자에 전달합니다.
mainLayout->addWidget(chartView);
m_audioInput = new QAudioInput(deviceInfo, this);
QAudioFormat formatAudio;그런 다음 QAudioFormat formatAudio 의 채널 수, 샘플 레이트 및 샘플 형식을 설정합니다.
formatAudio.setSampleRate(8000);
formatAudio.setChannelCount(1);이제 ` QAudioSource `를 생성하고 버퍼 크기를 설정할 수 있습니다.
formatAudio.setSampleFormat(QAudioFormat::UInt8);
m_audioSource = new QAudioSource(deviceInfo, formatAudio);이제 데이터를 차트에 연결할 차례입니다. 이를 위해 XYSeriesIODevice 클래스를 생성했습니다. 구현 방법은 XYSeriesIODevice를 참조하십시오.
m_audioSource->setBufferSize(200);
m_device = new XYSeriesIODevice(m_series, this);
m_device->open(QIODevice::WriteOnly);XYSeriesIODevice
xyseriesiodevice.cpp 에 구현된 XYSeriesIODevice는 신호 샘플링을 처리합니다. 고정 해상도 4를 갖는 ` writeData ` 함수는 샘플 수를 기준으로 ` QList ` m_buffer 의 크기를 설정하고, x 값은 증가하는 순서로, y 값은 0으로 설정된 `QPointFs`로 이를 채웁니다.
{
static const int resolution = 4;
if (m_buffer.isEmpty()) {
m_buffer.reserve(sampleCount);
for (int i = 0; i < sampleCount; ++i)
m_buffer.append(QPointF(i, 0));
}
int start = 0;그런 다음 샘플링을 수행합니다.
const int availableSamples = int(maxSize) / resolution;
if (availableSamples < sampleCount) {
start = sampleCount - availableSamples;
for (int s = 0; s < start; ++s)
m_buffer[s].setY(m_buffer.at(s + availableSamples).y());
}
for (int s = start; s < sampleCount; ++s, data += resolution)
m_buffer[s].setY(qreal(uchar(*data) -128) / qreal(128));
m_series->replace(m_buffer);
return (sampleCount - start) * resolution;© 2026 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.