Skip to content

Commit

Permalink
implementing the soundwave functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
wwmm committed Jun 10, 2024
1 parent 7935b53 commit f106a52
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 11 deletions.
1 change: 1 addition & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ kde_target_enable_exceptions(eyeofsauron PRIVATE)

target_sources(eyeofsauron PRIVATE
frame_source.cpp
io_device.cpp
main.cpp
sound_wave.cpp
tracker.cpp
Expand Down
48 changes: 48 additions & 0 deletions src/io_device.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#include "io_device.hpp"
#include <qobject.h>
#include <qtpreprocessorsupport.h>
#include <qtypes.h>

namespace sound {

IODevice::IODevice(QObject* parent) : QIODevice(parent) {}

qint64 IODevice::readData(char* data, qint64 maxSize) {
Q_UNUSED(data);
Q_UNUSED(maxSize);
return -1;
}

qint64 IODevice::writeData(const char* data, qint64 maxSize) {
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;
}

} // namespace sound
27 changes: 27 additions & 0 deletions src/io_device.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#pragma once

#include <qobject.h>
#include <qtmetamacros.h>
#include <qtypes.h>
#include <QIODevice>
#include <QList>
#include <QPointF>

namespace sound {

class IODevice : public QIODevice {
Q_OBJECT
public:
explicit IODevice(QObject* parent = nullptr);

static const int sampleCount = 2000;

protected:
qint64 readData(char* data, qint64 maxSize) override;
qint64 writeData(const char* data, qint64 maxSize) override;

private:
QList<QPointF> m_buffer;
};

} // namespace sound
21 changes: 14 additions & 7 deletions src/sound_wave.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#include "sound_wave.hpp"
#include <qaudiodevice.h>
#include <qaudiosink.h>
#include <qaudiosource.h>
#include <qmediacapturesession.h>
#include <qmediaplayer.h>
#include <qobject.h>
Expand All @@ -11,21 +11,22 @@
#include <memory>
#include "config.h"
#include "frame_source.hpp"
#include "io_device.hpp"

namespace sound {

Backend::Backend(QObject* parent)
: QObject(parent),
microphone(std::make_unique<QAudioDevice>()),
microphone_audio_sink(std::make_unique<QAudioSink>()),
io_device(std::make_unique<IODevice>()),
capture_session(std::make_unique<QMediaCaptureSession>()),
media_player(std::make_unique<QMediaPlayer>()),
media_player_audio_sink(std::make_unique<QAudioSink>()) {
media_player(std::make_unique<QMediaPlayer>()) {
qmlRegisterSingletonInstance<Backend>("EoSSoundBackend", VERSION_MAJOR, VERSION_MINOR, "EoSSoundBackend", this);

qmlRegisterSingletonInstance<SourceModel>("EosSoundSourceModel", VERSION_MAJOR, VERSION_MINOR, "EosSoundSourceModel",
&sourceModel);

io_device->open(QIODevice::WriteOnly);

find_microphones();
}

Expand Down Expand Up @@ -110,6 +111,14 @@ void Backend::selectSource(const int& index) {
break;
}
case Microphone: {
auto device = dynamic_cast<const MicSource*>(source.get())->device;

// https: // code.qt.io/cgit/qt/qtcharts.git/tree/examples/charts/audio/widget.cpp?h=6.7

microphone = std::make_unique<QAudioSource>(device, device.preferredFormat());

microphone->start(io_device.get());

break;
}
}
Expand All @@ -120,8 +129,6 @@ void Backend::selectSource(const int& index) {
void Backend::find_microphones() {
for (const auto& device : QMediaDevices::audioInputs()) {
if (!device.isNull()) {
qDebug() << device.description() << device.preferredFormat() << device.supportedSampleFormats();

sourceModel.append(std::make_shared<MicSource>(device));
}
}
Expand Down
8 changes: 4 additions & 4 deletions src/sound_wave.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,11 @@
#include <qtmetamacros.h>
#include <qtypes.h>
#include <qvariant.h>
#include <QAudioSink>
#include <QAudioSource>
#include <QMediaPlayer>
#include <memory>
#include "frame_source.hpp"
#include "io_device.hpp"

namespace sound {

Expand Down Expand Up @@ -94,11 +95,10 @@ class Backend : public QObject {

SourceType current_source_type = SourceType::Microphone;

std::unique_ptr<QAudioDevice> microphone;
std::unique_ptr<QAudioSink> microphone_audio_sink;
std::unique_ptr<IODevice> io_device;
std::unique_ptr<QAudioSource> microphone;
std::unique_ptr<QMediaCaptureSession> capture_session;
std::unique_ptr<QMediaPlayer> media_player;
std::unique_ptr<QAudioSink> media_player_audio_sink;

void find_microphones();
};
Expand Down

0 comments on commit f106a52

Please sign in to comment.