Line data Source code
1 : /* 2 : * Copyright (C) 2004-2024 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 : 32 : namespace jami { 33 : 34 465 : AudioLoop::AudioLoop(AudioFormat format) 35 465 : : format_(format) 36 465 : , buffer_(av_frame_alloc()) 37 465 : , pos_(0) 38 : { 39 465 : } 40 : 41 465 : AudioLoop::~AudioLoop() 42 465 : {} 43 : 44 : void 45 0 : AudioLoop::seek(double relative_position) 46 : { 47 0 : pos_ = static_cast<double>(getSize() * relative_position * 0.01); 48 0 : } 49 : 50 : void 51 0 : AudioLoop::getNext(AVFrame* output, bool mute) 52 : { 53 0 : if (!buffer_) { 54 0 : JAMI_ERR("buffer is NULL"); 55 0 : return; 56 : } 57 : 58 0 : const size_t buf_samples = buffer_->nb_samples; 59 0 : size_t pos = pos_; 60 0 : size_t total_samples = output->nb_samples; 61 0 : size_t output_pos = 0; 62 : 63 0 : if (buf_samples == 0) { 64 0 : JAMI_ERR("Audio loop size is 0"); 65 0 : av_samples_set_silence(output->data, 0, output->nb_samples, format_.nb_channels, format_.sampleFormat); 66 0 : return; 67 0 : } else if (pos >= buf_samples) { 68 0 : JAMI_ERR("Invalid loop position %zu", pos); 69 0 : return; 70 : } 71 : 72 0 : while (total_samples != 0) { 73 0 : size_t samples = std::min(total_samples, buf_samples - pos); 74 0 : if (not mute) 75 0 : av_samples_copy(output->data, buffer_->data, output_pos, pos, samples, format_.nb_channels, format_.sampleFormat); 76 : else 77 0 : av_samples_set_silence(output->data, output_pos, samples, format_.nb_channels, format_.sampleFormat); 78 0 : output_pos += samples; 79 0 : pos = (pos + samples) % buf_samples; 80 0 : total_samples -= samples; 81 : } 82 : 83 0 : pos_ = pos; 84 0 : onBufferFinish(); 85 : } 86 : 87 : void 88 0 : AudioLoop::onBufferFinish() 89 0 : {} 90 : 91 : std::unique_ptr<AudioFrame> 92 0 : AudioLoop::getNext(size_t samples, bool mute) 93 : { 94 0 : if (samples == 0) { 95 0 : samples = buffer_->sample_rate / 50; 96 : } 97 0 : auto buffer = std::make_unique<AudioFrame>(format_, samples); 98 0 : getNext(buffer->pointer(), mute); 99 0 : return buffer; 100 0 : } 101 : 102 : } // namespace jami