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 9, 2024
1 parent 08860eb commit 7935b53
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 10 deletions.
47 changes: 40 additions & 7 deletions src/frame_source.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#include "frame_source.hpp"
#include <qabstractitemmodel.h>
#include <qaudiodevice.h>
#include <qaudioformat.h>
#include <qbytearray.h>
#include <qdebug.h>
#include <qhash.h>
Expand All @@ -18,6 +20,7 @@
#include <MediaInfo/MediaInfo_Const.h>
#include <qcameradevice.h>
#include <qurl.h>
#include <KLocalizedString>
#include "util.hpp"

Source::Source(SourceType source_type) : source_type(source_type) {}
Expand Down Expand Up @@ -68,6 +71,8 @@ MediaFileSource::MediaFileSource(QUrl file_url) : Source(SourceType::MediaFile),
}
}

MicSource::MicSource(QAudioDevice dev) : Source(SourceType::Microphone), device(std::move(dev)) {}

int SourceModel::rowCount(const QModelIndex& /*parent*/) const {
return list.size();
}
Expand Down Expand Up @@ -158,14 +163,35 @@ QVariant SourceModel::data(const QModelIndex& index, int role) const {
break;
}
case Microphone: {
auto format = dynamic_cast<const MicSource*>(it->get())->format;

if (format.isValid()) {
auto resolution = util::to_string(format.sampleRate()) + " Hz, " + util::to_string(format.sampleFormat());

value = QString::fromStdString(resolution);
auto device = dynamic_cast<const MicSource*>(it->get())->device;

std::string format;

switch (device.preferredFormat().sampleFormat()) {
case QAudioFormat::Unknown:
format = "";
break;
case QAudioFormat::UInt8:
format = "UInt8";
break;
case QAudioFormat::Int16:
format = "Int16";
break;
case QAudioFormat::Int32:
format = "Int32";
break;
case QAudioFormat::Float:
format = "Float";
break;
case QAudioFormat::NSampleFormats:
format = "NSampleFormats";
break;
}

value = QString::fromStdString(std::format("{0:d} Hz, {1:d} {2}, {3}", device.preferredFormat().sampleRate(),
device.preferredFormat().channelCount(),
i18n("channels").toStdString(), format));

break;
}
}
Expand Down Expand Up @@ -221,7 +247,14 @@ void SourceModel::append(std::shared_ptr<Source> source) {
list.append(source);
break;
case Microphone:
list.append(source);
auto mic = dynamic_cast<const MicSource*>(source.get())->device;

if (mic.isDefault()) {
list.insert(0, source);

} else {
list.append(source);
}
break;
}

Expand Down
4 changes: 1 addition & 3 deletions src/frame_source.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,9 @@ class MediaFileSource : public Source {

class MicSource : public Source {
public:
MicSource(QAudioDevice dev, QAudioFormat fmt);
MicSource(QAudioDevice dev);

QAudioDevice device;

QAudioFormat format;
};

class SourceModel : public QAbstractListModel {
Expand Down
13 changes: 13 additions & 0 deletions src/sound_wave.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include <qqml.h>
#include <qtmetamacros.h>
#include <qtypes.h>
#include <QMediaDevices>
#include <memory>
#include "config.h"
#include "frame_source.hpp"
Expand All @@ -24,6 +25,8 @@ Backend::Backend(QObject* parent)

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

find_microphones();
}

Backend::~Backend() {
Expand Down Expand Up @@ -114,6 +117,16 @@ void Backend::selectSource(const int& index) {
Q_EMIT showPlayerSliderChanged();
}

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));
}
}
}

void Backend::saveTable(const QUrl& fileUrl) {}

void Backend::setPlayerPosition(qint64 value) {
Expand Down
2 changes: 2 additions & 0 deletions src/sound_wave.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,8 @@ class Backend : public QObject {
std::unique_ptr<QMediaCaptureSession> capture_session;
std::unique_ptr<QMediaPlayer> media_player;
std::unique_ptr<QAudioSink> media_player_audio_sink;

void find_microphones();
};

} // namespace sound

0 comments on commit 7935b53

Please sign in to comment.