summaryrefslogtreecommitdiff
path: root/muse_qt4_evolution/muse/wave.h
blob: 42054f87a46ba4ec087236423b9b93ef3f9327d5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
//=============================================================================
//  MusE
//  Linux Music Editor
//  $Id:$
//
//  Copyright (C) 2002-2006 by Werner Schweer and others
//
//  This program is free software; you can redistribute it and/or modify
//  it under the terms of the GNU General Public License version 2.
//
//  This program is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//  GNU General Public License for more details.
//
//  You should have received a copy of the GNU General Public License
//  along with this program; if not, write to the Free Software
//  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
//=============================================================================

#ifndef __WAVE_H__
#define __WAVE_H__

#include <sndfile.h>

//---------------------------------------------------------
//   SampleV
//    peak file value
//---------------------------------------------------------

struct SampleV {
      unsigned char peak;
      unsigned char rms;
      };

class SndFile;

class SndFileList : public QHash<QString, SndFile*> {
   public:
      SndFile* search(const QString& name) { return (*this)[name]; }
      };

//---------------------------------------------------------
//   SndFile
//---------------------------------------------------------

class SndFile {
      static QHash<QString, SndFile*> sndFiles;
      static QList<SndFile*> createdFiles;
      static int recFileNumber;

      QFileInfo _finfo;
      SNDFILE* sfRT;          // used by rt process (prefetch)
      SNDFILE* sfUI;          // used by ui process
      SF_INFO sfinfo;
      SampleV** cache;
      int csize;                    // frames in cache

      void readCache(const QString& path, bool progress);
      void writeCache(const QString& path);

      bool openFlag;
      bool writeFlag;

   protected:
      int refCount;

   public:
      SndFile(const QString& name);
      ~SndFile();

      static void applyUndoFile(const QString& original, const QString& tmpfile, unsigned sx, unsigned ex);

      bool openRead();        // return true on error
      bool openWrite();       // return true on error
      void close();
      void remove();

      bool isOpen() const     { return openFlag; }
      bool isWritable() const { return writeFlag; }
      void update();

      QFileInfo* finfo() 	{ return &_finfo; }

      unsigned samples() const;
      unsigned channels() const;
      unsigned samplerate() const;
      unsigned format() const;
      int sampleBits() const;
      void setFormat(int fmt, int ch, int rate);

      size_t read(int channel, float**, size_t);
      size_t write(int channel, float**, size_t);

      off_t seek(off_t frames);
      void read(SampleV* s, int mag, unsigned pos);
      QString strerror() const;

	static SndFile* createRecFile(int);
      static void cleanupRecFiles(bool);
      static void updateRecFiles();
      static SndFile* search(const QString& name);
	static SndFile* getWave(const QString& inName, bool writeFlag);

      friend class SndFileR;
      };

//---------------------------------------------------------
//   SndFileR
//    SndFile with reference count
//---------------------------------------------------------

class SndFileR {
      SndFile* sf;

   public:
      SndFileR() { sf = 0; }
      SndFileR(SndFile* _sf);
      SndFileR(const SndFileR& ed);
      SndFileR& operator=(const SndFileR& ed);
      bool operator==(const SndFileR& c) const { return sf == c.sf; }
      bool operator==(SndFile* c) const { return sf == c; }
      ~SndFileR();
      int getRefCount() const { return sf->refCount; }
      bool isNull() const     { return sf == 0; }

      bool openRead()         { return sf->openRead();  }
      bool openWrite()        { return sf->openWrite(); }
      void close()            { sf->close();     }
      void remove()           { sf->remove();    }

      bool isOpen() const     { return sf->isOpen(); }
      bool isWritable() const { return sf->isWritable(); }
      void update()           { sf->update(); }
      QFileInfo* finfo() 	{ return sf->finfo(); }
      const QFileInfo* finfo() const { return sf->finfo(); }

      unsigned samples() const    { return sf->samples(); }
      unsigned channels() const   { return sf->channels(); }
      unsigned samplerate() const { return sf->samplerate(); }
      unsigned format() const     { return sf->format(); }
      int sampleBits() const      { return sf->sampleBits(); }
      void setFormat(int fmt, int ch, int rate) {
            sf->setFormat(fmt, ch, rate);
            }
      size_t read(int channel, float** f, size_t n) {
            return sf->read(channel, f, n);
            }
      size_t write(int channel, float** f, size_t n) {
            return sf->write(channel, f, n);
            }
      off_t seek(off_t frames) {
            return sf->seek(frames);
            }
      void read(SampleV* s, int mag, unsigned pos) {
            sf->read(s, mag, pos);
            }
      QString strerror() const { return sf->strerror(); }
      };

#endif