1 #include "AudioTools/CoreAudio/AudioStreams.h"
2 #include "AudioTools/CoreAudio/Buffers.h"
19 PureDataStream(HeavyContextInterface &heavy,
int bufferSize = 1024 * 2) {
21 buffer_size = bufferSize;
25 AudioInfo result(p_heavy->getSampleRate(), p_heavy->getNumOutputChannels(),
31 AudioInfo result(p_heavy->getSampleRate(), p_heavy->getNumInputChannels(),
38 LOGE(
"The audio format in wrong and can not be changed");
42 size_t readBytes(uint8_t *data,
size_t len) {
43 int len_max = std::min(len, buffer_read.
size());
44 if (buffer_read.isEmpty()) readWrite(len_max);
45 return buffer_read.
readArray(data, len_max);
48 size_t write(
const uint8_t *data,
size_t len) {
49 int len_max = std::min(len, buffer_write.
size());
50 if (!buffer_write.isEmpty()) readWrite(len_max);
55 int sample_rate = p_heavy->getSampleRate();
56 in_channels = p_heavy->getNumInputChannels();
57 out_channels = p_heavy->getNumOutputChannels();
58 if (out_channels > 0) buffer_read.resize(buffer_size);
59 if (in_channels > 0) buffer_write.resize(buffer_size);
61 LOGW(
"rate: %d, channels: in=%d, out=%d", sample_rate, in_channels,
64 LOGI(
"rate: %d, channels: in=%d, out=%d", sample_rate, in_channels,
67 return in_channels > 0 || out_channels > 0;
70 void flush() { readWrite(buffer_write.
available() / sample_size); }
74 buffer_read.resize(0);
75 buffer_write.resize(0);
81 HeavyContextInterface *p_heavy =
nullptr;
82 RingBuffer<uint8_t> buffer_write{0};
83 RingBuffer<uint8_t> buffer_read{0};
88 const float max_int = 32767.0;
89 const int sample_size =
sizeof(int16_t);
94 void readWrite(
int bytes) {
95 if (bytes == 0)
return;
96 int samples = bytes / sample_size;
98 samples = samples / HV_N_SIMD * HV_N_SIMD;
99 int frames = samples / out_channels;
101 if (in.size()<samples) in.resize(samples);
102 if (out.size()<samples) out.resize(samples);
105 if (buffer_write.
size() > 0) {
106 for (
int j = 0; j < samples; j++) {
108 size_t eff = buffer_write.
readArray((uint8_t *)&tmp16, sample_size);
109 assert(eff == sample_size);
110 in[j] = volume * tmp16 / max_int;
115 int frames_eff = p_heavy->processInlineInterleaved(in.data(), out.data(), frames);
117 assert(frames == frames_eff);
120 if (buffer_read.
size() > 0) {
121 for (
int j = 0; j < samples; j++) {
122 int16_t tmp16 = volume * out[j] * max_int;
123 size_t eff = buffer_read.
writeArray((uint8_t *)&tmp16, sample_size);
124 assert(eff == sample_size);