Skip to content

Commit

Permalink
✨ Added InputStream
Browse files Browse the repository at this point in the history
  • Loading branch information
JulesFouchy committed Oct 27, 2023
1 parent e552232 commit 27b8706
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 3 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ set(WARNINGS_AS_ERRORS_FOR_RTAUDIOWRAPPER OFF CACHE BOOL "ON iff you want to tre

add_library(RtAudioWrapper)
add_library(RtAudioWrapper::RtAudioWrapper ALIAS RtAudioWrapper)
target_compile_features(RtAudioWrapper PRIVATE cxx_std_11)
target_compile_features(RtAudioWrapper PRIVATE cxx_std_20)

# ---Add source files---
if(WARNINGS_AS_ERRORS_FOR_RTAUDIOWRAPPER)
Expand Down
3 changes: 2 additions & 1 deletion include/RtAudioWrapper/RtAudioWrapper.hpp
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"
48 changes: 48 additions & 0 deletions src/InputStream.cpp
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, &params, RTAUDIO_FLOAT32, info.preferredSampleRate, &nb_frames, &audio_input_callback, this);
_backend.startStream();
_current_input_device_name = info.name;
}

} // namespace RtAudioW
39 changes: 39 additions & 0 deletions src/InputStream.hpp
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
2 changes: 1 addition & 1 deletion tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ project(RtAudioWrapper-tests)

# ---Create executable---
add_executable(${PROJECT_NAME} tests.cpp)
target_compile_features(${PROJECT_NAME} PRIVATE cxx_std_11)
target_compile_features(${PROJECT_NAME} PRIVATE cxx_std_20)

# ---Include our library---
add_subdirectory(.. ${CMAKE_CURRENT_SOURCE_DIR}/build/RtAudioWrapper)
Expand Down

0 comments on commit 27b8706

Please sign in to comment.