Ring Daemon 16.0.0
Loading...
Searching...
No Matches
audioloop.cpp
Go to the documentation of this file.
1/*
2 * Copyright (C) 2004-2025 Savoir-faire Linux Inc.
3 *
4 * Inspired by tonegenerator of
5 * Laurielle Lea <laurielle.lea@savoirfairelinux.com> (2004)
6 * Inspired by ringbuffer of Audacity Project
7 *
8 * This program is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation, either version 3 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <https://www.gnu.org/licenses/>.
20 */
21
22#ifdef HAVE_CONFIG_H
23#include "config.h"
24#endif
25
26#include "audioloop.h"
27#include "logger.h"
28#include "libav_deps.h"
29
30#include <algorithm> // std::min
31
32namespace jami {
33
35 : format_(format)
36 , buffer_(av_frame_alloc())
37 , pos_(0)
38{
39}
40
43
44void
46{
47 pos_ = static_cast<double>(getSize() * relative_position * 0.01);
48}
49
50void
52{
53 if (!buffer_) {
54 JAMI_ERR("buffer is NULL");
55 return;
56 }
57
58 const size_t buf_samples = buffer_->nb_samples;
59 size_t pos = pos_;
60 size_t total_samples = output->nb_samples;
61 size_t output_pos = 0;
62
63 if (buf_samples == 0) {
64 JAMI_ERR("Audio loop size is 0");
66 return;
67 } else if (pos >= buf_samples) {
68 JAMI_ERR("Invalid loop position %zu", pos);
69 return;
70 }
71
72 while (total_samples != 0) {
73 size_t samples = std::min(total_samples, buf_samples - pos);
74 if (not mute)
76 else
78 output_pos += samples;
79 pos = (pos + samples) % buf_samples;
80 total_samples -= samples;
81 }
82
83 pos_ = pos;
84 onBufferFinish();
85}
86
87void
88AudioLoop::onBufferFinish()
89{}
90
91std::unique_ptr<AudioFrame>
92AudioLoop::getNext(size_t samples, bool mute)
93{
94 if (samples == 0) {
95 samples = buffer_->sample_rate / 50;
96 }
97 auto buffer = std::make_unique<AudioFrame>(format_, samples);
98 getNext(buffer->pointer(), mute);
99 return buffer;
100}
101
102} // namespace jami
Loop on a sound file.
size_t getSize() const
Accessor to the size of the buffer.
Definition audioloop.h:73
virtual ~AudioLoop()
Definition audioloop.cpp:41
size_t pos_
current position, set to 0, when initialize
Definition audioloop.h:82
void seek(double relative_position)
Definition audioloop.cpp:45
AudioFormat format_
Definition audioloop.h:77
AudioLoop(AudioFormat format)
Definition audioloop.cpp:34
libjami::FrameBuffer buffer_
The data buffer.
Definition audioloop.h:79
void getNext(AVFrame *output, bool mute)
Get the next fragment of the tone the function change the intern position, and will loop.
Definition audioloop.cpp:51
#define JAMI_ERR(...)
Definition logger.h:218
void emitSignal(Args... args)
Definition ring_signal.h:64
Structure to hold sample rate and channel number associated with audio data.
AVSampleFormat sampleFormat