~ubuntu-branches/ubuntu/karmic/muse/karmic-proposed

« back to all changes in this revision

Viewing changes to sf/sndfile.h

  • Committer: Bazaar Package Importer
  • Author(s): Daniel Kobras
  • Date: 2002-04-23 17:28:23 UTC
  • Revision ID: james.westby@ubuntu.com-20020423172823-w8yplzr81a759xa3
Tags: upstream-0.5.2
ImportĀ upstreamĀ versionĀ 0.5.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
//=========================================================
 
2
//  MusE
 
3
//  Linux Music Editor
 
4
//  $Id: sndfile.h,v 1.3 2002/02/13 11:42:56 muse Exp $
 
5
//
 
6
//  (C) Copyright 2001 Werner Schweer (ws@seh.de)
 
7
//  parts based on libsndfile:
 
8
//  Copyright (C) 1999-2000 Erik de Castro Lopo <erikd@zip.com.au>
 
9
//=========================================================
 
10
 
 
11
#ifndef SNDFILE_H
 
12
#define SNDFILE_H
 
13
 
 
14
#include <list>
 
15
#include <stdio.h>
 
16
#include <stdlib.h>
 
17
 
 
18
#include <sys/types.h>
 
19
#include <qstring.h>
 
20
#include <qfileinfo.h>
 
21
 
 
22
const int cacheMag = 128;
 
23
 
 
24
#define SF_BUFFER_LEN    4096
 
25
#define SF_FILENAME_LEN  256
 
26
#define SF_HEADER_LEN    2048
 
27
 
 
28
#define BITWIDTH2BYTES(x) (((x) + 7) / 8)
 
29
 
 
30
enum {  
 
31
      SF_MODE_READ = 1,
 
32
        SF_MODE_WRITE = 2,
 
33
        SF_MODE_RW = 3
 
34
      };
 
35
 
 
36
enum {  
 
37
      SF_ENDIAN_LITTLE = 100,
 
38
        SF_ENDIAN_BIG = 200
 
39
      };
 
40
 
 
41
enum {
 
42
      SFE_NO_ERROR = 0,
 
43
        SFE_BAD_FILE,
 
44
        SFE_OPEN_FAILED,
 
45
        SFE_BAD_OPEN_FORMAT,
 
46
        SFE_BAD_SNDFILE_PTR,
 
47
        SFE_BAD_SF_INFO_PTR,
 
48
        SFE_BAD_INT_FD,
 
49
        SFE_BAD_INT_PTR,
 
50
        SFE_MALLOC_FAILED,
 
51
        SFE_BAD_SEEK,
 
52
        SFE_NOT_SEEKABLE,
 
53
        SFE_UNIMPLEMENTED,
 
54
        SFE_BAD_READ_ALIGN,
 
55
        SFE_BAD_WRITE_ALIGN,
 
56
        SFE_UNKNOWN_FORMAT,
 
57
        SFE_NOT_READMODE,
 
58
        SFE_NOT_WRITEMODE,
 
59
        SFE_BAD_MODE_RW,
 
60
        SFE_BAD_SF_INFO,
 
61
        SFE_SHORT_READ,
 
62
        SFE_SHORT_WRITE,
 
63
        SFE_INTERNAL,
 
64
        
 
65
        SFE_WAV_NO_RIFF,
 
66
        SFE_WAV_NO_WAVE,
 
67
        SFE_WAV_NO_FMT,
 
68
        SFE_WAV_FMT_SHORT,
 
69
        SFE_WAV_FMT_TOO_BIG,
 
70
        SFE_WAV_BAD_FORMAT,
 
71
        SFE_WAV_BAD_BLOCKALIGN,
 
72
        SFE_WAV_NO_DATA,
 
73
        SFE_WAV_ADPCM_NOT4BIT,
 
74
        SFE_WAV_ADPCM_CHANNELS,
 
75
        SFE_WAV_GSM610_FORMAT,
 
76
        SFE_WAV_UNKNOWN_CHUNK,
 
77
 
 
78
        SFE_AIFF_NO_FORM,
 
79
        SFE_AIFF_UNKNOWN_CHUNK,
 
80
        SFE_COMM_CHUNK_SIZE,
 
81
        SFE_AIFF_NO_SSND,
 
82
        SFE_AIFF_NO_DATA,
 
83
 
 
84
        SFE_AU_UNKNOWN_FORMAT,
 
85
        SFE_AU_NO_DOTSND,
 
86
        
 
87
        SFE_RAW_READ_BAD_SPEC,
 
88
        SFE_RAW_BAD_BITWIDTH,
 
89
        
 
90
        SFE_PAF_NO_MARKER,
 
91
        SFE_PAF_VERSION,
 
92
        SFE_PAF_UNKNOWN_FORMAT,
 
93
        SFE_PAF_SHORT_HEADER,
 
94
        
 
95
        SFE_SVX_NO_FORM,
 
96
        SFE_SVX_NO_BODY,
 
97
        SFE_SVX_NO_DATA,
 
98
        SFE_SVX_BAD_COMP,       
 
99
 
 
100
        SFE_NIST_BAD_HEADER,
 
101
        SFE_NIST_BAD_ENCODING,
 
102
 
 
103
        SFE_MAX_ERROR   // This must be last in list.
 
104
      };
 
105
 
 
106
void    endswap_short_array(short *ptr, int len);
 
107
void    endswap_int_array(int *ptr, int len);
 
108
 
 
109
/* The following file types can be read and written.
 
110
** A file type would consist of a major type (ie SF_FORMAT_WAV) bitwise
 
111
** ORed with a minor type (ie SF_FORMAT_PCM). SF_FORMAT_TYPEMASK and
 
112
** SF_FORMAT_SUBMASK can be used to separate the major and minor file
 
113
** types.
 
114
*/
 
115
 
 
116
enum {  
 
117
      SF_FORMAT_WAV       = 0x10000,  // Microsoft WAV format (big endian).
 
118
        SF_FORMAT_AIFF    = 0x20000,  // Apple/SGI AIFF format (little endian).
 
119
        SF_FORMAT_AU      = 0x30000,  // Sun/NeXT AU format (big endian).
 
120
        SF_FORMAT_AULE    = 0x40000,  // DEC AU format (little endian).
 
121
        SF_FORMAT_RAW     = 0x50000,  // RAW PCM data.
 
122
        SF_FORMAT_PAF     = 0x60000,  // Ensoniq PARIS file format.
 
123
        SF_FORMAT_SVX     = 0x70000,  // Amiga IFF / SVX8 / SV16 format.
 
124
        SF_FORMAT_NIST    = 0x80000,  // Sphere NIST format.
 
125
        
 
126
        SF_FORMAT_PCM     = 0x0001,       // PCM data in 8, 16, 24 or 32 bits.
 
127
        SF_FORMAT_FLOAT   = 0x0002,       // 32 bit floats.
 
128
        SF_FORMAT_ULAW    = 0x0003,       // U-Law encoded.
 
129
        SF_FORMAT_ALAW    = 0x0004,       // A-Law encoded.
 
130
        SF_FORMAT_IMA_ADPCM = 0x0005,     // IMA ADPCM.
 
131
        SF_FORMAT_MS_ADPCM  = 0x0006,     // Microsoft ADPCM.
 
132
 
 
133
        SF_FORMAT_PCM_BE          = 0x0007,       // Big endian PCM data.
 
134
        SF_FORMAT_PCM_LE    = 0x0008,     // Little endian PCM data.
 
135
        SF_FORMAT_PCM_S8          = 0x0009,       // Signed 8 bit PCM.
 
136
        SF_FORMAT_PCM_U8    = 0x000A,     // Unsigned 8 bit PCM.
 
137
        
 
138
        SF_FORMAT_SVX_FIB         = 0x000B,   // SVX Fibonacci Delta encoding.
 
139
        SF_FORMAT_SVX_EXP         = 0x000C,   // SVX Exponential Delta encoding.
 
140
 
 
141
        SF_FORMAT_GSM610          = 0x000D,   // GSM 6.10 encoding.
 
142
 
 
143
        SF_FORMAT_G721_32         = 0x000E,   // 32kbs G721 ADPCM encoding.
 
144
        SF_FORMAT_G723_24         = 0x000F,   // 24kbs G723 ADPCM encoding.
 
145
 
 
146
        SF_FORMAT_SUBMASK         = 0xFFFF,             
 
147
        SF_FORMAT_TYPEMASK  = 0x7FFF0000
 
148
      };
 
149
 
 
150
//---------------------------------------------------------
 
151
//   SndFileFormat
 
152
//---------------------------------------------------------
 
153
 
 
154
class SndFile;
 
155
 
 
156
class SndFileFormat {
 
157
      size_t unimpl() const;
 
158
 
 
159
   protected:
 
160
      SndFile* sfile;
 
161
      float buffer[SF_BUFFER_LEN];
 
162
 
 
163
   public:
 
164
      SndFileFormat(SndFile* s)             { sfile = s; }
 
165
      virtual off_t seek(off_t, int)        { return unimpl(); }
 
166
      virtual size_t read(float**, size_t)  { return unimpl(); }
 
167
      virtual size_t write(float**, size_t) { return unimpl(); }
 
168
      virtual void close()                  {}
 
169
      };
 
170
 
 
171
//---------------------------------------------------------
 
172
//   SampleV
 
173
//    peak file value
 
174
//---------------------------------------------------------
 
175
 
 
176
struct SampleV {
 
177
      unsigned char peak;
 
178
      unsigned char rms;
 
179
      };
 
180
 
 
181
union WAV_FMT;
 
182
 
 
183
//---------------------------------------------------------
 
184
//   SndFile
 
185
//---------------------------------------------------------
 
186
 
 
187
class SndFile {
 
188
      QFileInfo* finfo;
 
189
      int refs;         // reference count to file
 
190
 
 
191
        int endian;
 
192
        
 
193
      SndFileFormat* fmt;
 
194
        unsigned long current;
 
195
 
 
196
        unsigned int _blockwidth;     // Size in bytes of one set of interleaved samples.
 
197
        unsigned long _dataoffset;    // Offset in number of bytes from beginning of file.
 
198
 
 
199
      SampleV** cache;
 
200
      int csize;                    // frames in cache
 
201
 
 
202
        unsigned _samples;
 
203
      unsigned _samplerate;
 
204
        unsigned _channels;
 
205
        unsigned _format;
 
206
        unsigned _sections;
 
207
        unsigned _pcmbitwidth;
 
208
        bool _seekable;
 
209
 
 
210
        FILE* _file;
 
211
        unsigned int _bytewidth;         // Size in bytes of one sample (one channel).
 
212
 
 
213
        unsigned long _filelength;
 
214
        unsigned long _datalength;     // Length in bytes of the audio data.
 
215
        int _mode;
 
216
 
 
217
      int wav_open_read();
 
218
      int wav_open_write();
 
219
      bool validate();
 
220
      bool validateSfinfo();
 
221
      void readCache(const QString& path);
 
222
      void writeCache(const QString& path);
 
223
      int read_fmt_chunk(WAV_FMT* wav_fmt);
 
224
      int write_header(WAV_FMT *wav_fmt, unsigned int size, int do_fact);
 
225
 
 
226
   public:
 
227
      SndFile(const QString path);
 
228
      ~SndFile();
 
229
 
 
230
      int _errno;
 
231
 
 
232
      FILE* file() const { return _file; }
 
233
      bool openRead();
 
234
      bool openWrite();
 
235
      void close();
 
236
 
 
237
      unsigned samples() const      { return _samples; }
 
238
      unsigned channels() const     { return _channels; }
 
239
      unsigned samplerate() const   { return _samplerate; }
 
240
        unsigned format() const       { return _format; }
 
241
      int sampleBits() const        { return _pcmbitwidth; }
 
242
      void setFormat(int fmt, int ch, int rate, int bits);
 
243
 
 
244
      size_t read(float**, size_t);
 
245
      size_t write(float**, size_t);
 
246
      off_t     seek(off_t frames, int whence);
 
247
      void read(SampleV* s, int mag, unsigned pos);
 
248
 
 
249
      void setErrno(int n) { _errno = n; }
 
250
 
 
251
      QString path() const                { return finfo->filePath(); }
 
252
      QString basename() const            { return finfo->baseName(); }
 
253
      QString name() const                { return finfo->fileName(); }
 
254
 
 
255
      int references()                    { return refs; }
 
256
      int incRef();
 
257
      int decRef();
 
258
 
 
259
      unsigned dataoffset() const         { return _dataoffset; }
 
260
        unsigned long filelength() const    { return _filelength; }
 
261
        void setFilelength(unsigned long v) { _filelength = v; }
 
262
        unsigned long datalength()          { return _datalength; }
 
263
        void setDatalength(unsigned long v) { _datalength = v; }
 
264
        int mode() const                    { return _mode; }
 
265
        unsigned int bytewidth() const      { return _bytewidth; }
 
266
      void setSamples(unsigned v)         { _samples = v; }
 
267
        unsigned blockwidth() const         { return _blockwidth; }
 
268
        void setBlockwidth(unsigned v)      { _blockwidth = v;    }
 
269
      };
 
270
 
 
271
//---------------------------------------------------------
 
272
//   SndFileList
 
273
//---------------------------------------------------------
 
274
 
 
275
class SndFileList : public std::list<SndFile*> {
 
276
   public:
 
277
      SndFile* search(const QString& name);
 
278
      };
 
279
 
 
280
typedef SndFileList::iterator iSndFile;
 
281
 
 
282
#endif