From c61fcd1f2bc971d80d7e66990d3429107509fafb Mon Sep 17 00:00:00 2001 From: sudden6 Date: Thu, 4 Apr 2019 23:57:08 +0200 Subject: [PATCH] refactor(audio): create interface for audio sinks and sources --- CMakeLists.txt | 1 + src/audio/iaudiosink.h | 94 ++++++++++++++++++++++++++++++++++++++++ src/audio/iaudiosource.h | 22 ++++++++++ 3 files changed, 117 insertions(+) create mode 100644 src/audio/iaudiosink.h create mode 100644 src/audio/iaudiosource.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 3c972eb45..1fef07ecd 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -239,6 +239,7 @@ set(${PROJECT_NAME}_SOURCES src/audio/backend/openal.cpp src/audio/backend/openal.h src/audio/iaudiosettings.h + src/audio/iaudiosink.h src/chatlog/chatlinecontent.cpp src/chatlog/chatlinecontent.h src/chatlog/chatlinecontentproxy.cpp diff --git a/src/audio/iaudiosink.h b/src/audio/iaudiosink.h new file mode 100644 index 000000000..1fb1a9e61 --- /dev/null +++ b/src/audio/iaudiosink.h @@ -0,0 +1,94 @@ +#ifndef IAUDIOSINK_H +#define IAUDIOSINK_H + +#include + +#include + +/** + * @brief The IAudioSink class represents an interface to devices that can play audio. + * + * @enum IAudioSink::Sound + * @brief Provides the different sounds for use in the getSound function. + * @see getSound + * + * @value NewMessage Returns the new message notification sound. + * @value Test Returns the test sound. + * @value IncomingCall Returns the incoming call sound. + * @value OutgoingCall Returns the outgoing call sound. + * + * @fn QString IAudioSink::getSound(Sound s) + * @brief Function to get the path of the requested sound. + * + * @param s Name of the sound to get the path of. + * @return The path of the requested sound. + * + * @fn void IAudioSink::playAudioBuffer(const int16_t* data, int samples, + * unsigned channels, int sampleRate) + * @brief adds a number of audio frames to the play buffer + * + * @param[in] data 16bit mono or stereo PCM data with alternating channel + * mapping for stereo (LRLR) + * @param[in] samples number of samples per channel + * @param[in] channels number of channels, currently 1 or 2 is supported + * @param[in] sampleRate sample rate in Hertz + * + * @fn void IAudioSink::playMono16Sound(const Sound& sound) + * @brief Play a 44100Hz mono 16bit PCM sound from the builtin sounds. + * + * @param[in] sound The sound to play + * + * @fn void IAudioSink::startLoop() + * @brief starts looping the sound played with playMono16Sound(...) + * + * @fn void IAudioSink::stopLoop() + * @brief stops looping the sound played with playMono16Sound(...) + * + */ + +class IAudioSink : public QObject +{ + Q_OBJECT +public: + enum class Sound + { + NewMessage, + Test, + IncomingCall, + OutgoingCall, + CallEnd + }; + + inline static QString getSound(Sound s) + { + switch (s) { + case Sound::Test: + return QStringLiteral(":/audio/notification.pcm"); + case Sound::NewMessage: + return QStringLiteral(":/audio/notification.pcm"); + case Sound::IncomingCall: + return QStringLiteral(":/audio/ToxIncomingCall.pcm"); + case Sound::OutgoingCall: + return QStringLiteral(":/audio/ToxOutgoingCall.pcm"); + case Sound::CallEnd: + return QStringLiteral(":/audio/ToxEndCall.pcm"); + } + assert(false); + return {}; + } + + virtual ~IAudioSink() {} + virtual void playAudioBuffer(const int16_t* data, int samples, unsigned channels, + int sampleRate) const = 0; + virtual void playMono16Sound(const Sound& sound) = 0; + virtual void startLoop() = 0; + virtual void stopLoop() = 0; + + virtual operator bool() const = 0; + +signals: + void finishedPlaying(); + void invalidated(); +}; + +#endif // IAUDIOSINK_H diff --git a/src/audio/iaudiosource.h b/src/audio/iaudiosource.h new file mode 100644 index 000000000..c9f1c1da2 --- /dev/null +++ b/src/audio/iaudiosource.h @@ -0,0 +1,22 @@ +#ifndef IAUDIOSOURCE_H +#define IAUDIOSOURCE_H + +#include + +class Audio; +class IAudioSource : public QObject +{ + Q_OBJECT +public: + virtual ~IAudioSource() {} + + virtual operator bool() const = 0; + +signals: + void frameAvailable(const int16_t* pcm, size_t sample_count, uint8_t channels, + uint32_t sampling_rate); + void volumeAvailable(float value); + void invalidated(); +}; + +#endif // IAUDIOSOURCE_H