このページでは

オーディオサンプルと音量の関係

この例では、マイクサンプルとオーディオレベルの関係をグラフで示しています。

マイクのオーディオ入力から出力される音量レベルを表示したグラフのスクリーンショット

この例の実装はWidgetsに基づいています。詳細については、『Qt Widgets プログラミング入門』を参照してください。以下のセクションでは、Qt Charts APIを使用して、サンプル数と音量の関係を示すグラフに動的なデータを表示する方法について説明します。サンプリングに関する詳細については、「サンプリング (信号処理)」を参照してください。

例の動作

この例は、以下の手順で実行できます。

オーディオデータの取得

main.cpp にQt Multimedia モジュールが組み込まれているため、ターゲットプラットフォームのデフォルトのオーディオデバイスを表す新しいQAudioDevice を作成します。また、オーディオ入力デバイスが存在するかを確認し、利用可能な入力オーディオデバイスがない場合はユーザーに通知します。

    if (inputDevice.isNull()) {
        QMessageBox::warning(nullptr, "audio",
                             "There is no audio input device available.");
        return -1;
    }

その後、オーディオデバイスを、コンストラクタの引数w を使用してWidgetw に渡します。続いて、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軸の「Samples」

x軸の最小値から最大値の範囲を0からXYSeriesIODevice::sampleCountに設定します(xyseriesiodevice.hで2000として宣言されています)。その後、そのタイトルテキストを設定します。

    auto axisX = new QValueAxis;
    axisX->setRange(0, XYSeriesIODevice::sampleCount);
    axisX->setLabelFormat("%g");
    axisX->setTitleText("Samples");
y軸「Audio Level」

axisY を使用して QValueAxis を作成し、その範囲とタイトルテキストを設定します。

    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 を使用し、垂直レイアウトに QChartviewchartView を追加します。

    m_chart->setTitle("Data from the microphone (" + deviceInfo.description() + ')');

    auto mainLayout = new QVBoxLayout(this);

オーディオ入力データの使用

このセクションでは、マイクデータが QLineSeriesm_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は、信号のサンプリングを処理します。writeData 関数は、固定解像度4を用いて、サンプル数に基づいて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;

サンプルプロジェクト @ code.qt.io

© 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.