1 |
iliev |
2012 |
/*************************************************************************** |
2 |
|
|
* * |
3 |
|
|
* LinuxSampler - modular, streaming capable sampler * |
4 |
|
|
* * |
5 |
|
|
* Copyright (C) 2003, 2004 by Benno Senoner and Christian Schoenebeck * |
6 |
schoenebeck |
3054 |
* Copyright (C) 2005 - 2016 Christian Schoenebeck * |
7 |
iliev |
2012 |
* 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 |
|
|
#ifndef __LS_STREAMBASE_H__ |
26 |
|
|
#define __LS_STREAMBASE_H__ |
27 |
|
|
|
28 |
|
|
#include "Stream.h" |
29 |
|
|
#include "../../common/global.h" |
30 |
|
|
#include "../../common/RingBuffer.h" |
31 |
|
|
#include <iostream> |
32 |
|
|
|
33 |
|
|
namespace LinuxSampler { |
34 |
|
|
|
35 |
|
|
/** @brief Buffered Disk Stream |
36 |
|
|
* |
37 |
|
|
* This encapsulation of a disk stream uses a ring buffer to allow |
38 |
|
|
* thread safe refilling the stream's buffer with one thread (disk |
39 |
|
|
* thread) and actual use / extraction of the audio data from the |
40 |
|
|
* stream's buffer with another thread (audio thread). |
41 |
|
|
*/ |
42 |
|
|
template <class R> |
43 |
|
|
class StreamBase : public Stream { |
44 |
|
|
public: |
45 |
|
|
// Methods |
46 |
|
|
StreamBase(uint BufferSize, uint BufferWrapElements) : Stream(BufferSize, BufferWrapElements) { |
47 |
|
|
this->pRegion = NULL; |
48 |
|
|
this->SampleOffset = 0; |
49 |
|
|
} |
50 |
|
|
|
51 |
|
|
virtual ~StreamBase() { |
52 |
|
|
Reset(); |
53 |
|
|
if (pRingBuffer) delete pRingBuffer; |
54 |
|
|
UnusedStreams--; |
55 |
|
|
TotalStreams--; |
56 |
|
|
} |
57 |
|
|
|
58 |
|
|
/// Returns number of refilled sample points or a value < 0 on error. |
59 |
|
|
virtual int ReadAhead(unsigned long SampleCount) { |
60 |
|
|
if (this->State == state_unused) return -1; |
61 |
|
|
if (this->State == state_end) return 0; |
62 |
|
|
if (!SampleCount) return 0; |
63 |
|
|
if (!pRingBuffer->write_space()) return 0; |
64 |
|
|
|
65 |
|
|
long samplestoread = SampleCount / SampleInfo.ChannelsPerFrame; |
66 |
|
|
uint8_t* pBuf = pRingBuffer->get_write_ptr(); |
67 |
|
|
long total_readsamples = Read(pBuf, samplestoread); |
68 |
|
|
|
69 |
|
|
// we must delay the increment_write_ptr_with_wrap() after the while() loop because we need to |
70 |
|
|
// ensure that we read exactly SampleCount sample, otherwise the buffer wrapping code will fail |
71 |
schoenebeck |
3054 |
pRingBuffer->increment_write_ptr_with_wrap(int(total_readsamples * SampleInfo.FrameSize)); |
72 |
iliev |
2012 |
|
73 |
schoenebeck |
3054 |
return (int)total_readsamples; |
74 |
iliev |
2012 |
} |
75 |
|
|
|
76 |
|
|
virtual void WriteSilence(unsigned long SilenceSampleWords) { |
77 |
|
|
memset(pRingBuffer->get_write_ptr(), 0, SilenceSampleWords * SampleInfo.BytesPerSample); |
78 |
schoenebeck |
3054 |
pRingBuffer->increment_write_ptr_with_wrap(int(SilenceSampleWords * SampleInfo.BytesPerSample)); |
79 |
iliev |
2012 |
} |
80 |
|
|
|
81 |
|
|
/// Called by disk thread to activate the disk stream. |
82 |
|
|
void Launch ( |
83 |
|
|
Stream::Handle hStream, |
84 |
|
|
reference_t* pExportReference, |
85 |
|
|
R* pRgn, |
86 |
|
|
SampleDescription SampleInfo, |
87 |
|
|
Sample::PlaybackState PlaybackState, |
88 |
|
|
unsigned long SampleOffset, |
89 |
|
|
bool DoLoop |
90 |
|
|
) { |
91 |
|
|
UnusedStreams--; |
92 |
|
|
this->pExportReference = pExportReference; |
93 |
|
|
this->hThis = hStream; |
94 |
|
|
this->pRegion = pRgn; |
95 |
|
|
this->SampleInfo = SampleInfo; |
96 |
|
|
this->PlaybackState = PlaybackState; |
97 |
|
|
this->SampleOffset = SampleOffset; |
98 |
|
|
this->DoLoop = DoLoop; |
99 |
|
|
SetState(state_active); |
100 |
|
|
} |
101 |
|
|
|
102 |
|
|
protected: |
103 |
|
|
// Attributes |
104 |
|
|
unsigned long SampleOffset; |
105 |
|
|
R* pRegion; |
106 |
|
|
bool DoLoop; |
107 |
|
|
|
108 |
|
|
virtual void Reset() { |
109 |
|
|
SampleOffset = 0; |
110 |
|
|
pRegion = NULL; |
111 |
|
|
PlaybackState.position = 0; |
112 |
|
|
PlaybackState.reverse = false; |
113 |
|
|
hThis = 0; |
114 |
|
|
pRingBuffer->init(); // reset ringbuffer |
115 |
|
|
if (State != state_unused) { |
116 |
|
|
// we can't do 'SetPos(state_unused)' here, due to possible race conditions) |
117 |
|
|
if (pExportReference) { |
118 |
|
|
pExportReference->State = state_unused; |
119 |
|
|
pExportReference = NULL; |
120 |
|
|
} |
121 |
|
|
State = state_unused; |
122 |
|
|
UnusedStreams++; |
123 |
|
|
} |
124 |
|
|
} |
125 |
|
|
|
126 |
|
|
private: |
127 |
|
|
|
128 |
|
|
// Methods |
129 |
|
|
|
130 |
|
|
}; |
131 |
|
|
} // namespace LinuxSampler |
132 |
|
|
|
133 |
|
|
#endif // __LS_STREAMBASE_H__ |