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 : * 7 : * This program is free software: you can redistribute it and/or modify 8 : * it under the terms of the GNU General Public License as published by 9 : * the Free Software Foundation, either version 3 of the License, or 10 : * (at your option) any later version. 11 : * 12 : * This program is distributed in the hope that it will be useful, 13 : * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 : * GNU General Public License for more details. 16 : * 17 : * You should have received a copy of the GNU General Public License 18 : * along with this program. If not, see <https://www.gnu.org/licenses/>. 19 : */ 20 : #pragma once 21 : 22 : #include "ring_types.h" 23 : #include "noncopyable.h" 24 : #include "audio_format.h" 25 : #include "media_buffer.h" 26 : 27 : extern "C" { 28 : #include <libavutil/frame.h> 29 : } 30 : 31 : /** 32 : * @file audioloop.h 33 : * @brief Loop on a sound file 34 : */ 35 : 36 : namespace jami { 37 : 38 : class AudioLoop 39 : { 40 : public: 41 : AudioLoop(AudioFormat format); 42 : 43 : AudioLoop& operator=(AudioLoop&& o) noexcept 44 : { 45 : std::swap(buffer_, o.buffer_); 46 : std::swap(pos_, o.pos_); 47 : return *this; 48 : } 49 : 50 : virtual ~AudioLoop(); 51 : 52 : /** 53 : * Get the next fragment of the tone 54 : * the function change the intern position, and will loop 55 : * @param output The data buffer 56 : * @param nb of int16 to send 57 : * @param gain The gain [-1.0, 1.0] 58 : */ 59 : void getNext(AVFrame* output, bool mute); 60 : std::unique_ptr<AudioFrame> getNext(size_t samples = 0, bool mute = false); 61 : 62 : void seek(double relative_position); 63 : 64 : /** 65 : * Reset the pointer position 66 : */ 67 70 : void reset() { pos_ = 0; } 68 : 69 : /** 70 : * Accessor to the size of the buffer 71 : * @return unsigned int The size 72 : */ 73 0 : size_t getSize() const { return buffer_->nb_samples; } 74 528 : AudioFormat getFormat() const { return format_; } 75 : 76 : protected: 77 : AudioFormat format_; 78 : /** The data buffer */ 79 : libjami::FrameBuffer buffer_ {}; 80 : 81 : /** current position, set to 0, when initialize */ 82 : size_t pos_ {0}; 83 : 84 : private: 85 : NON_COPYABLE(AudioLoop); 86 : virtual void onBufferFinish(); 87 : }; 88 : 89 : } // namespace jami