OpenShot Audio Library | OpenShotAudio  0.6.0
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 
26 namespace juce
27 {
28 
30  const bool deleteReaderWhenThisIsDeleted)
31  : reader (r, deleteReaderWhenThisIsDeleted),
32  nextPlayPos (0),
33  looping (false)
34 {
35  jassert (reader != nullptr);
36 }
37 
39 
40 int64 AudioFormatReaderSource::getTotalLength() const { return reader->lengthInSamples; }
41 void AudioFormatReaderSource::setNextReadPosition (int64 newPosition) { nextPlayPos = newPosition; }
42 void AudioFormatReaderSource::setLooping (bool shouldLoop) { looping = shouldLoop; }
43 
45 {
46  return looping ? nextPlayPos % reader->lengthInSamples
47  : nextPlayPos;
48 }
49 
50 void AudioFormatReaderSource::prepareToPlay (int /*samplesPerBlockExpected*/, double /*sampleRate*/) {}
52 
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
void prepareToPlay(int samplesPerBlockExpected, double sampleRate) override
AudioFormatReaderSource(AudioFormatReader *sourceReader, bool deleteReaderWhenThisIsDeleted)
void setLooping(bool shouldLoop) override
void setNextReadPosition(int64 newPosition) override
void getNextAudioBlock(const AudioSourceChannelInfo &) override
AudioBuffer< float > * buffer