OpenShot Library | libopenshot-audio  0.2.0
juce_AudioFormatReaderSource.cpp
1 /*
2  ==============================================================================
3 
4  This file is part of the JUCE library.
5  Copyright (c) 2017 - ROLI Ltd.
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 5 End-User License
11  Agreement and JUCE 5 Privacy Policy (both updated and effective as of the
12  27th April 2017).
13 
14  End User License Agreement: www.juce.com/juce-5-licence
15  Privacy Policy: www.juce.com/juce-5-privacy-policy
16 
17  Or: You may also use this code under the terms of the GPL v3 (see
18  www.gnu.org/licenses).
19 
20  JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
21  EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
22  DISCLAIMED.
23 
24  ==============================================================================
25 */
26 
27 namespace juce
28 {
29 
31  const bool deleteReaderWhenThisIsDeleted)
32  : reader (r, deleteReaderWhenThisIsDeleted),
33  nextPlayPos (0),
34  looping (false)
35 {
36  jassert (reader != nullptr);
37 }
38 
40 
41 int64 AudioFormatReaderSource::getTotalLength() const { return reader->lengthInSamples; }
42 void AudioFormatReaderSource::setNextReadPosition (int64 newPosition) { nextPlayPos = newPosition; }
43 void AudioFormatReaderSource::setLooping (bool shouldLoop) { looping = shouldLoop; }
44 
46 {
47  return looping ? nextPlayPos % reader->lengthInSamples
48  : nextPlayPos;
49 }
50 
51 void AudioFormatReaderSource::prepareToPlay (int /*samplesPerBlockExpected*/, double /*sampleRate*/) {}
53 
55 {
56  if (info.numSamples > 0)
57  {
58  const int64 start = nextPlayPos;
59 
60  if (looping)
61  {
62  const int64 newStart = start % reader->lengthInSamples;
63  const int64 newEnd = (start + info.numSamples) % reader->lengthInSamples;
64 
65  if (newEnd > newStart)
66  {
67  reader->read (info.buffer, info.startSample,
68  (int) (newEnd - newStart), newStart, true, true);
69  }
70  else
71  {
72  const int endSamps = (int) (reader->lengthInSamples - newStart);
73 
74  reader->read (info.buffer, info.startSample,
75  endSamps, newStart, true, true);
76 
77  reader->read (info.buffer, info.startSample + endSamps,
78  (int) newEnd, 0, true, true);
79  }
80 
81  nextPlayPos = newEnd;
82  }
83  else
84  {
85  reader->read (info.buffer, info.startSample,
86  info.numSamples, start, true, true);
87  nextPlayPos += info.numSamples;
88  }
89  }
90 }
91 
92 } // namespace juce
int numSamples
The number of samples in the buffer which the callback is expected to fill with data.
void releaseResources() override
Implementation of the AudioSource method.
AudioFormatReaderSource(AudioFormatReader *sourceReader, bool deleteReaderWhenThisIsDeleted)
Creates an AudioFormatReaderSource for a given reader.
int64 getTotalLength() const override
Implements the PositionableAudioSource method.
int startSample
The first sample in the buffer from which the callback is expected to write data. ...
void getNextAudioBlock(const AudioSourceChannelInfo &) override
Implementation of the AudioSource method.
AudioBuffer< float > * buffer
The destination buffer to fill with audio data.
void prepareToPlay(int samplesPerBlockExpected, double sampleRate) override
Implementation of the AudioSource method.
void setLooping(bool shouldLoop) override
Toggles loop-mode.
int64 getNextReadPosition() const override
Implements the PositionableAudioSource method.
Reads samples from an audio file stream.
Used by AudioSource::getNextAudioBlock().
void setNextReadPosition(int64 newPosition) override
Implements the PositionableAudioSource method.