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