File: ExportPCM.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 (277 lines) | stat: -rw-r--r-- 7,649 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
/**********************************************************************

  Audacity: A Digital Audio Editor

  ExportPCM.cpp

  Dominic Mazzoni

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

#ifdef __WXMAC__
#include <Files.h>
#include <Resources.h>
void wxMacFilename2FSSpec( const char *path , FSSpec *spec ) ;
#endif

#include <wx/string.h>
#include <wx/window.h>
#include <wx/msgdlg.h>
#include <wx/progdlg.h>
#include <wx/timer.h>

#include "snd/snd.h"

#include "Audacity.h"
#include "LabelTrack.h"
#include "Mix.h"
#include "Project.h"
#include "Track.h"
#include "WaveTrack.h"

bool ExportPCM(AudacityProject *project,
               wxString format, bool stereo, wxString fName,
               bool selectionOnly, double t0, double t1)
{
   double rate = project->GetRate();
   wxWindow *parent = project;
   TrackList *tracks = project->GetTracks();

   int header = SND_HEAD_NONE;
   bool trackMarkers = false;

   if (format == "WAV")
      header = SND_HEAD_WAVE;
   else if (format == "AIFF")
      header = SND_HEAD_AIFF;
   else if (format == "IRCAM")
      header = SND_HEAD_IRCAM;
   else if (format == "AU")
      header = SND_HEAD_NEXT;
#ifdef __WXMAC__
   else if (format == "AIFF with track markers") {
      header = SND_HEAD_AIFF;
      trackMarkers = true;
   }
#endif


   // Use snd library to export file

   snd_node sndfile;
   snd_node sndbuffer;

   sndfile.device = SND_DEVICE_FILE;
   sndfile.write_flag = SND_WRITE;
   strcpy(sndfile.u.file.filename, (const char *) fName);
   sndfile.u.file.file = 0;
   sndfile.u.file.header = header;
   sndfile.u.file.byte_offset = 0;
   sndfile.u.file.end_offset = 0;
   sndfile.u.file.swap = 0;
   sndfile.format.channels = stereo ? 2 : 1;
   sndfile.format.mode = SND_MODE_PCM;  // SND_MODE_FLOAT
   sndfile.format.bits = 16;
   sndfile.format.srate = int (rate + 0.5);

   int err;
   long flags = 0;

   err = snd_open(&sndfile, &flags);
   if (err) {
      wxMessageBox("Could not write to file.");
      return false;
   }

   sndbuffer.device = SND_DEVICE_MEM;
   sndbuffer.write_flag = SND_READ;
   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 = stereo ? 2 : 1;
   sndbuffer.format.mode = SND_MODE_PCM;        // SND_MODE_FLOAT
   sndbuffer.format.bits = 16;
   sndbuffer.format.srate = int (rate + 0.5);

   double timeStep = 10.0;      // write in blocks of 10 secs

   sampleCount maxSamples = int (timeStep * rate + 0.5);

   wxProgressDialog *progress = NULL;
   wxYield();
   wxStartTimer();
   wxBusyCursor busy;
   bool cancelling = false;

   double t = t0;

   while (t < t1 && !cancelling) {

      double deltat = timeStep;
      if (t + deltat > t1)
         deltat = t1 - t;

      sampleCount numSamples = int (deltat * rate + 0.5);

      Mixer *mixer = new Mixer(stereo ? 2 : 1, numSamples, true);
      wxASSERT(mixer);
      mixer->Clear();

      char *buffer = new char[numSamples * 2 * sndbuffer.format.channels];
      wxASSERT(buffer);

      TrackListIterator iter(tracks);
      VTrack *tr = iter.First();
      while (tr) {
         if (tr->GetKind() == VTrack::Wave) {
            if (tr->selected || !selectionOnly) {
               if (tr->channel == VTrack::MonoChannel)
                  mixer->MixMono((WaveTrack *) tr, t, t + deltat);
               if (tr->channel == VTrack::LeftChannel)
                  mixer->MixLeft((WaveTrack *) tr, t, t + deltat);
               if (tr->channel == VTrack::RightChannel)
                  mixer->MixRight((WaveTrack *) tr, t, t + deltat);
            }
         }
         tr = iter.Next();
      }

      sampleType *mixed = mixer->GetBuffer();

      long b2 = snd_convert(&sndfile, buffer,   // to
                            &sndbuffer, mixed, numSamples);     // from

      int actual = snd_write(&sndfile, buffer, b2);

      t += deltat;

      if (!progress && wxGetElapsedTime(false) > 500) {

         wxString message;

         if (selectionOnly)
            message =
                wxString::
                Format("Exporting the selected audio as a %s file",
                       (const char *) format);
         else
            message =
                wxString::
                Format("Exporting the entire project as a %s file",
                       (const char *) format);

         progress =
             new wxProgressDialog("Export",
                                  message,
                                  1000,
                                  parent,
                                  wxPD_CAN_ABORT |
                                  wxPD_REMAINING_TIME | wxPD_AUTO_HIDE);
      }
      if (progress) {
         cancelling =
             !progress->Update(int (((t - t0) * 1000) / (t1 - t0) + 0.5));
      }

      delete mixer;
      delete[]buffer;
   }

   snd_close(&sndfile);

#ifdef __WXMAC__

   FSSpec spec;

   wxMacFilename2FSSpec(fName, &spec);

   if (trackMarkers) {
      // Export the label track as "CD Spin Doctor" files

      LabelTrack *labels = NULL;
      TrackListIterator iter(tracks);
      VTrack *t = iter.First();
      while (t && !labels) {
         if (t->GetKind() == VTrack::Label)
            labels = (LabelTrack *) t;
         t = iter.Next();
      }
      if (labels) {
         FSpCreateResFile(&spec, 'AIFF', AUDACITY_CREATOR, 0);
         int resFile = FSpOpenResFile(&spec, fsWrPerm);
         if (resFile == -1) {
            int x = ResError();
         }
         if (resFile != -1) {
            UseResFile(resFile);

            int numLabels = labels->mLabels.Count();
            for (int i = 0; i < numLabels; i++) {
               int startBlock = (int) (labels->mLabels[i]->t * 75);
               int lenBlock;
               if (i < numLabels - 1)
                  lenBlock =
                      (int) ((labels->mLabels[i + 1]->t -
                              labels->mLabels[i]->t) * 75);
               else
                  lenBlock =
                      (int) ((tracks->GetMaxLen() -
                              labels->mLabels[i]->t) * 75);
               int startSample = startBlock * 1176 + 54;
               int lenSample = lenBlock * 1176 + 54;

               Handle theHandle = NewHandle(50);
               HLock(theHandle);
               char *data = (char *) (*theHandle);
               *(int *) &data[0] = startSample;
               *(int *) &data[4] = lenSample;
               *(int *) &data[8] = startBlock;
               *(int *) &data[12] = lenBlock;
               *(short *) &data[16] = i + 1;

               wxString title = labels->mLabels[i]->title;
               if (title.Length() > 31)
                  title = title.Left(31);
               data[18] = title.Length();
               strcpy(&data[19], (const char *) title);

               HUnlock(theHandle);
               AddResource(theHandle, 'SdCv', 128 + i, "\p");
            }
            CloseResFile(resFile);

            wxMessageBox("Saved track information with file.");
         }
      }
   }

   FInfo finfo;
   if (FSpGetFInfo(&spec, &finfo) == noErr) {
      switch (header) {
      case SND_HEAD_AIFF:
         finfo.fdType = 'AIFF';
         break;
      case SND_HEAD_IRCAM:
         finfo.fdType = 'IRCA';
         break;
      case SND_HEAD_NEXT:
         finfo.fdType = 'AU  ';
         break;
      case SND_HEAD_WAVE:
         finfo.fdType = 'WAVE';
         break;
      }

      finfo.fdCreator = AUDACITY_CREATOR;

      FSpSetFInfo(&spec, &finfo);
   }
#endif

   if (progress)
      delete progress;

   return true;

}