forked from Green-Sky/tomato
further small reframer fixes, workaround distortion bug by wrapping sdl
input with reframer (magic fix, someone pls tell my why)
This commit is contained in:
parent
45e6fe0033
commit
0039340fd5
@ -30,41 +30,49 @@ struct AudioStreamPopReFramer : public FrameStream2I<AudioFrame2> {
|
|||||||
|
|
||||||
~AudioStreamPopReFramer(void) {}
|
~AudioStreamPopReFramer(void) {}
|
||||||
|
|
||||||
|
size_t getDesiredSize(void) const {
|
||||||
|
return _frame_length_ms * _sample_rate * _channels / 1000;
|
||||||
|
}
|
||||||
|
|
||||||
int32_t size(void) override { return -1; }
|
int32_t size(void) override { return -1; }
|
||||||
|
|
||||||
std::optional<AudioFrame2> pop(void) override {
|
std::optional<AudioFrame2> pop(void) override {
|
||||||
auto new_in = _stream.pop();
|
do {
|
||||||
if (new_in.has_value()) {
|
auto new_in = _stream.pop();
|
||||||
auto& new_value = new_in.value();
|
if (new_in.has_value()) {
|
||||||
|
auto& new_value = new_in.value();
|
||||||
|
|
||||||
// changed
|
// changed
|
||||||
if (_sample_rate != new_value.sample_rate || _channels != new_value.channels) {
|
if (_sample_rate != new_value.sample_rate || _channels != new_value.channels) {
|
||||||
if (_channels != 0) {
|
//if (_channels != 0) {
|
||||||
//std::cerr << "ReFramer warning: reconfiguring, dropping buffer\n";
|
// std::cerr << "ReFramer warning: reconfiguring, dropping buffer\n";
|
||||||
|
//}
|
||||||
|
_sample_rate = new_value.sample_rate;
|
||||||
|
_channels = new_value.channels;
|
||||||
|
|
||||||
|
// buffer does not exist or config changed and we discard
|
||||||
|
_buffer = {};
|
||||||
}
|
}
|
||||||
_sample_rate = new_value.sample_rate;
|
|
||||||
_channels = new_value.channels;
|
|
||||||
|
|
||||||
// buffer does not exist or config changed and we discard
|
//std::cout << "new incoming frame is " << new_value.getSpan().size/new_value.channels*1000/new_value.sample_rate << "ms\n";
|
||||||
_buffer = {};
|
|
||||||
}
|
|
||||||
|
|
||||||
|
auto new_span = new_value.getSpan();
|
||||||
|
|
||||||
//std::cout << "new incoming frame is " << new_value.getSpan().size/new_value.channels*1000/new_value.sample_rate << "ms\n";
|
if (_buffer.empty()) {
|
||||||
|
_buffer = {new_span.cbegin(), new_span.cend()};
|
||||||
auto new_span = new_value.getSpan();
|
} else {
|
||||||
|
_buffer.insert(_buffer.cend(), new_span.cbegin(), new_span.cend());
|
||||||
if (_buffer.empty()) {
|
}
|
||||||
_buffer = {new_span.cbegin(), new_span.cend()};
|
} else if (_buffer.empty()) {
|
||||||
|
// first pop might result in invalid state
|
||||||
|
return std::nullopt;
|
||||||
} else {
|
} else {
|
||||||
_buffer.insert(_buffer.cend(), new_span.cbegin(), new_span.cend());
|
// inner stream pop did not give a new value
|
||||||
|
break; // out of loop
|
||||||
}
|
}
|
||||||
} else if (_buffer.empty()) {
|
} while (_buffer.size() < getDesiredSize());
|
||||||
// first pop might result in invalid state
|
|
||||||
return std::nullopt;
|
|
||||||
}
|
|
||||||
|
|
||||||
const size_t desired_size {_frame_length_ms * _sample_rate * _channels / 1000};
|
const auto desired_size = getDesiredSize();
|
||||||
|
|
||||||
// > threshold?
|
// > threshold?
|
||||||
if (_buffer.size() < desired_size) {
|
if (_buffer.size() < desired_size) {
|
||||||
|
@ -4,6 +4,8 @@
|
|||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <optional>
|
#include <optional>
|
||||||
|
|
||||||
|
#include "../audio_stream_pop_reframer.hpp"
|
||||||
|
|
||||||
// "thin" wrapper around sdl audio streams
|
// "thin" wrapper around sdl audio streams
|
||||||
// we dont needs to get fance, as they already provide everything we need
|
// we dont needs to get fance, as they already provide everything we need
|
||||||
struct SDLAudio2StreamReader : public AudioFrame2Stream2I {
|
struct SDLAudio2StreamReader : public AudioFrame2Stream2I {
|
||||||
@ -58,7 +60,7 @@ struct SDLAudio2StreamReader : public AudioFrame2Stream2I {
|
|||||||
return std::nullopt;
|
return std::nullopt;
|
||||||
}
|
}
|
||||||
|
|
||||||
return AudioFrame2 {
|
return AudioFrame2{
|
||||||
_sample_rate, _channels,
|
_sample_rate, _channels,
|
||||||
Span<int16_t>(_buffer.data(), read_bytes/sizeof(int16_t)),
|
Span<int16_t>(_buffer.data(), read_bytes/sizeof(int16_t)),
|
||||||
};
|
};
|
||||||
@ -127,11 +129,17 @@ std::shared_ptr<FrameStream2I<AudioFrame2>> SDLAudio2InputDevice::subscribe(void
|
|||||||
// error check
|
// error check
|
||||||
SDL_BindAudioStream(_virtual_device_id, sdl_stream);
|
SDL_BindAudioStream(_virtual_device_id, sdl_stream);
|
||||||
|
|
||||||
auto new_stream = std::make_shared<SDLAudio2StreamReader>();
|
//auto new_stream = std::make_shared<SDLAudio2StreamReader>();
|
||||||
// TODO: move to ctr
|
//// TODO: move to ctr
|
||||||
new_stream->_stream = {sdl_stream, &SDL_DestroyAudioStream};
|
//new_stream->_stream = {sdl_stream, &SDL_DestroyAudioStream};
|
||||||
new_stream->_sample_rate = spec.freq;
|
//new_stream->_sample_rate = spec.freq;
|
||||||
new_stream->_channels = spec.channels;
|
//new_stream->_channels = spec.channels;
|
||||||
|
|
||||||
|
auto new_stream = std::make_shared<AudioStreamPopReFramer<SDLAudio2StreamReader>>();
|
||||||
|
new_stream->_stream._stream = {sdl_stream, &SDL_DestroyAudioStream};
|
||||||
|
new_stream->_stream._sample_rate = spec.freq;
|
||||||
|
new_stream->_stream._channels = spec.channels;
|
||||||
|
new_stream->_frame_length_ms = 5; // WHY DOES THIS FIX MY ISSUE !!!
|
||||||
|
|
||||||
_streams.emplace_back(new_stream);
|
_streams.emplace_back(new_stream);
|
||||||
|
|
||||||
|
@ -81,15 +81,9 @@ int main(void) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
stream.push(f1);
|
stream.push(f1);
|
||||||
|
|
||||||
{
|
|
||||||
auto ret_opt = stream.pop();
|
|
||||||
assert(!ret_opt);
|
|
||||||
}
|
|
||||||
|
|
||||||
// push the other half
|
|
||||||
stream.push(f2);
|
stream.push(f2);
|
||||||
|
|
||||||
|
// supposed to combine both
|
||||||
auto ret_opt = stream.pop();
|
auto ret_opt = stream.pop();
|
||||||
assert(ret_opt);
|
assert(ret_opt);
|
||||||
|
|
||||||
|
@ -58,7 +58,8 @@ struct ToxAVCallAudioSink : public FrameStream2SinkI<AudioFrame2> {
|
|||||||
return nullptr;
|
return nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
_writer = std::make_shared<AudioStreamPopReFramer<LockedFrameStream2<AudioFrame2>>>();
|
// 20ms for now, 10ms would work too, further investigate stutters at 5ms (probably too slow interval rate)
|
||||||
|
_writer = std::make_shared<AudioStreamPopReFramer<LockedFrameStream2<AudioFrame2>>>(20);
|
||||||
|
|
||||||
return _writer;
|
return _writer;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user