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
|
/*
* Copyright (C) 2002 by Phil Karn KA9Q
* Copyright (C) 2003 by Jonathan Naylor G4KLX/HB9DRD
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* 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.
*/
#include "Fade.h"
#include "Gaussian.h"
#include "common/Exception.h"
#include "common/SoundFile.h"
#include <cmath>
#include <limits.h>
using namespace std;
int main(int argc, char *argv[])
{
if (argc < 5) {
::fprintf(stderr, "Usage: Fade <snr> <fade period> <infile> <outfile>\n");
return 1;
}
double snr = ::pow(10.0, ::atof(argv[1]) / 20.0);
double fadePeriod = ::atof(argv[2]);
wxString inFileName = wxString(argv[3]);
wxString outFileName = wxString(argv[4]);
try {
CFade noise(snr, fadePeriod, inFileName, outFileName);
noise.run();
}
catch (CException& ex) {
::fprintf(stderr, "Fade: error: %s\n", ex.getMessage().c_str());
return 1;
}
catch (...) {
::fprintf(stderr, "Fade: an error has occured\n");
return 1;
}
return 0;
}
CFade::CFade(double snr, double fadePeriod, const wxString& inFileName, const wxString& outFileName) :
m_snr(snr),
m_fadePeriod(fadePeriod),
m_inFileName(inFileName),
m_outFileName(outFileName)
{
}
CFade::~CFade()
{
}
void CFade::run()
{
CSoundFile in;
in.openRead(m_inFileName, 11025, 8);
CSoundFile out;
out.openWrite(m_outFileName, 11025, 8);
CGaussian gaussian;
int nSamples = int(m_fadePeriod * 11025.0);
double* samples = new double[nSamples];
double fadeIncrement = 2.0 * M_PI / double(nSamples);
double fadePhase = 0.0;
for (;;) {
int len = nSamples;
bool ret = in.read(samples, len);
if (!ret) break;
for (int i = 0; i < len; i++) {
samples[i] *= ::sin(fadePhase);
fadePhase += fadeIncrement;
}
double energy = 0.0;
for (int i = 0; i < len; i++)
energy += samples[i] * samples[i];
/* Compute signal, noise amplitudes */
double signalAmpl = ::sqrt(energy / len);
double noiseAmpl = signalAmpl / m_snr;
/* Set gain to make signal + noise amplitude 12 dB below clipping */
double gain = 1.0 / (4.0 * (signalAmpl + noiseAmpl));
for (int i = 0; i < len; i++){
double s = gain * gaussian.rand(samples[i], noiseAmpl);
samples[i] = (s > 1.0) ? 1.0 : (s < -1.0 ? -1.0 : s);
}
out.write(samples, len, 1.0);
}
in.close();
out.close();
delete[] samples;
}
|