Skip to content
Snippets Groups Projects
Commit dbba9e62 authored by Adrien Béraud's avatar Adrien Béraud
Browse files

tone: generate requested length

Change-Id: Ifc8e85cad40def55606591c4267a81b0a3d90951
parent 0f032559
Branches
No related tags found
No related merge requests found
...@@ -116,9 +116,9 @@ DTMFGenerator::fillToneBuffer(int index) ...@@ -116,9 +116,9 @@ DTMFGenerator::fillToneBuffer(int index)
ptr->format = tone_.getFormat().sampleFormat; ptr->format = tone_.getFormat().sampleFormat;
ptr->sample_rate = sampleRate_; ptr->sample_rate = sampleRate_;
ptr->channel_layout = AV_CH_LAYOUT_MONO; ptr->channel_layout = AV_CH_LAYOUT_MONO;
av_channel_layout_from_mask(&ptr->ch_layout, AV_CH_LAYOUT_MONO); av_channel_layout_from_mask(&ptr->ch_layout, AV_CH_LAYOUT_MONO);
av_frame_get_buffer(ptr.get(), 0); av_frame_get_buffer(ptr.get(), 0);
tone_.genSin(ptr.get(), 0, tones_[index].higher, tones_[index].lower); tone_.genSin(ptr.get(), 0, ptr->nb_samples, tones_[index].higher, tones_[index].lower);
return ptr; return ptr;
} }
......
...@@ -97,8 +97,7 @@ Tone::genBuffer(std::string_view definition) ...@@ -97,8 +97,7 @@ Tone::genBuffer(std::string_view definition)
if (definition.empty()) if (definition.empty())
return; return;
auto [total_samples, frequencies] = parseDefinition(definition, buffer_->sample_rate); auto [total_samples, frequencies] = parseDefinition(definition, format_.sample_rate);
buffer_->nb_samples = total_samples; buffer_->nb_samples = total_samples;
buffer_->format = format_.sampleFormat; buffer_->format = format_.sampleFormat;
buffer_->sample_rate = format_.sample_rate; buffer_->sample_rate = format_.sample_rate;
...@@ -107,13 +106,13 @@ Tone::genBuffer(std::string_view definition) ...@@ -107,13 +106,13 @@ Tone::genBuffer(std::string_view definition)
size_t outPos = 0; size_t outPos = 0;
for (auto& [low, high, count] : frequencies) { for (auto& [low, high, count] : frequencies) {
genSin(buffer_.get(), outPos, low, high); genSin(buffer_.get(), outPos, count, low, high);
outPos += count; outPos += count;
} }
} }
void void
Tone::genSin(AVFrame* buffer, unsigned outPos, unsigned lowFrequency, unsigned highFrequency) Tone::genSin(AVFrame* buffer, size_t outPos, unsigned nb_samples, unsigned lowFrequency, unsigned highFrequency)
{ {
static constexpr auto PI_2 = 3.141592653589793238462643383279502884L * 2.0L; static constexpr auto PI_2 = 3.141592653589793238462643383279502884L * 2.0L;
const double sr = (double) buffer->sample_rate; const double sr = (double) buffer->sample_rate;
...@@ -121,16 +120,15 @@ Tone::genSin(AVFrame* buffer, unsigned outPos, unsigned lowFrequency, unsigned h ...@@ -121,16 +120,15 @@ Tone::genSin(AVFrame* buffer, unsigned outPos, unsigned lowFrequency, unsigned h
const double dx_l = sr ? PI_2 * highFrequency / sr : 0.0; const double dx_l = sr ? PI_2 * highFrequency / sr : 0.0;
static constexpr double DATA_AMPLITUDE_S16 = 2048; static constexpr double DATA_AMPLITUDE_S16 = 2048;
static constexpr double DATA_AMPLITUDE_FLT = 0.0625; static constexpr double DATA_AMPLITUDE_FLT = 0.0625;
size_t nb = buffer->nb_samples;
if (buffer->format == AV_SAMPLE_FMT_S16 || buffer->format == AV_SAMPLE_FMT_S16P) { if (buffer->format == AV_SAMPLE_FMT_S16 || buffer->format == AV_SAMPLE_FMT_S16P) {
int16_t* ptr = ((int16_t*) buffer->data[0]) + outPos; int16_t* ptr = ((int16_t*) buffer->data[0]) + outPos;
for (size_t t = 0; t < nb; t++) { for (size_t t = 0; t < nb_samples; t++) {
ptr[t] = DATA_AMPLITUDE_S16 * (sin(t * dx_h) + sin(t * dx_l)); ptr[t] = DATA_AMPLITUDE_S16 * (sin(t * dx_h) + sin(t * dx_l));
} }
} else if (buffer->format == AV_SAMPLE_FMT_FLT || buffer->format == AV_SAMPLE_FMT_FLTP) { } else if (buffer->format == AV_SAMPLE_FMT_FLT || buffer->format == AV_SAMPLE_FMT_FLTP) {
float* ptr = ((float*) buffer->data[0]) + outPos; float* ptr = ((float*) buffer->data[0]) + outPos;
for (size_t t = 0; t < nb; t++) { for (size_t t = 0; t < nb_samples; t++) {
ptr[t] = (sin(t * dx_h) + sin(t * dx_l)) * DATA_AMPLITUDE_FLT; ptr[t] = (sin(t * dx_h) + sin(t * dx_l)) * DATA_AMPLITUDE_FLT;
} }
} else { } else {
......
...@@ -53,7 +53,7 @@ public: ...@@ -53,7 +53,7 @@ public:
* @param frequency2 The second frequency * @param frequency2 The second frequency
* @param nb the number of samples to generate * @param nb the number of samples to generate
*/ */
static void genSin(AVFrame* buffer, unsigned outPos, unsigned frequency1, unsigned frequency2); static void genSin(AVFrame* buffer, size_t outPos, unsigned nb_samples, unsigned frequency1, unsigned frequency2);
private: private:
/** /**
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment