OpenShot Audio Library | OpenShotAudio: juce_AudioFormatReaderSource.cpp Source File

OpenShot Audio Library | OpenShotAudio 0.4.0

Loading...
Searching...
No Matches
juce_AudioFormatReaderSource.cpp
1/*
2 ==============================================================================
3
4 This file is part of the JUCE library.
5 Copyright (c) 2022 - Raw Material Software Limited
6
7 JUCE is an open source library subject to commercial or open-source
8 licensing.
9
10 By using JUCE, you agree to the terms of both the JUCE 7 End-User License
11 Agreement and JUCE Privacy Policy.
12
13 End User License Agreement: www.juce.com/juce-7-licence
14 Privacy Policy: www.juce.com/juce-privacy-policy
15
16 Or: You may also use this code under the terms of the GPL v3 (see
17 www.gnu.org/licenses).
18
19 JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
20 EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
21 DISCLAIMED.
22
23 ==============================================================================
24*/
25
26namespace juce
27{
28
29AudioFormatReaderSource::AudioFormatReaderSource (AudioFormatReader* const r,
30 const bool deleteReaderWhenThisIsDeleted)
31 : reader (r, deleteReaderWhenThisIsDeleted),
32 nextPlayPos (0),
33 looping (false)
34{
35 jassert (reader != nullptr);
36}
37
38AudioFormatReaderSource::~AudioFormatReaderSource() {}
39
40int64 AudioFormatReaderSource::getTotalLength() const { return reader->lengthInSamples; }
41 void AudioFormatReaderSource::setNextReadPosition (int64 newPosition) { nextPlayPos = newPosition; }
42 void AudioFormatReaderSource::setLooping (bool shouldLoop) { looping = shouldLoop; }
43
44int64 AudioFormatReaderSource::getNextReadPosition() const
45{
46 return looping ? nextPlayPos % reader->lengthInSamples
47 : nextPlayPos;
48}
49
50 void AudioFormatReaderSource::prepareToPlay (int /*samplesPerBlockExpected*/, double /*sampleRate*/) {}
51 void AudioFormatReaderSource::releaseResources() {}
52
53 void AudioFormatReaderSource::getNextAudioBlock (const AudioSourceChannelInfo& info)
54{
55 if (info.numSamples > 0)
56 {
57 const int64 start = nextPlayPos;
58
59 if (looping)
60 {
61 const int64 newStart = start % reader->lengthInSamples;
62 const int64 newEnd = (start + info.numSamples) % reader->lengthInSamples;
63
64 if (newEnd > newStart)
65 {
66 reader->read (info.buffer, info.startSample,
67 (int) (newEnd - newStart), newStart, true, true);
68 }
69 else
70 {
71 const int endSamps = (int) (reader->lengthInSamples - newStart);
72
73 reader->read (info.buffer, info.startSample,
74 endSamps, newStart, true, true);
75
76 reader->read (info.buffer, info.startSample + endSamps,
77 (int) newEnd, 0, true, true);
78 }
79
80 nextPlayPos = newEnd;
81 }
82 else
83 {
84 const auto samplesToRead = jlimit (int64{},
85 (int64) info.numSamples,
86 reader->lengthInSamples - start);
87
88 reader->read (info.buffer, info.startSample, (int) samplesToRead, start, true, true);
89 info.buffer->clear ((int) (info.startSample + samplesToRead),
90 (int) (info.numSamples - samplesToRead));
91
92 nextPlayPos += info.numSamples;
93 }
94 }
95}
96
97} // namespace juce
juce::AudioBuffer::clear
void clear() noexcept
Definition juce_AudioSampleBuffer.h:556
juce::AudioFormatReaderSource::getNextReadPosition
int64 getNextReadPosition() const override
Definition juce_AudioFormatReaderSource.cpp:44
juce::AudioFormatReaderSource::prepareToPlay
void prepareToPlay(int samplesPerBlockExpected, double sampleRate) override
Definition juce_AudioFormatReaderSource.cpp:50
juce::AudioFormatReaderSource::releaseResources
void releaseResources() override
Definition juce_AudioFormatReaderSource.cpp:51
juce::AudioFormatReaderSource::~AudioFormatReaderSource
~AudioFormatReaderSource() override
Definition juce_AudioFormatReaderSource.cpp:38
juce::AudioFormatReaderSource::AudioFormatReaderSource
AudioFormatReaderSource(AudioFormatReader *sourceReader, bool deleteReaderWhenThisIsDeleted)
Definition juce_AudioFormatReaderSource.cpp:29
juce::AudioFormatReaderSource::setLooping
void setLooping(bool shouldLoop) override
Definition juce_AudioFormatReaderSource.cpp:42
juce::AudioFormatReaderSource::setNextReadPosition
void setNextReadPosition(int64 newPosition) override
Definition juce_AudioFormatReaderSource.cpp:41
juce::AudioFormatReaderSource::getTotalLength
int64 getTotalLength() const override
Definition juce_AudioFormatReaderSource.cpp:40
juce::AudioFormatReaderSource::getNextAudioBlock
void getNextAudioBlock(const AudioSourceChannelInfo &) override
Definition juce_AudioFormatReaderSource.cpp:53
juce::AudioFormatReader
Definition juce_AudioFormatReader.h:44
juce::AudioSourceChannelInfo
Definition juce_AudioSource.h:33
juce::AudioSourceChannelInfo::numSamples
int numSamples
Definition juce_AudioSource.h:81
juce::AudioSourceChannelInfo::buffer
AudioBuffer< float > * buffer
Definition juce_AudioSource.h:73
juce::AudioSourceChannelInfo::startSample
int startSample
Definition juce_AudioSource.h:77