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

Annotation of /linuxsampler/trunk/src/engines/common/SampleFile.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2119 - (hide annotations) (download)
Fri Aug 27 16:27:16 2010 UTC (13 years, 8 months ago) by persson
File size: 7821 byte(s)
* sfz engine: fixed playback of 16 bit wav files on big endian CPUs
* sfz engine: added support for Ogg Vorbis sample files

1 iliev 2012 /***************************************************************************
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     #include "SampleFile.h"
25 persson 2119 #include "../../common/global_private.h"
26 iliev 2012 #include "../../common/Exception.h"
27    
28     #include <cstring>
29    
30     namespace LinuxSampler {
31     #if CONFIG_DEVMODE
32     int SampleFile_OpenFilesCount = 0;
33     #endif
34    
35     SampleFile::SampleFile(String File, bool DontClose) {
36     this->File = File;
37     this->pSndFile = NULL;
38    
39     SF_INFO sfInfo;
40     sfInfo.format = 0;
41     pSndFile = sf_open(File.c_str(), SFM_READ, &sfInfo);
42     if(pSndFile == NULL) throw Exception(File + ": Can't get sample info: " + String(sf_strerror (NULL)));
43     #if CONFIG_DEVMODE
44     std::cout << "Number of opened sample files: " << ++SampleFile_OpenFilesCount << std::endl;
45     #endif
46     SampleRate = sfInfo.samplerate;
47     ChannelCount = sfInfo.channels;
48     Format = sfInfo.format;
49    
50 persson 2119 switch(Format & SF_FORMAT_SUBMASK) {
51 iliev 2012 case SF_FORMAT_PCM_S8:
52     case SF_FORMAT_PCM_U8:
53 iliev 2019 case SF_FORMAT_DPCM_8:
54     FrameSize = ChannelCount;
55     break;
56 iliev 2012 case SF_FORMAT_PCM_16:
57     case SF_FORMAT_DPCM_16:
58 iliev 2019 FrameSize = 2 * ChannelCount;
59 iliev 2012 break;
60 iliev 2019 case SF_FORMAT_PCM_24:
61     case SF_FORMAT_DWVW_24:
62     FrameSize = 3 * ChannelCount;
63     break;
64 iliev 2012 default:
65 iliev 2019 FrameSize = 2 * ChannelCount;
66 iliev 2012 }
67     TotalFrameCount = sfInfo.frames;
68    
69     if(!DontClose) Close();
70     }
71    
72     SampleFile::~SampleFile() {
73     Close();
74 persson 2058 ReleaseSampleData();
75 iliev 2012 }
76    
77     void SampleFile::Open() {
78     if(pSndFile) return; // Already opened
79     SF_INFO sfInfo;
80     sfInfo.format = 0;
81     pSndFile = sf_open(File.c_str(), SFM_READ, &sfInfo);
82     if(pSndFile == NULL) throw Exception(File + ": Can't load sample");
83     #if CONFIG_DEVMODE
84     std::cout << "Number of opened sample files: " << ++SampleFile_OpenFilesCount << std::endl;
85     #endif
86     }
87    
88     void SampleFile::Close() {
89     if(pSndFile == NULL) return;
90     if(sf_close(pSndFile)) std::cerr << "Sample::Close() " << "Failed to close " << File << std::endl;
91     pSndFile = NULL;
92     #if CONFIG_DEVMODE
93     std::cout << "Number of opened sample files: " << --SampleFile_OpenFilesCount << std::endl;
94     #endif
95     }
96    
97     long SampleFile::SetPos(unsigned long FrameOffset) {
98     return SetPos(FrameOffset, SEEK_SET);
99     }
100    
101     long SampleFile::SetPos(unsigned long FrameCount, int Whence) {
102     if(pSndFile == NULL) {
103     std::cerr << "Sample::SetPos() " << File << " not opened" << std::endl;
104     return -1;
105     }
106    
107     return sf_seek(pSndFile, FrameCount, Whence);
108     }
109    
110     long SampleFile::GetPos() {
111     if(pSndFile == NULL) {
112     std::cerr << "Sample::GetPos() " << File << " not opened" << std::endl;
113     return -1;
114     }
115    
116     return sf_seek(pSndFile, 0, SEEK_CUR);
117     }
118    
119     Sample::buffer_t SampleFile::LoadSampleData() {
120     return LoadSampleDataWithNullSamplesExtension(this->TotalFrameCount, 0); // 0 amount of NullSamples
121     }
122    
123     Sample::buffer_t SampleFile::LoadSampleData(unsigned long FrameCount) {
124     return LoadSampleDataWithNullSamplesExtension(FrameCount, 0); // 0 amount of NullSamples
125     }
126    
127     Sample::buffer_t SampleFile::LoadSampleDataWithNullSamplesExtension(uint NullFrameCount) {
128     return LoadSampleDataWithNullSamplesExtension(this->TotalFrameCount, NullFrameCount);
129     }
130    
131     Sample::buffer_t SampleFile::LoadSampleDataWithNullSamplesExtension(unsigned long FrameCount, uint NullFramesCount) {
132     Open();
133     if (FrameCount > this->TotalFrameCount) FrameCount = this->TotalFrameCount;
134     if (RAMCache.pStart) delete[] (int8_t*) RAMCache.pStart;
135     unsigned long allocationsize = (FrameCount + NullFramesCount) * this->FrameSize;
136     SetPos(0, SEEK_SET); // reset read position to begin of sample
137     RAMCache.pStart = new int8_t[allocationsize];
138    
139     RAMCache.Size = Read(RAMCache.pStart, FrameCount) * this->FrameSize;
140     RAMCache.NullExtensionSize = allocationsize - RAMCache.Size;
141     // fill the remaining buffer space with silence samples
142     memset((int8_t*)RAMCache.pStart + RAMCache.Size, 0, RAMCache.NullExtensionSize);
143     Close();
144     return GetCache();
145     }
146    
147     long SampleFile::Read(void* pBuffer, unsigned long FrameCount) {
148     Open();
149 persson 2119
150     // ogg files must be read with sf_readf, not sf_read_raw. On
151     // big endian machines, sf_readf_short is also used for 16 bit
152     // wav files, to get automatic endian conversion (for 24 bit
153     // samples this is handled in Synthesize::GetSample instead).
154    
155     #if WORDS_BIGENDIAN || HAVE_DECL_SF_FORMAT_VORBIS
156     if (
157     #if WORDS_BIGENDIAN
158     FrameSize == 2 * ChannelCount
159     #else
160     (Format & SF_FORMAT_SUBMASK) == SF_FORMAT_VORBIS
161     #endif
162     ) {
163     return sf_readf_short(pSndFile, static_cast<short*>(pBuffer), FrameCount);
164     } else
165     #endif
166     {
167     int bytes = sf_read_raw(pSndFile, pBuffer, FrameCount * GetFrameSize());
168     return bytes / GetFrameSize();
169     }
170 iliev 2012 }
171    
172     unsigned long SampleFile::ReadAndLoop (
173     void* pBuffer,
174     unsigned long FrameCount,
175     PlaybackState* pPlaybackState
176     ) {
177     // TODO:
178     SetPos(pPlaybackState->position);
179 iliev 2018 unsigned long count = Read(pBuffer, FrameCount);
180 iliev 2012 pPlaybackState->position = GetPos();
181 iliev 2018 return count;
182 iliev 2012 }
183    
184     void SampleFile::ReleaseSampleData() {
185     if (RAMCache.pStart) delete[] (int8_t*) RAMCache.pStart;
186     RAMCache.pStart = NULL;
187     RAMCache.Size = 0;
188     RAMCache.NullExtensionSize = 0;
189     }
190    
191     Sample::buffer_t SampleFile::GetCache() {
192     // return a copy of the buffer_t structure
193     buffer_t result;
194     result.Size = this->RAMCache.Size;
195     result.pStart = this->RAMCache.pStart;
196     result.NullExtensionSize = this->RAMCache.NullExtensionSize;
197     return result;
198     }
199     } // namespace LinuxSampler

  ViewVC Help
Powered by ViewVC