Ring Daemon 16.0.0
Loading...
Searching...
No Matches
audiofile.cpp
Go to the documentation of this file.
1/* Copyright (C) 2004-2025 Savoir-faire Linux Inc.
2 *
3 * Inspired by tonegenerator of
4 * Laurielle Lea <laurielle.lea@savoirfairelinux.com> (2004)
5 * Inspired by ringbuffer of Audacity Project
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#include <fstream>
21#include <cmath>
22#include <cstring>
23#include <vector>
24#include <climits>
25
26#include "libav_deps.h"
27#include "audiofile.h"
28#include "audio/resampler.h"
29#include "manager.h"
30#include "media_decoder.h"
31#include "client/ring_signal.h"
32
33#include "logger.h"
34
35namespace jami {
36
37void
38AudioFile::onBufferFinish()
39{
40 if (buffer_->sample_rate == 0) {
41 JAMI_ERR("Error unable to update playback slider, sampling rate is 0");
42 return;
43 }
44
45 // We want to send values in milisecond
46 if ((updatePlaybackScale_ % 5) == 0)
48 (unsigned) (1000lu * pos_ / buffer_->sample_rate),
49 (unsigned) (1000lu * buffer_->nb_samples / buffer_->sample_rate));
50
51 updatePlaybackScale_++;
52}
53
54AudioFile::AudioFile(const std::string& fileName, unsigned int sampleRate, AVSampleFormat sampleFormat)
55 : AudioLoop(AudioFormat(sampleRate, 1, sampleFormat))
56 , filepath_(fileName)
57 , updatePlaybackScale_(0)
58{
59 std::list<std::shared_ptr<AudioFrame>> buf;
60 size_t total_samples = 0;
61
62 auto start = std::chrono::steady_clock::now();
63 Resampler r {};
64 auto decoder = std::make_unique<MediaDecoder>(
65 [&r, this, &buf, &total_samples](const std::shared_ptr<MediaFrame>& frame) mutable {
66 auto resampled = r.resample(std::static_pointer_cast<AudioFrame>(frame), format_);
67 total_samples += resampled->getFrameSize();
68 buf.emplace_back(std::move(resampled));
69 });
71 dev.input = fileName;
72 dev.name = fileName;
73
74 if (decoder->openInput(dev) < 0)
75 throw AudioFileException("Unable to open file: " + fileName);
76
77 if (decoder->setupAudio() < 0)
78 throw AudioFileException("Decoder setup failed: " + fileName);
79
80 while (decoder->decode() != MediaDemuxer::Status::EndOfFile)
81 ;
82
83 buffer_->nb_samples = total_samples;
85 buffer_->sample_rate = format_.sample_rate;
88
89 size_t outPos = 0;
90 for (auto& frame : buf) {
91 av_samples_copy(buffer_->data, frame->pointer()->data, outPos, 0, frame->getFrameSize(), format_.nb_channels, format_.sampleFormat);
92 outPos += frame->getFrameSize();
93 }
94 auto end = std::chrono::steady_clock::now();
95 auto audioDuration = std::chrono::duration<double>(total_samples/(double)format_.sample_rate);
96 JAMI_LOG("AudioFile: loaded {} samples ({}) as {} in {} from {:s}",
97 total_samples, audioDuration, format_.toString(), dht::print_duration(end-start), fileName);
98}
99
100} // namespace jami
std::string filepath_
The absolute path to the sound file.
Definition audiofile.h:48
AudioFile(const std::string &filepath, unsigned int sampleRate, AVSampleFormat sampleFormat)
Definition audiofile.cpp:54
size_t pos_
current position, set to 0, when initialize
Definition audioloop.h:82
AudioFormat format_
Definition audioloop.h:77
libjami::FrameBuffer buffer_
The data buffer.
Definition audioloop.h:79
Wrapper class for libswresample.
Definition resampler.h:36
#define JAMI_ERR(...)
Definition logger.h:218
#define JAMI_LOG(formatstr,...)
Definition logger.h:225
Definition Address.h:25
void emitSignal(Args... args)
Definition ring_signal.h:64
Structure to hold sample rate and channel number associated with audio data.
AVSampleFormat sampleFormat
std::string toString() const
DeviceParams Parameters used by MediaDecoder and MediaEncoder to open a LibAV device/stream.