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 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 sf2 { |
32 |
|
33 |
Stream::Stream ( |
34 |
uint BufferSize, |
35 |
uint BufferWrapElements |
36 |
) : LinuxSampler::StreamBase< ::sf2::Region>(BufferSize, BufferWrapElements) { |
37 |
|
38 |
} |
39 |
|
40 |
long Stream::Read(uint8_t* pBuf, long SamplesToRead) { |
41 |
::sf2::Sample* pSample = pRegion->pSample; |
42 |
long total_readsamples = 0, readsamples = 0; |
43 |
bool endofsamplereached; |
44 |
|
45 |
// refill the disk stream buffer |
46 |
if (this->DoLoop) { // honor looping |
47 |
::sf2::Sample::PlaybackState pbs; |
48 |
pbs.position = PlaybackState.position; |
49 |
pbs.reverse = PlaybackState.reverse; |
50 |
pbs.loop_cycles_left = PlaybackState.loop_cycles_left; |
51 |
|
52 |
total_readsamples = pSample->ReadAndLoop(pBuf, SamplesToRead, &pbs); |
53 |
PlaybackState.position = pbs.position; |
54 |
PlaybackState.reverse = pbs.reverse; |
55 |
PlaybackState.loop_cycles_left = pbs.loop_cycles_left; |
56 |
endofsamplereached = (this->PlaybackState.position >= pSample->GetTotalFrameCount()); |
57 |
dmsg(5,("Refilled stream %d with %d (SamplePos: %d)", this->hThis, total_readsamples, this->PlaybackState.position)); |
58 |
} |
59 |
else { // normal forward playback |
60 |
|
61 |
pSample->SetPos(this->SampleOffset); // recover old position |
62 |
|
63 |
do { |
64 |
readsamples = pSample->Read(&pBuf[total_readsamples * pSample->GetFrameSize()], SamplesToRead); |
65 |
SamplesToRead -= readsamples; |
66 |
total_readsamples += readsamples; |
67 |
} while (SamplesToRead && readsamples > 0); |
68 |
|
69 |
// we have to store the position within the sample, because other streams might use the same sample |
70 |
this->SampleOffset = pSample->GetPos(); |
71 |
|
72 |
endofsamplereached = (SampleOffset >= pSample->GetTotalFrameCount()); |
73 |
dmsg(5,("Refilled stream %d with %d (SamplePos: %d)", this->hThis, total_readsamples, this->SampleOffset)); |
74 |
} |
75 |
|
76 |
// update stream state |
77 |
if (endofsamplereached) SetState(state_end); |
78 |
else SetState(state_active); |
79 |
|
80 |
return total_readsamples; |
81 |
} |
82 |
|
83 |
void Stream::Kill() { |
84 |
StreamBase< ::sf2::Region>::Kill(); |
85 |
} |
86 |
|
87 |
void Stream::Launch ( |
88 |
Stream::Handle hStream, |
89 |
reference_t* pExportReference, |
90 |
::sf2::Region* pRgn, |
91 |
unsigned long SampleOffset, |
92 |
bool DoLoop |
93 |
) { |
94 |
SampleDescription info; |
95 |
info.ChannelsPerFrame = pRgn->pSample->GetChannelCount(); |
96 |
info.FrameSize = pRgn->pSample->GetFrameSize(); |
97 |
info.BytesPerSample = pRgn->pSample->GetFrameSize() / pRgn->pSample->GetChannelCount(); |
98 |
info.TotalSampleCount = pRgn->pSample->GetTotalFrameCount(); |
99 |
|
100 |
Sample::PlaybackState playbackState; |
101 |
playbackState.position = SampleOffset; |
102 |
playbackState.reverse = false; |
103 |
playbackState.loop_cycles_left = 0; // TODO: pRgn->pSample->LoopPlayCount; |
104 |
|
105 |
LinuxSampler::StreamBase< ::sf2::Region>::Launch ( |
106 |
hStream, pExportReference, pRgn, info, playbackState, SampleOffset, DoLoop |
107 |
); |
108 |
} |
109 |
|
110 |
}} // namespace LinuxSampler::sf2 |