generated from CoolLibs/library-template
-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e552232
commit 27b8706
Showing
5 changed files
with
91 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
#pragma once | ||
|
||
#include "../../src/Player.hpp" | ||
#include "../../src/InputStream.hpp" | ||
#include "../../src/Player.hpp" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
#include "InputStream.hpp" | ||
|
||
namespace RtAudioW { | ||
|
||
InputStream::InputStream(AudioInputCallback callback) | ||
: _callback{std::move(callback)} | ||
{ | ||
set_device(_backend.getDefaultInputDevice()); | ||
} | ||
|
||
auto InputStream::device_ids() const -> std::vector<unsigned int> | ||
{ | ||
auto ids = _backend.getDeviceIds(); | ||
// Keep only the input devices | ||
std::erase_if(ids, [&](unsigned int id) { | ||
auto const info = _backend.getDeviceInfo(id); | ||
return info.inputChannels == 0; | ||
}); | ||
return ids; | ||
} | ||
|
||
auto InputStream::device_info(unsigned int device_id) const -> RtAudio::DeviceInfo | ||
{ | ||
return _backend.getDeviceInfo(device_id); | ||
} | ||
|
||
auto audio_input_callback(void* /* output_buffer */, void* input_buffer, unsigned int frames_count, double /* stream_time */, RtAudioStreamStatus /* status */, void* user_data) -> int | ||
{ | ||
static_cast<InputStream*>(user_data)->_callback(std::span{static_cast<float*>(input_buffer), frames_count}); | ||
return 0; | ||
} | ||
|
||
void InputStream::set_device(unsigned int device_id) | ||
{ | ||
if (_backend.isStreamOpen()) | ||
_backend.closeStream(); | ||
|
||
auto const info = _backend.getDeviceInfo(device_id); | ||
RtAudio::StreamParameters params; | ||
params.deviceId = device_id; | ||
params.nChannels = 1; | ||
unsigned int nb_frames{512}; // TODO(Audio) Allow users to customize this? | ||
_backend.openStream(nullptr, ¶ms, RTAUDIO_FLOAT32, info.preferredSampleRate, &nb_frames, &audio_input_callback, this); | ||
_backend.startStream(); | ||
_current_input_device_name = info.name; | ||
} | ||
|
||
} // namespace RtAudioW |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
#pragma once | ||
#include <span> | ||
#include "rtaudio/RtAudio.h" | ||
|
||
namespace RtAudioW { | ||
|
||
/// Receives a buffer containing audio samples. | ||
/// There is always 1 channel in the input buffer. | ||
using AudioInputCallback = std::function<void(std::span<float>)>; | ||
|
||
class InputStream { | ||
public: | ||
explicit InputStream(AudioInputCallback); | ||
~InputStream() = default; | ||
InputStream(InputStream const&) = delete; // | ||
auto operator=(InputStream const&) -> InputStream& = delete; // Can't copy nor move | ||
InputStream(InputStream&&) noexcept = delete; // because we pass the address of this object to the audio callback. | ||
auto operator=(InputStream&&) noexcept -> InputStream& = delete; // | ||
|
||
/// Returns the list of all the ids of input devices. | ||
auto device_ids() const -> std::vector<unsigned int>; | ||
/// Returns all the info about a given device. | ||
auto device_info(unsigned int device_id) const -> RtAudio::DeviceInfo; | ||
/// | ||
auto current_device_name() const -> std::string const& { return _current_input_device_name; } | ||
/// Sets the device to use. | ||
/// By default, when an InputStream is created it uses the default input device selected by the OS. | ||
void set_device(unsigned int device_id); | ||
|
||
private: | ||
friend auto audio_input_callback(void* output_buffer, void* input_buffer, unsigned int frames_count, double stream_time, RtAudioStreamStatus status, void* user_data) -> int; | ||
|
||
private: | ||
mutable RtAudio _backend{}; | ||
AudioInputCallback _callback{}; | ||
std::string _current_input_device_name{}; | ||
}; | ||
|
||
} // namespace RtAudioW |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters