File: BlockFile.cpp

package info (click to toggle)
audacity 0.98-3
  • links: PTS
  • area: main
  • in suites: woody
  • size: 2,896 kB
  • ctags: 4,089
  • sloc: cpp: 26,099; ansic: 4,961; sh: 2,465; makefile: 156; perl: 23
file content (279 lines) | stat: -rw-r--r-- 5,652 bytes parent folder | download
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
/**********************************************************************

  Audacity: A Digital Audio Editor

  BlockFile.cpp

  Dominic Mazzoni

**********************************************************************/

#include "wx/file.h"
#include "wx/filefn.h"

#include "BlockFile.h"
#include "WaveTrack.h"

#include "snd/snd.h"

BlockFile::BlockFile(wxString name, wxString fullPath)
{
   mName = name;
   mFullPath = fullPath;
   mAlias = false;

   mFile = NULL;
   mSndNode = NULL;

   mPos = 0;

   mRefCount = 1;

   mLocked = false;
}

BlockFile::BlockFile(wxString name, wxString fullPath,
                     int localLen,
                     wxString aliasFullPath,
                     sampleCount aliasStart,
                     sampleCount aliasLen, int aliasChannel)
{
   mName = name;
   mFullPath = fullPath;

   mAlias = true;

   mFile = NULL;

   mAliasFullPath = aliasFullPath;
   mLocalLen = localLen;
   mStart = aliasStart;
   mLen = aliasLen;
   mChannel = aliasChannel;
   mPos = 0;

   mSndNode = NULL;

   mRefCount = 1;

   mLocked = false;
}

BlockFile::~BlockFile()
{
   Close();
}

wxString BlockFile::GetName()
{
   return mName;
}

void BlockFile::Lock()
{
   mLocked = true;
}

void BlockFile::Unlock()
{
   mLocked = false;
}

bool BlockFile::IsLocked()
{
   return mLocked;
}

bool BlockFile::IsAlias()
{
   return mAlias;
}

wxString BlockFile::GetAliasedFile()
{
   wxASSERT(mAlias);

   return mAliasFullPath;
}

void BlockFile::ChangeAliasedFile(wxString newFile)
{
   // This method is only called with the DirManager is moving
   // a file we depend on out of the way, so that a new file
   // with the same name can be exported.

   wxASSERT(mAlias);

   mAliasFullPath = newFile;
}

void BlockFile::Ref()
{
   mRefCount++;
}

bool BlockFile::Deref()
{
   mRefCount--;
   if (mRefCount <= 0) {
      Close();
      wxRemoveFile(mFullPath);
      delete this;
      return true;
   } else
      return false;
}

bool BlockFile::OpenReadHeader()
{
   mPos = 0;

   mFile = new wxFFile();
   bool success = mFile->Open((const wxChar *) mFullPath, "r+b");

   return success;
}

bool BlockFile::OpenReadData()
{
   if (mAlias) {
      mSndNode = (void *) new snd_node();
      ((snd_node *) mSndNode)->device = SND_DEVICE_FILE;
      ((snd_node *) mSndNode)->write_flag = SND_READ;
      strcpy(((snd_node *) mSndNode)->u.file.filename,
             (const char *) mAliasFullPath);
      ((snd_node *) mSndNode)->u.file.file = 0;

      int err;
      long flags = 0;

      mPos = mLocalLen;

      err = snd_open(((snd_node *) mSndNode), &flags);

      if (err == 0) {
         double secs = mStart / ((snd_node *) mSndNode)->format.srate;
         snd_seek(((snd_node *) mSndNode), secs);
      }

      return (err == 0);
   } else {
      mPos = mLocalLen;

      mFile = new wxFFile();
      bool success = mFile->Open((const wxChar *) mFullPath, "r+b");

      return success;
   }
}

bool BlockFile::OpenWriting()
{
   mPos = 0;

   mFile = new wxFFile();
   bool success = mFile->Open((const wxChar *) mFullPath, "w+b");

   return success;
}

void BlockFile::Close()
{
   if (mFile) {
      mFile->Close();
      delete mFile;
      mFile = 0;
   }

   if (mAlias && ((snd_node *) mSndNode)) {
      snd_close(((snd_node *) mSndNode));
      delete((snd_node *) mSndNode);
      mSndNode = NULL;
   }

}

int BlockFile::Read(void *data, int len)
{
   wxASSERT(!(mAlias && mPos < mLocalLen && mPos + len > mLocalLen));

   if (!mAlias || (mAlias && mPos < mLocalLen)) {
      wxASSERT(mFile);

      int rval = (int) mFile->Read(data, (size_t) len);

      if (rval != len) {
         printf("Expected %d bytes, got %d\n", len, rval);
         wxASSERT(0);
      }

      mPos += rval;

      return rval;
   } else {
      snd_node sndbuffer;

      int channels = ((snd_node *) mSndNode)->format.channels;

      sndbuffer.device = SND_DEVICE_MEM;
      sndbuffer.write_flag = SND_WRITE;
      sndbuffer.u.mem.buffer_max = 0;
      sndbuffer.u.mem.buffer = 0;
      sndbuffer.u.mem.buffer_len = 0;
      sndbuffer.u.mem.buffer_pos = 0;
      sndbuffer.format.channels = channels;
      sndbuffer.format.mode = SND_MODE_PCM;
      sndbuffer.format.bits = 16;
      sndbuffer.format.srate = ((snd_node *) mSndNode)->format.srate;

      int frames = len / 2;
      int fromBufferSize =
          frames * snd_bytes_per_frame(((snd_node *) mSndNode));
      char *fromBuffer = new char[fromBufferSize];
      int toBufferSize = frames * channels;
      sampleType *toBuffer = new short[toBufferSize];

      int framesRead =
          snd_read(((snd_node *) mSndNode), fromBuffer, frames);

      mPos += framesRead;

      snd_convert(&sndbuffer, (void *) toBuffer,        // to
                  ((snd_node *) mSndNode), (void *) fromBuffer, framesRead);    // from

      for (int i = 0; i < framesRead; i++)
         ((sampleType *) data)[i] = toBuffer[(channels * i) + mChannel];

      delete[]fromBuffer;
      delete[]toBuffer;

      return (framesRead * sizeof(sampleType));
   }
}

int BlockFile::Write(void *data, int len)
{
   wxASSERT(mFile);
   wxASSERT(!mAlias || mPos < mLocalLen);

   int rval = (int) mFile->Write((const void *) data, (size_t) len);
   mPos += rval;

   return rval;
}

bool BlockFile::SeekTo(int where)
{
   if (mAlias && where >= mLocalLen) {
      int skipSamples = (where - mPos) / 2;
      mPos = where;
      double secs = skipSamples / ((snd_node *) mSndNode)->format.srate;

      snd_seek(((snd_node *) mSndNode), secs);
      return true;
   } else {
      wxASSERT(mFile);
      mPos = where;
      return mFile->Seek((long) mPos, wxFromStart);
   }
}