Ring Daemon
Loading...
Searching...
No Matches
audioloop.cpp
Go to the documentation of this file.
1/*
2 * Copyright (C) 2004-2026 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
41
42void
44{
45 pos_ = static_cast<size_t>(static_cast<double>(getSize()) * relative_position * 0.01);
46}
47
48void
50{
51 if (!buffer_) {
52 JAMI_ERR("buffer is NULL");
53 return;
54 }
55
56 const size_t buf_samples = buffer_->nb_samples;
57 size_t pos = pos_;
58 size_t total_samples = output->nb_samples;
59 size_t output_pos = 0;
60
61 if (buf_samples == 0) {
62 JAMI_ERR("Audio loop size is 0");
64 0,
65 output->nb_samples,
66 static_cast<int>(format_.nb_channels),
68 return;
69 } else if (pos >= buf_samples) {
70 JAMI_ERR("Invalid loop position %zu", pos);
71 return;
72 }
73
74 while (total_samples != 0) {
75 size_t samples = std::min(total_samples, buf_samples - pos);
76 if (not mute)
78 buffer_->data,
79 static_cast<int>(output_pos),
80 static_cast<int>(pos),
81 static_cast<int>(samples),
82 static_cast<int>(format_.nb_channels),
84 else
86 static_cast<int>(output_pos),
87 static_cast<int>(samples),
88 static_cast<int>(format_.nb_channels),
90 output_pos += samples;
91 pos = (pos + samples) % buf_samples;
92 total_samples -= samples;
93 }
94
95 pos_ = pos;
96 onBufferFinish();
97}
98
99void
100AudioLoop::onBufferFinish()
101{}
102
103std::unique_ptr<AudioFrame>
104AudioLoop::getNext(size_t samples, bool mute)
105{
106 if (samples == 0) {
107 samples = buffer_->sample_rate / 50;
108 }
109 auto buffer = std::make_unique<AudioFrame>(format_, samples);
110 getNext(buffer->pointer(), mute);
111 return buffer;
112}
113
114} // namespace jami
Loop on a sound file.
size_t getSize() const
Accessor to the size of the buffer.
Definition audioloop.h:72
virtual ~AudioLoop()
Definition audioloop.cpp:40
size_t pos_
current position, set to 0, when initialize
Definition audioloop.h:81
void seek(double relative_position)
Definition audioloop.cpp:43
AudioFormat format_
Definition audioloop.h:76
AudioLoop(AudioFormat format)
Definition audioloop.cpp:34
libjami::FrameBuffer buffer_
The data buffer.
Definition audioloop.h:78
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:49
#define JAMI_ERR(...)
Definition logger.h:230
void emitSignal(Args... args)
Definition jami_signal.h:64
Structure to hold sample rate and channel number associated with audio data.
AVSampleFormat sampleFormat