1 |
/*************************************************************************** |
2 |
* * |
3 |
* LinuxSampler - modular, streaming capable sampler * |
4 |
* * |
5 |
* Copyright (C) 2003,2004 by Benno Senoner and Christian Schoenebeck * |
6 |
* Copyright (C) 2005-2009 Christian Schoenebeck * |
7 |
* Copyright (C) 2009 Christian Schoenebeck and Grigor Iliev * |
8 |
* Copyright (C) 2016 Christian Schoenebeck * |
9 |
* * |
10 |
* This program is free software; you can redistribute it and/or modify * |
11 |
* it under the terms of the GNU General Public License as published by * |
12 |
* the Free Software Foundation; either version 2 of the License, or * |
13 |
* (at your option) any later version. * |
14 |
* * |
15 |
* This program is distributed in the hope that it will be useful, * |
16 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of * |
17 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
18 |
* GNU General Public License for more details. * |
19 |
* * |
20 |
* You should have received a copy of the GNU General Public License * |
21 |
* along with this program; if not, write to the Free Software * |
22 |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, * |
23 |
* MA 02111-1307 USA * |
24 |
***************************************************************************/ |
25 |
|
26 |
#include "Stream.h" |
27 |
#include "../../common/global_private.h" |
28 |
|
29 |
namespace LinuxSampler { namespace gig { |
30 |
|
31 |
Stream::Stream ( |
32 |
::gig::buffer_t* pDecompressionBuffer, |
33 |
uint BufferSize, |
34 |
uint BufferWrapElements) : LinuxSampler::StreamBase< ::gig::DimensionRegion>(BufferSize, BufferWrapElements) |
35 |
{ |
36 |
this->pDecompressionBuffer = pDecompressionBuffer; |
37 |
} |
38 |
|
39 |
long Stream::Read(uint8_t* pBuf, long SamplesToRead) { |
40 |
::gig::Sample* pSample = pRegion->pSample; |
41 |
long total_readsamples = 0, readsamples = 0; |
42 |
bool endofsamplereached; |
43 |
|
44 |
// refill the disk stream buffer |
45 |
if (this->DoLoop) { // honor looping |
46 |
::gig::playback_state_t pbs; |
47 |
pbs.position = PlaybackState.position; |
48 |
pbs.reverse = PlaybackState.reverse; |
49 |
pbs.loop_cycles_left = PlaybackState.loop_cycles_left; |
50 |
|
51 |
total_readsamples = pSample->ReadAndLoop(pBuf, SamplesToRead, &pbs, pRegion, pDecompressionBuffer); |
52 |
PlaybackState.position = pbs.position; |
53 |
PlaybackState.reverse = pbs.reverse; |
54 |
PlaybackState.loop_cycles_left = pbs.loop_cycles_left; |
55 |
endofsamplereached = (this->PlaybackState.position >= pSample->SamplesTotal); |
56 |
dmsg(5,("Refilled stream %d with %ld (SamplePos: %lu)", this->hThis, total_readsamples, this->PlaybackState.position)); |
57 |
} |
58 |
else { // normal forward playback |
59 |
|
60 |
pSample->SetPos(this->SampleOffset); // recover old position |
61 |
|
62 |
do { |
63 |
readsamples = pSample->Read(&pBuf[total_readsamples * pSample->FrameSize], SamplesToRead, pDecompressionBuffer); |
64 |
SamplesToRead -= readsamples; |
65 |
total_readsamples += readsamples; |
66 |
} while (SamplesToRead && readsamples > 0); |
67 |
|
68 |
// we have to store the position within the sample, because other streams might use the same sample |
69 |
this->SampleOffset = pSample->GetPos(); |
70 |
|
71 |
endofsamplereached = (SampleOffset >= pSample->SamplesTotal); |
72 |
dmsg(5,("Refilled stream %d with %ld (SamplePos: %lu)", this->hThis, total_readsamples, this->SampleOffset)); |
73 |
} |
74 |
|
75 |
// update stream state |
76 |
if (endofsamplereached) SetState(state_end); |
77 |
else SetState(state_active); |
78 |
|
79 |
return total_readsamples; |
80 |
} |
81 |
|
82 |
void Stream::Launch ( |
83 |
Stream::Handle hStream, |
84 |
reference_t* pExportReference, |
85 |
::gig::DimensionRegion* pRgn, |
86 |
unsigned long SampleOffset, |
87 |
bool DoLoop |
88 |
) { |
89 |
SampleDescription info; |
90 |
info.ChannelsPerFrame = pRgn->pSample->Channels; |
91 |
info.FrameSize = pRgn->pSample->FrameSize; |
92 |
info.BytesPerSample = pRgn->pSample->BitDepth / 8; |
93 |
info.TotalSampleCount = (int)pRgn->pSample->SamplesTotal; |
94 |
|
95 |
Sample::PlaybackState playbackState; |
96 |
playbackState.position = SampleOffset; |
97 |
playbackState.reverse = false; |
98 |
playbackState.loop_cycles_left = pRgn->pSample->LoopPlayCount; |
99 |
|
100 |
LinuxSampler::StreamBase< ::gig::DimensionRegion>::Launch ( |
101 |
hStream, pExportReference, pRgn, info, playbackState, SampleOffset, DoLoop |
102 |
); |
103 |
} |
104 |
|
105 |
}} // namespace LinuxSampler::gig |