VST3: fetch midi mappings all at once, use it for note/sound-off
[carla.git] / source / modules / juce_core / streams / juce_SubregionStream.cpp
blobbb2572a1c17951c9200429d7fb6e10023f3e2f09
1 /*
2 ==============================================================================
4 This file is part of the JUCE library.
5 Copyright (c) 2022 - Raw Material Software Limited
7 JUCE is an open source library subject to commercial or open-source
8 licensing.
10 The code included in this file is provided under the terms of the ISC license
11 http://www.isc.org/downloads/software-support-policy/isc-license. Permission
12 To use, copy, modify, and/or distribute this software for any purpose with or
13 without fee is hereby granted provided that the above copyright notice and
14 this permission notice appear in all copies.
16 JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
17 EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
18 DISCLAIMED.
20 ==============================================================================
23 namespace juce
26 SubregionStream::SubregionStream (InputStream* sourceStream,
27 int64 start, int64 length,
28 bool deleteSourceWhenDestroyed)
29 : source (sourceStream, deleteSourceWhenDestroyed),
30 startPositionInSourceStream (start),
31 lengthOfSourceStream (length)
33 SubregionStream::setPosition (0);
36 SubregionStream::~SubregionStream()
40 int64 SubregionStream::getTotalLength()
42 auto srcLen = source->getTotalLength() - startPositionInSourceStream;
44 return lengthOfSourceStream >= 0 ? jmin (lengthOfSourceStream, srcLen)
45 : srcLen;
48 int64 SubregionStream::getPosition()
50 return source->getPosition() - startPositionInSourceStream;
53 bool SubregionStream::setPosition (int64 newPosition)
55 return source->setPosition (jmax ((int64) 0, newPosition + startPositionInSourceStream));
58 int SubregionStream::read (void* destBuffer, int maxBytesToRead)
60 jassert (destBuffer != nullptr && maxBytesToRead >= 0);
62 if (lengthOfSourceStream < 0)
63 return source->read (destBuffer, maxBytesToRead);
65 maxBytesToRead = (int) jmin ((int64) maxBytesToRead, lengthOfSourceStream - getPosition());
67 if (maxBytesToRead <= 0)
68 return 0;
70 return source->read (destBuffer, maxBytesToRead);
73 bool SubregionStream::isExhausted()
75 if (lengthOfSourceStream >= 0 && getPosition() >= lengthOfSourceStream)
76 return true;
78 return source->isExhausted();
82 //==============================================================================
83 //==============================================================================
84 #if JUCE_UNIT_TESTS
86 struct SubregionInputStreamTests : public UnitTest
88 SubregionInputStreamTests()
89 : UnitTest ("SubregionInputStream", UnitTestCategories::streams)
92 void runTest() override
94 const MemoryBlock data ("abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz", 52);
95 MemoryInputStream mi (data, true);
97 const int offset = getRandom().nextInt ((int) data.getSize());
98 const size_t subregionSize = data.getSize() - (size_t) offset;
100 SubregionStream stream (&mi, offset, (int) subregionSize, false);
102 beginTest ("Read");
104 expectEquals (stream.getPosition(), (int64) 0);
105 expectEquals (stream.getTotalLength(), (int64) subregionSize);
106 expectEquals (stream.getNumBytesRemaining(), stream.getTotalLength());
107 expect (! stream.isExhausted());
109 size_t numBytesRead = 0;
110 MemoryBlock readBuffer (subregionSize);
112 while (numBytesRead < subregionSize)
114 numBytesRead += (size_t) stream.read (&readBuffer[numBytesRead], 3);
116 expectEquals (stream.getPosition(), (int64) numBytesRead);
117 expectEquals (stream.getNumBytesRemaining(), (int64) (subregionSize - numBytesRead));
118 expect (stream.isExhausted() == (numBytesRead == subregionSize));
121 expectEquals (stream.getPosition(), (int64) subregionSize);
122 expectEquals (stream.getNumBytesRemaining(), (int64) 0);
123 expect (stream.isExhausted());
125 const MemoryBlock memoryBlockToCheck (data.begin() + (size_t) offset, data.getSize() - (size_t) offset);
126 expect (readBuffer == memoryBlockToCheck);
128 beginTest ("Skip");
130 stream.setPosition (0);
131 expectEquals (stream.getPosition(), (int64) 0);
132 expectEquals (stream.getTotalLength(), (int64) subregionSize);
133 expectEquals (stream.getNumBytesRemaining(), stream.getTotalLength());
134 expect (! stream.isExhausted());
136 numBytesRead = 0;
137 const int64 numBytesToSkip = 5;
139 while (numBytesRead < subregionSize)
141 stream.skipNextBytes (numBytesToSkip);
142 numBytesRead += numBytesToSkip;
143 numBytesRead = std::min (numBytesRead, subregionSize);
145 expectEquals (stream.getPosition(), (int64) numBytesRead);
146 expectEquals (stream.getNumBytesRemaining(), (int64) (subregionSize - numBytesRead));
147 expect (stream.isExhausted() == (numBytesRead == subregionSize));
150 expectEquals (stream.getPosition(), (int64) subregionSize);
151 expectEquals (stream.getNumBytesRemaining(), (int64) 0);
152 expect (stream.isExhausted());
156 static SubregionInputStreamTests subregionInputStreamTests;
158 #endif
160 } // namespace juce