1 |
iliev |
2012 |
/*************************************************************************** |
2 |
|
|
* * |
3 |
|
|
* LinuxSampler - modular, streaming capable sampler * |
4 |
|
|
* * |
5 |
|
|
* Copyright (C) 2003 - 2009 Christian Schoenebeck * |
6 |
persson |
2167 |
* Copyright (C) 2009 - 2011 Grigor Iliev * |
7 |
iliev |
2012 |
* * |
8 |
|
|
* This program is free software; you can redistribute it and/or modify * |
9 |
|
|
* it under the terms of the GNU General Public License as published by * |
10 |
|
|
* the Free Software Foundation; either version 2 of the License, or * |
11 |
|
|
* (at your option) any later version. * |
12 |
|
|
* * |
13 |
|
|
* This program is distributed in the hope that it will be useful, * |
14 |
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of * |
15 |
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
16 |
|
|
* GNU General Public License for more details. * |
17 |
|
|
* * |
18 |
|
|
* You should have received a copy of the GNU General Public License * |
19 |
|
|
* along with this program; if not, write to the Free Software * |
20 |
|
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, * |
21 |
|
|
* MA 02111-1307 USA * |
22 |
|
|
***************************************************************************/ |
23 |
|
|
|
24 |
|
|
#ifndef __LS_SAMPLEFILE_H__ |
25 |
|
|
#define __LS_SAMPLEFILE_H__ |
26 |
|
|
|
27 |
|
|
#include "Sample.h" |
28 |
|
|
|
29 |
|
|
#include <sndfile.h> |
30 |
|
|
#include "../../common/global.h" |
31 |
|
|
|
32 |
|
|
|
33 |
|
|
namespace LinuxSampler { |
34 |
|
|
class SampleFile : public Sample { |
35 |
|
|
public: |
36 |
|
|
SampleFile(String File, bool DontClose = false); |
37 |
|
|
virtual ~SampleFile(); |
38 |
|
|
|
39 |
|
|
String GetFile() { return File; } |
40 |
|
|
|
41 |
|
|
virtual String GetName() { return File; } |
42 |
|
|
virtual int GetSampleRate() { return SampleRate; } |
43 |
|
|
virtual int GetChannelCount() { return ChannelCount; } |
44 |
|
|
virtual long GetTotalFrameCount() { return TotalFrameCount; } |
45 |
|
|
virtual int GetFrameSize() { return FrameSize; } |
46 |
persson |
2167 |
virtual int GetLoops() { return Loops; } |
47 |
|
|
virtual uint GetLoopStart() { return LoopStart; } |
48 |
|
|
virtual uint GetLoopEnd() { return LoopEnd; } |
49 |
iliev |
2012 |
|
50 |
|
|
virtual buffer_t LoadSampleData(); |
51 |
|
|
virtual buffer_t LoadSampleData(unsigned long FrameCount); |
52 |
|
|
virtual buffer_t LoadSampleDataWithNullSamplesExtension(uint NullFrameCount); |
53 |
|
|
virtual buffer_t LoadSampleDataWithNullSamplesExtension(unsigned long FrameCount, uint NullFramesCount); |
54 |
|
|
virtual void ReleaseSampleData(); |
55 |
|
|
virtual buffer_t GetCache(); |
56 |
|
|
virtual long Read(void* pBuffer, unsigned long FrameCount); |
57 |
|
|
|
58 |
|
|
virtual unsigned long ReadAndLoop ( |
59 |
|
|
void* pBuffer, |
60 |
|
|
unsigned long FrameCount, |
61 |
|
|
PlaybackState* pPlaybackState |
62 |
|
|
); |
63 |
|
|
|
64 |
|
|
virtual long SetPos(unsigned long FrameOffset); |
65 |
|
|
virtual long GetPos(); |
66 |
|
|
|
67 |
|
|
void Open(); |
68 |
|
|
void Close(); |
69 |
|
|
|
70 |
|
|
private: |
71 |
|
|
String File; |
72 |
|
|
int SampleRate; |
73 |
|
|
int ChannelCount; |
74 |
|
|
int Format; |
75 |
|
|
int FrameSize; ///< In bytes |
76 |
|
|
long TotalFrameCount; |
77 |
persson |
2167 |
int Loops; |
78 |
|
|
uint LoopStart; |
79 |
|
|
uint LoopEnd; |
80 |
iliev |
2012 |
|
81 |
|
|
SNDFILE* pSndFile; |
82 |
|
|
|
83 |
|
|
buffer_t RAMCache; ///< Buffers samples (already uncompressed) in RAM. |
84 |
|
|
long SetPos(unsigned long FrameCount, int Whence); |
85 |
|
|
}; |
86 |
iliev |
2021 |
|
87 |
|
|
template <class R> |
88 |
|
|
class SampleFileBase : public SampleFile { |
89 |
|
|
public: |
90 |
|
|
SampleFileBase(String File, bool DontClose = false) : SampleFile(File, DontClose) { } |
91 |
|
|
virtual ~SampleFileBase() { } |
92 |
|
|
|
93 |
|
|
|
94 |
|
|
|
95 |
|
|
/** |
96 |
|
|
* Reads \a SampleCount number of sample points from the position stored |
97 |
|
|
* in \a pPlaybackState into the buffer pointed by \a pBuffer and moves |
98 |
|
|
* the position within the sample respectively, this method honors the |
99 |
|
|
* looping informations of the sample (if any). Use this |
100 |
|
|
* method if you don't want to load the sample into RAM, thus for disk |
101 |
|
|
* streaming. All this methods needs to know to proceed with streaming |
102 |
|
|
* for the next time you call this method is stored in \a pPlaybackState. |
103 |
|
|
* You have to allocate and initialize the playback_state_t structure by |
104 |
|
|
* yourself before you use it to stream a sample: |
105 |
|
|
* @code |
106 |
|
|
* PlaybackState playbackstate; |
107 |
|
|
* playbackstate.position = 0; |
108 |
|
|
* playbackstate.reverse = false; |
109 |
|
|
* playbackstate.loop_cycles_left = pSample->LoopPlayCount; |
110 |
|
|
* @endcode |
111 |
|
|
* You don't have to take care of things like if there is actually a loop |
112 |
|
|
* defined or if the current read position is located within a loop area. |
113 |
|
|
* The method already handles such cases by itself. |
114 |
|
|
* |
115 |
|
|
* @param pBuffer destination buffer |
116 |
|
|
* @param FrameCount number of sample points to read |
117 |
|
|
* @param pPlaybackState will be used to store and reload the playback |
118 |
|
|
* state for the next ReadAndLoop() call |
119 |
|
|
* @returns number of successfully read sample points |
120 |
|
|
*/ |
121 |
|
|
unsigned long ReadAndLoop ( |
122 |
|
|
void* pBuffer, |
123 |
|
|
unsigned long FrameCount, |
124 |
|
|
PlaybackState* pPlaybackState, |
125 |
|
|
R* pRegion |
126 |
|
|
) { |
127 |
|
|
// TODO: startAddrsCoarseOffset, endAddrsCoarseOffset |
128 |
|
|
unsigned long samplestoread = FrameCount, totalreadsamples = 0, readsamples, samplestoloopend; |
129 |
|
|
uint8_t* pDst = (uint8_t*) pBuffer; |
130 |
|
|
SetPos(pPlaybackState->position); |
131 |
|
|
if (pRegion->HasLoop()) { |
132 |
|
|
do { |
133 |
|
|
samplestoloopend = pRegion->GetLoopEnd() - GetPos(); |
134 |
|
|
readsamples = Read(&pDst[totalreadsamples * GetFrameSize()], Min(samplestoread, samplestoloopend)); |
135 |
|
|
samplestoread -= readsamples; |
136 |
|
|
totalreadsamples += readsamples; |
137 |
|
|
if (readsamples == samplestoloopend) { |
138 |
|
|
SetPos(pRegion->GetLoopStart()); |
139 |
|
|
} |
140 |
|
|
} while (samplestoread && readsamples); |
141 |
|
|
} else { |
142 |
|
|
totalreadsamples = Read(pBuffer, FrameCount); |
143 |
|
|
} |
144 |
|
|
|
145 |
|
|
pPlaybackState->position = GetPos(); |
146 |
|
|
|
147 |
|
|
return totalreadsamples; |
148 |
|
|
} |
149 |
|
|
|
150 |
|
|
protected: |
151 |
|
|
inline long Min(long A, long B) { return (A > B) ? B : A; } |
152 |
|
|
}; |
153 |
iliev |
2012 |
} // namespace LinuxSampler |
154 |
|
|
|
155 |
|
|
#endif // __LS_SAMPLEFILE_H__ |