1 |
/*************************************************************************** |
2 |
* * |
3 |
* LinuxSampler - modular, streaming capable sampler * |
4 |
* * |
5 |
* Copyright (C) 2003 by Benno Senoner * |
6 |
* * |
7 |
* This program is free software; you can redistribute it and/or modify * |
8 |
* it under the terms of the GNU General Public License as published by * |
9 |
* the Free Software Foundation; either version 2 of the License, or * |
10 |
* (at your option) any later version. * |
11 |
* * |
12 |
* This program is distributed in the hope that it will be useful, * |
13 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of * |
14 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
15 |
* GNU General Public License for more details. * |
16 |
* * |
17 |
* You should have received a copy of the GNU General Public License * |
18 |
* along with this program; if not, write to the Free Software * |
19 |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, * |
20 |
* MA 02111-1307 USA * |
21 |
***************************************************************************/ |
22 |
|
23 |
#include "stream.h" |
24 |
|
25 |
// FIXME: already independent of audio channels, but assumes sample depth defined by 'sample_t' (usually 16 bit) |
26 |
|
27 |
|
28 |
// *********** Stream ************** |
29 |
// * |
30 |
|
31 |
uint Stream::UnusedStreams = 0; |
32 |
|
33 |
/// Returns number of refilled sample points or a value < 0 on error. |
34 |
int Stream::ReadAhead(unsigned long SampleCount) { |
35 |
if (this->State == state_unused) return -1; |
36 |
if (this->State == state_end) return 0; |
37 |
if (!SampleCount) return 0; |
38 |
if (!pRingBuffer->write_space()) return 0; |
39 |
|
40 |
long total_readsamples = 0, readsamples = 0; |
41 |
long samplestoread = SampleCount / pSample->Channels; |
42 |
|
43 |
// refill the disk stream buffer |
44 |
pSample->SetPos(this->SampleOffset); |
45 |
|
46 |
sample_t* ptr = pRingBuffer->get_write_ptr(); |
47 |
do { |
48 |
readsamples = pSample->Read(&ptr[total_readsamples * pSample->Channels], samplestoread); |
49 |
samplestoread -= readsamples; |
50 |
total_readsamples += readsamples; |
51 |
} while (samplestoread && readsamples > 0); |
52 |
|
53 |
// we must delay the increment_write_ptr_with_wrap() after the while() loop because we need to |
54 |
// ensure that we read exactly SampleCount sample, otherwise the buffer wrapping code will fail |
55 |
pRingBuffer->increment_write_ptr_with_wrap(total_readsamples * pSample->Channels); |
56 |
|
57 |
// we have to store the position within the sample, because other streams might use the same sample |
58 |
this->SampleOffset = pSample->GetPos(); |
59 |
|
60 |
bool endofsamplereached = (SampleOffset >= pSample->SamplesTotal); |
61 |
|
62 |
// update stream state |
63 |
if (endofsamplereached) SetState(state_end); |
64 |
else SetState(state_active); |
65 |
|
66 |
// dmsg(("Refilled stream with %d (SamplePos: %d)", SampleCount - samplestoread, this->SampleOffset)); |
67 |
return (SampleCount - samplestoread); |
68 |
} |
69 |
|
70 |
void Stream::WriteSilence(unsigned long SilenceSampleWords) { |
71 |
memset(pRingBuffer->get_write_ptr(), 0, SilenceSampleWords); |
72 |
pRingBuffer->increment_write_ptr_with_wrap(SilenceSampleWords); |
73 |
} |
74 |
|
75 |
Stream::Stream(uint BufferSize, uint BufferWrapElements) { |
76 |
this->pExportReference = NULL; |
77 |
this->State = state_unused; |
78 |
this->pSample = NULL; |
79 |
this->SampleOffset = 0; |
80 |
this->pRingBuffer = new RingBuffer<sample_t>(BufferSize, BufferWrapElements); |
81 |
UnusedStreams++; |
82 |
} |
83 |
|
84 |
Stream::~Stream() { |
85 |
Reset(); |
86 |
if (pRingBuffer) delete pRingBuffer; |
87 |
} |
88 |
|
89 |
/// Called by disk thread to activate the disk stream. |
90 |
void Stream::Launch(reference_t* pExportReference, gig::Sample* pSample, unsigned long SampleOffset) { |
91 |
UnusedStreams--; |
92 |
this->pExportReference = pExportReference; |
93 |
this->pSample = pSample; |
94 |
this->SampleOffset = SampleOffset; |
95 |
SetState(state_active); |
96 |
} |