/[svn]/linuxsampler/trunk/src/engines/common/SampleFile.h
ViewVC logotype

Contents of /linuxsampler/trunk/src/engines/common/SampleFile.h

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2021 - (show annotations) (download) (as text)
Fri Oct 30 16:36:20 2009 UTC (14 years, 5 months ago) by iliev
File MIME type: text/x-c++hdr
File size: 7008 byte(s)
* sfz engine: loop support
* sf2 engine: 24bit support
* sf2 engine: loop support
* sf2 engine: instrument unloading

1 /***************************************************************************
2 * *
3 * LinuxSampler - modular, streaming capable sampler *
4 * *
5 * Copyright (C) 2003 - 2009 Christian Schoenebeck *
6 * Copyright (C) 2009 Grigor Iliev *
7 * *
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
47 virtual buffer_t LoadSampleData();
48 virtual buffer_t LoadSampleData(unsigned long FrameCount);
49 virtual buffer_t LoadSampleDataWithNullSamplesExtension(uint NullFrameCount);
50 virtual buffer_t LoadSampleDataWithNullSamplesExtension(unsigned long FrameCount, uint NullFramesCount);
51 virtual void ReleaseSampleData();
52 virtual buffer_t GetCache();
53 virtual long Read(void* pBuffer, unsigned long FrameCount);
54
55 virtual unsigned long ReadAndLoop (
56 void* pBuffer,
57 unsigned long FrameCount,
58 PlaybackState* pPlaybackState
59 );
60
61 virtual long SetPos(unsigned long FrameOffset);
62 virtual long GetPos();
63
64 void Open();
65 void Close();
66
67 private:
68 String File;
69 int SampleRate;
70 int ChannelCount;
71 int Format;
72 int FrameSize; ///< In bytes
73 long TotalFrameCount;
74
75 SNDFILE* pSndFile;
76
77 buffer_t RAMCache; ///< Buffers samples (already uncompressed) in RAM.
78 long SetPos(unsigned long FrameCount, int Whence);
79 };
80
81 template <class R>
82 class SampleFileBase : public SampleFile {
83 public:
84 SampleFileBase(String File, bool DontClose = false) : SampleFile(File, DontClose) { }
85 virtual ~SampleFileBase() { }
86
87
88
89 /**
90 * Reads \a SampleCount number of sample points from the position stored
91 * in \a pPlaybackState into the buffer pointed by \a pBuffer and moves
92 * the position within the sample respectively, this method honors the
93 * looping informations of the sample (if any). Use this
94 * method if you don't want to load the sample into RAM, thus for disk
95 * streaming. All this methods needs to know to proceed with streaming
96 * for the next time you call this method is stored in \a pPlaybackState.
97 * You have to allocate and initialize the playback_state_t structure by
98 * yourself before you use it to stream a sample:
99 * @code
100 * PlaybackState playbackstate;
101 * playbackstate.position = 0;
102 * playbackstate.reverse = false;
103 * playbackstate.loop_cycles_left = pSample->LoopPlayCount;
104 * @endcode
105 * You don't have to take care of things like if there is actually a loop
106 * defined or if the current read position is located within a loop area.
107 * The method already handles such cases by itself.
108 *
109 * @param pBuffer destination buffer
110 * @param FrameCount number of sample points to read
111 * @param pPlaybackState will be used to store and reload the playback
112 * state for the next ReadAndLoop() call
113 * @returns number of successfully read sample points
114 */
115 unsigned long ReadAndLoop (
116 void* pBuffer,
117 unsigned long FrameCount,
118 PlaybackState* pPlaybackState,
119 R* pRegion
120 ) {
121 // TODO: startAddrsCoarseOffset, endAddrsCoarseOffset
122 unsigned long samplestoread = FrameCount, totalreadsamples = 0, readsamples, samplestoloopend;
123 uint8_t* pDst = (uint8_t*) pBuffer;
124 SetPos(pPlaybackState->position);
125 if (pRegion->HasLoop()) {
126 do {
127 samplestoloopend = pRegion->GetLoopEnd() - GetPos();
128 readsamples = Read(&pDst[totalreadsamples * GetFrameSize()], Min(samplestoread, samplestoloopend));
129 samplestoread -= readsamples;
130 totalreadsamples += readsamples;
131 if (readsamples == samplestoloopend) {
132 SetPos(pRegion->GetLoopStart());
133 }
134 } while (samplestoread && readsamples);
135 } else {
136 totalreadsamples = Read(pBuffer, FrameCount);
137 }
138
139 pPlaybackState->position = GetPos();
140
141 return totalreadsamples;
142 }
143
144 protected:
145 inline long Min(long A, long B) { return (A > B) ? B : A; }
146 };
147 } // namespace LinuxSampler
148
149 #endif // __LS_SAMPLEFILE_H__

  ViewVC Help
Powered by ViewVC