Line data Source code
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 : *
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 "noncopyable.h"
23 : #include "audio_format.h"
24 : #include "media_buffer.h"
25 :
26 : extern "C" {
27 : #include <libavutil/frame.h>
28 : }
29 :
30 : /**
31 : * @file audioloop.h
32 : * @brief Loop on a sound file
33 : */
34 :
35 : namespace jami {
36 :
37 : class AudioLoop
38 : {
39 : public:
40 : AudioLoop(AudioFormat format);
41 :
42 : AudioLoop& operator=(AudioLoop&& o) noexcept
43 : {
44 : std::swap(buffer_, o.buffer_);
45 : std::swap(pos_, o.pos_);
46 : return *this;
47 : }
48 :
49 : virtual ~AudioLoop();
50 :
51 : /**
52 : * Get the next fragment of the tone
53 : * the function change the intern position, and will loop
54 : * @param output The data buffer
55 : * @param nb of int16 to send
56 : * @param gain The gain [-1.0, 1.0]
57 : */
58 : void getNext(AVFrame* output, bool mute);
59 : std::unique_ptr<AudioFrame> getNext(size_t samples = 0, bool mute = false);
60 :
61 : void seek(double relative_position);
62 :
63 : /**
64 : * Reset the pointer position
65 : */
66 63 : void reset() { pos_ = 0; }
67 :
68 : /**
69 : * Accessor to the size of the buffer
70 : * @return unsigned int The size
71 : */
72 0 : size_t getSize() const { return buffer_->nb_samples; }
73 512 : AudioFormat getFormat() const { return format_; }
74 :
75 : protected:
76 : AudioFormat format_;
77 : /** The data buffer */
78 : libjami::FrameBuffer buffer_ {};
79 :
80 : /** current position, set to 0, when initialize */
81 : size_t pos_ {0};
82 :
83 : private:
84 : NON_COPYABLE(AudioLoop);
85 : virtual void onBufferFinish();
86 : };
87 :
88 : } // namespace jami
|