~ubuntu-branches/ubuntu/gutsy/audacity/gutsy-backports

« back to all changes in this revision

Viewing changes to src/effects/NoiseRemoval.cpp

  • Committer: Bazaar Package Importer
  • Author(s): John Dong
  • Date: 2008-02-18 21:58:19 UTC
  • mfrom: (13.1.2 hardy)
  • Revision ID: james.westby@ubuntu.com-20080218215819-tmbcf1rx238r8gdv
Tags: 1.3.4-1.1ubuntu1~gutsy1
Automated backport upload; no source changes.

Show diffs side-by-side

added added

removed removed

Lines of Context:
47
47
#include "../WaveTrack.h"
48
48
#include "../Prefs.h"
49
49
#include "../Project.h"
 
50
#include "../FileNames.h"
50
51
 
51
52
#include <math.h>
52
53
 
70
71
#include <wx/textctrl.h>
71
72
 
72
73
#include "../AudacityApp.h"
 
74
#include "../PlatformCompatibility.h"
73
75
 
74
76
EffectNoiseRemoval::EffectNoiseRemoval()
75
77
{
83
85
   gPrefs->Read(wxT("/CsPresets/NoiseAttackDecayTime"),
84
86
                &mAttackDecayTime, 0.15);
85
87
 
86
 
   mMinSignalTime = 0.05;
 
88
   mMinSignalTime = 0.05f;
87
89
   mHasProfile = false;
88
90
   mDoProfile = true;
89
91
 
116
118
         return;
117
119
      }
118
120
   }
 
121
 
119
122
   // Try to open the file.
120
 
   wxString filename = wxT("noisegate.nrp");
 
123
   if( !wxDirExists( FileNames::NRPDir() ))
 
124
      return;
 
125
 
121
126
   // if file doesn't exist, return quietly.
122
 
   if( !wxFile::Exists( filename ))
 
127
   wxString fileName = FileNames::NRPFile();
 
128
   if( !wxFileExists( fileName ))
123
129
      return;
124
 
   wxFFile noiseGateFile(filename, wxT("rb"));
 
130
 
 
131
   wxFFile noiseGateFile(fileName, wxT("rb"));
125
132
   bool flag = noiseGateFile.IsOpened();
126
133
   if (flag != true)
127
134
      return;
131
138
   int count = noiseGateFile.Read(mNoiseThreshold, expectedCount);
132
139
   noiseGateFile.Close();
133
140
   if (count == expectedCount) {
134
 
      for (int i = halfWindowSize; i < mWindowSize; ++i) {
135
 
         mNoiseThreshold[i] = float(0.0);  // only half filled by Read
 
141
      for (int i = halfWindowSize; i < mSpectrumSize; ++i) {
 
142
         mNoiseThreshold[i] = float(0.0);  // only partly filled by Read?
136
143
      }
137
144
      mHasProfile = true;
138
145
      mDoProfile = false;
144
151
   AudacityProject * project = GetActiveProject();
145
152
   if( !project || !project->GetCleanSpeechMode() )
146
153
      return;
147
 
   wxFFile noiseGateFile(wxT("noisegate.nrp"), wxT("wb"));
 
154
 
 
155
   // code borrowed from ThemeBase::SaveComponents() - MJS
 
156
   // IF directory doesn't exist THEN create it
 
157
   if( !wxDirExists( FileNames::NRPDir() ))
 
158
   {
 
159
      /// \bug 1 in wxWidgets documentation; wxMkDir returns false if 
 
160
      /// directory didn't exist, even if it successfully creates it.
 
161
      /// so we create and then test if it exists instead.
 
162
      /// \bug 2 in wxWidgets documentation; wxMkDir has only one argument
 
163
      /// under MSW
 
164
#ifdef __WXMSW__
 
165
      wxMkDir( FileNames::NRPDir().fn_str() );
 
166
#else
 
167
      wxMkDir( FileNames::NRPDir().fn_str(), 0700 );
 
168
#endif
 
169
      if( !wxDirExists( FileNames::NRPDir() ))
 
170
      {
 
171
         wxMessageBox(
 
172
            wxString::Format( 
 
173
            _("Could not create directory:\n  %s"),
 
174
               FileNames::NRPDir().c_str() ));
 
175
         return;
 
176
      }
 
177
   }
 
178
 
 
179
   wxString fileName = FileNames::NRPFile();
 
180
   fileName = PlatformCompatibility::GetLongFileName(fileName);
 
181
   wxFFile noiseGateFile(fileName, wxT("wb"));
148
182
   bool flag = noiseGateFile.IsOpened();
149
183
   if (flag == true) {
150
184
      int expectedCount = (mWindowSize / 2) * sizeof(float);
152
186
      noiseGateFile.Write(mNoiseThreshold, expectedCount);
153
187
      noiseGateFile.Close();
154
188
   }
 
189
   else {
 
190
      wxMessageBox(
 
191
         wxString::Format( 
 
192
         _("Could not open file:\n  %s"), fileName.c_str() ));
 
193
      return;
 
194
   }
155
195
}
156
196
 
157
197
#define MAX_NOISE_LEVEL  30
180
220
 
181
221
   if( !mHasProfile )
182
222
   {
183
 
      CleanSpeechMayReadNoisegate();
 
223
      AudacityProject * p = GetActiveProject();
 
224
      if (p->GetCleanSpeechMode())
 
225
         CleanSpeechMayReadNoisegate();
184
226
   }
185
227
 
186
228
   // We may want to twiddle the levels if we are setting
232
274
   // This should only happen in CleanSpeech.
233
275
   if(!mDoProfile && !mHasProfile) {
234
276
      wxMessageBox(
235
 
        _("Attempt to run Noise Removal without a noise profile\n."));
 
277
        _("Attempt to run Noise Removal without a noise profile.\n"));
236
278
      return false;
237
279
   }
238
280
 
240
282
 
241
283
   // This same code will both remove noise and profile it,
242
284
   // depending on 'mDoProfile'
243
 
   TrackListIterator iter(mWaveTracks);
 
285
   this->CopyInputWaveTracks(); // Set up m_pOutputWaveTracks.
 
286
   bool bGoodResult = true;
 
287
 
 
288
   TrackListIterator iter(m_pOutputWaveTracks);
244
289
   WaveTrack *track = (WaveTrack *) iter.First();
245
290
   int count = 0;
246
291
   while (track) {
256
301
 
257
302
         if (!ProcessOne(count, track, start, len)) {
258
303
            Cleanup();
259
 
            return false;
 
304
            bGoodResult = false;
 
305
            break;
260
306
         }
261
307
      }
262
308
      track = (WaveTrack *) iter.Next();
263
309
      count++;
264
310
   }
265
311
 
266
 
   if (mDoProfile) {
 
312
   if (bGoodResult && mDoProfile) {
267
313
      CleanSpeechMayWriteNoiseGate();
268
314
      mHasProfile = true;
269
315
      mDoProfile = false;
270
316
   }
271
317
 
272
 
   Cleanup();
273
 
   return true;
 
318
   if (bGoodResult)
 
319
      Cleanup();
 
320
   this->ReplaceProcessedWaveTracks(bGoodResult); 
 
321
   return bGoodResult;
274
322
}
275
323
 
276
324
void EffectNoiseRemoval::ApplyFreqSmoothing(float *spec)