summaryrefslogtreecommitdiffstats
path: root/tests/manual/spectrum/spectrumapp/engine.h
diff options
context:
space:
mode:
Diffstat (limited to 'tests/manual/spectrum/spectrumapp/engine.h')
-rw-r--r--tests/manual/spectrum/spectrumapp/engine.h250
1 files changed, 250 insertions, 0 deletions
diff --git a/tests/manual/spectrum/spectrumapp/engine.h b/tests/manual/spectrum/spectrumapp/engine.h
new file mode 100644
index 00000000..fb8450e0
--- /dev/null
+++ b/tests/manual/spectrum/spectrumapp/engine.h
@@ -0,0 +1,250 @@
+/******************************************************************************
+**
+** Copyright (C) 2015 The Qt Company Ltd.
+** Contact: http://www.qt.io/licensing/
+**
+** This file is part of the Qt Data Visualization module.
+**
+** $QT_BEGIN_LICENSE:COMM$
+**
+** Commercial License Usage
+** Licensees holding valid commercial Qt licenses may use this file in
+** accordance with the commercial license agreement provided with the
+** Software or, alternatively, in accordance with the terms contained in
+** a written agreement between you and The Qt Company. For licensing terms
+** and conditions see http://www.qt.io/terms-conditions. For further
+** information use the contact form at http://www.qt.io/contact-us.
+**
+** $QT_END_LICENSE$
+**
+******************************************************************************/
+
+#ifndef ENGINE_H
+#define ENGINE_H
+
+#include "spectrum.h"
+#include "spectrumanalyser.h"
+#include "wavfile.h"
+
+#include <QAudioDeviceInfo>
+#include <QAudioFormat>
+#include <QBuffer>
+#include <QByteArray>
+#include <QDir>
+#include <QObject>
+#include <QVector>
+
+class FrequencySpectrum;
+QT_BEGIN_NAMESPACE
+class QAudioInput;
+class QAudioOutput;
+QT_END_NAMESPACE
+
+/**
+ * This class interfaces with the QtMultimedia audio classes, and also with
+ * the SpectrumAnalyser class. Its role is to manage the capture and playback
+ * of audio data, meanwhile performing real-time analysis of the audio level
+ * and frequency spectrum.
+ */
+class Engine : public QObject
+{
+ Q_OBJECT
+
+public:
+ explicit Engine(QObject *parent = 0);
+ ~Engine();
+
+ const QList<QAudioDeviceInfo> &availableAudioInputDevices() const
+ { return m_availableAudioInputDevices; }
+
+ const QList<QAudioDeviceInfo> &availableAudioOutputDevices() const
+ { return m_availableAudioOutputDevices; }
+
+ QAudio::Mode mode() const { return m_mode; }
+ QAudio::State state() const { return m_state; }
+
+ /**
+ * \return Current audio format
+ * \note May be QAudioFormat() if engine is not initialized
+ */
+ const QAudioFormat& format() const { return m_format; }
+
+ /**
+ * Stop any ongoing recording or playback, and reset to ground state.
+ */
+ void reset();
+
+ /**
+ * Load data from WAV file
+ */
+ bool loadFile(const QString &fileName);
+
+ /**
+ * Position of the audio input device.
+ * \return Position in bytes.
+ */
+ qint64 recordPosition() const { return m_recordPosition; }
+
+ /**
+ * RMS level of the most recently processed set of audio samples.
+ * \return Level in range (0.0, 1.0)
+ */
+ qreal rmsLevel() const { return m_rmsLevel; }
+
+ /**
+ * Peak level of the most recently processed set of audio samples.
+ * \return Level in range (0.0, 1.0)
+ */
+ qreal peakLevel() const { return m_peakLevel; }
+
+ /**
+ * Position of the audio output device.
+ * \return Position in bytes.
+ */
+ qint64 playPosition() const { return m_playPosition; }
+
+ /**
+ * Length of the internal engine buffer.
+ * \return Buffer length in bytes.
+ */
+ qint64 bufferLength() const;
+
+ /**
+ * Amount of data held in the buffer.
+ * \return Data length in bytes.
+ */
+ qint64 dataLength() const { return m_dataLength; }
+
+ /**
+ * Set window function applied to audio data before spectral analysis.
+ */
+ void setWindowFunction(WindowFunction type);
+
+public Q_SLOTS:
+ void startRecording();
+ void startPlayback();
+ void suspend();
+ void setAudioInputDevice(const QAudioDeviceInfo &device);
+ void setAudioOutputDevice(const QAudioDeviceInfo &device);
+
+Q_SIGNALS:
+ void stateChanged(QAudio::Mode mode, QAudio::State state);
+
+ /**
+ * Format of audio data has changed
+ */
+ void formatChanged(const QAudioFormat &format);
+
+ /**
+ * Length of buffer has changed.
+ * \param duration Duration in microseconds
+ */
+ void bufferLengthChanged(qint64 duration);
+
+ /**
+ * Amount of data in buffer has changed.
+ * \param Length of data in bytes
+ */
+ void dataLengthChanged(qint64 duration);
+
+ /**
+ * Position of the audio input device has changed.
+ * \param position Position in bytes
+ */
+ void recordPositionChanged(qint64 position);
+
+ /**
+ * Position of the audio output device has changed.
+ * \param position Position in bytes
+ */
+ void playPositionChanged(qint64 position);
+
+ /**
+ * Level changed
+ * \param rmsLevel RMS level in range 0.0 - 1.0
+ * \param peakLevel Peak level in range 0.0 - 1.0
+ * \param numSamples Number of audio samples analyzed
+ */
+ void levelChanged(qreal rmsLevel, qreal peakLevel, int numSamples);
+
+ /**
+ * Spectrum has changed.
+ * \param position Position of start of window in bytes
+ * \param length Length of window in bytes
+ * \param spectrum Resulting frequency spectrum
+ */
+ void changedSpectrum(qint64 position, qint64 length, const FrequencySpectrum &spectrum);
+
+ /**
+ * Buffer containing audio data has changed.
+ * \param position Position of start of buffer in bytes
+ * \param buffer Buffer
+ */
+ void bufferChanged(qint64 position, qint64 length, const QByteArray &buffer);
+
+private Q_SLOTS:
+ void audioNotify();
+ void audioStateChanged(QAudio::State state);
+ void audioDataReady();
+ void spectrumChanged(const FrequencySpectrum &spectrum);
+
+private:
+ void resetAudioDevices();
+ bool initialize();
+ bool selectFormat();
+ void stopRecording();
+ void stopPlayback();
+ void setState(QAudio::State state);
+ void setState(QAudio::Mode mode, QAudio::State state);
+ void setFormat(const QAudioFormat &format);
+ void setRecordPosition(qint64 position, bool forceEmit = false);
+ void setPlayPosition(qint64 position, bool forceEmit = false);
+ void calculateLevel(qint64 position, qint64 length);
+ void calculateSpectrum(qint64 position);
+ void setLevel(qreal rmsLevel, qreal peakLevel, int numSamples);
+
+private:
+ QAudio::Mode m_mode;
+ QAudio::State m_state;
+
+ bool m_generateTone;
+ SweptTone m_tone;
+
+ WavFile* m_file;
+ // We need a second file handle via which to read data into m_buffer
+ // for analysis
+ WavFile* m_analysisFile;
+
+ QAudioFormat m_format;
+
+ const QList<QAudioDeviceInfo> m_availableAudioInputDevices;
+ QAudioDeviceInfo m_audioInputDevice;
+ QAudioInput* m_audioInput;
+ QIODevice* m_audioInputIODevice;
+ qint64 m_recordPosition;
+
+ const QList<QAudioDeviceInfo> m_availableAudioOutputDevices;
+ QAudioDeviceInfo m_audioOutputDevice;
+ QAudioOutput* m_audioOutput;
+ qint64 m_playPosition;
+ QBuffer m_audioOutputIODevice;
+
+ QByteArray m_buffer;
+ qint64 m_bufferPosition;
+ qint64 m_bufferLength;
+ qint64 m_dataLength;
+
+ int m_levelBufferLength;
+ qreal m_rmsLevel;
+ qreal m_peakLevel;
+
+ int m_spectrumBufferLength;
+ QByteArray m_spectrumBuffer;
+ SpectrumAnalyser m_spectrumAnalyser;
+ qint64 m_spectrumPosition;
+
+ int m_count;
+
+};
+
+#endif // ENGINE_H