File: ThreadedProcessing_example.cxx

package info (click to toggle)
clam 1.4.0-5.1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 17,780 kB
  • sloc: cpp: 92,499; python: 9,721; ansic: 1,602; xml: 444; sh: 239; makefile: 153; perl: 54; asm: 15
file content (259 lines) | stat: -rw-r--r-- 7,244 bytes parent folder | download | duplicates (2)
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
/*
 * Copyright (c) 2001-2004 MUSIC TECHNOLOGY GROUP (MTG)
 *                         UNIVERSITAT POMPEU FABRA
 *
 *
 * 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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 */
#include "AudioFileIn.hxx"       // this header imports the CLAM::AudioFileIn Processing class interface
#include "LPC_AutoCorrelation.hxx" // imports CLAM LPC_AutoCorrelation Processing declaration
#include "LPModel.hxx"             // imports CLAM::LPModel ProcessingData declaration
#include "Audio.hxx"               // imports CLAM::Audio ProcessingData declaration
#include "Spectrum.hxx"            // imports CLAM::Spectrum ProcessingData declaration
#include "SpectrumConfig.hxx"      // imports CLAM::Spectrum auxiliar datatypes declaration
#include "WindowGenerator.hxx"     // imports CLAM::WindowGenerator Processing declaration
#include "AudioMultiplier.hxx"     // imports CLAM::AudioMultiplier Processing declaration
#include "DownSampling.hxx"
#include "Plot.hxx"
#include "SystemPlots.hxx"
#include "Thread.hxx"
#include "Err.hxx"
#include <iostream>
#include <FL/fl_file_chooser.H>  // imports FLTK functions for file choosing dialogs


// Our simple speech analysis system declaration
class SimpleSpeechAnalysisSystem
{
public: // Methods
	SimpleSpeechAnalysisSystem();
	~SimpleSpeechAnalysisSystem();
	void Run();

protected: 
	// Methods

	void LoadInputSpeechFile();
	void GenerateAnalysisWindow();
	void SetupData();
	void SetupDownsampler();

protected: // Attributes

	CLAMVM::Plot                 mSpeechPlot;
	CLAMVM::Plot                 audioPlot;
	CLAM::Audio                  mInputSignal;
	CLAM::Audio                  mAnalysisWindow;
	CLAM::LPModel                mLPModel;
	CLAM::Spectrum               mLPSpectrum;
	CLAM::LPC_AutoCorrelation    mLPC;
	CLAM::DownSampling           mDownSampler;
	CLAM::AudioMultiplier        mWindowApplier;
	CLAM::TSize                  mFrameSize;
	CLAM::TSize                  mDownSampledFrameSize;
	CLAM::WindowGenerator winGen;
	CLAM::WindowGeneratorConfig wndGenCfg;


};

// And our simple speech analysis system definition
SimpleSpeechAnalysisSystem::SimpleSpeechAnalysisSystem()
	: mSpeechPlot( "speech spectrum" ),
	audioPlot("aaa")
{
	LoadInputSpeechFile();
	SetupDownsampler();
	GenerateAnalysisWindow();
	SetupData();
	mSpeechPlot.SetLabel( "Speech spectrum" );
	mSpeechPlot.SetPosition( 100, 100 );
	mSpeechPlot.SetSize( 640, 480 );
	mSpeechPlot.SetColor( CLAMVM::Color( 127, 150, 0 ) );
	mSpeechPlot.SetYRange( -50, 20 );
	audioPlot.SetLabel( "Input speech" );
	audioPlot.SetPosition( 100, 100 );
	audioPlot.SetSize( 640, 480 );
	audioPlot.SetColor( CLAMVM::Color( 127, 150, 0 ) );
	audioPlot.SetYRange( -1, 1 );


}

SimpleSpeechAnalysisSystem::~SimpleSpeechAnalysisSystem()
{
}

void SimpleSpeechAnalysisSystem::SetupDownsampler()
{
	CLAM::DownSamplingConfig cfg;
	cfg.SetDownSamplingRate( 10000 );
	mDownSampledFrameSize = CLAM::TSize( 10000.0*0.02 );
	mDownSampler.Configure( cfg );
}

void SimpleSpeechAnalysisSystem::LoadInputSpeechFile()
{
	CLAM::AudioFileConfig fileLoaderConfig;
	CLAM::AudioFileIn   fileLoader; 
	const char* filename = fl_file_chooser( "Please, select a .wav file", "*.wav", NULL );

	if ( filename == NULL )
	{
		std::cout << "User cancelled" << std::endl;
		exit(0);
	}

	fileLoaderConfig.SetFilename( filename );
	fileLoaderConfig.SetFiletype( CLAM::EAudioFileType::eWave ); 

	if ( !fileLoader.Configure( fileLoaderConfig ) )
	{
		std::cerr << "ERROR: could not open " << filename << std::endl;
		exit(-1);
	}

	CLAM::TSize nSamples = fileLoader.Size();
	CLAM::TData sampleRate = fileLoader.SampleRate();
		
	mInputSignal.SetSize( nSamples );
	mInputSignal.SetSampleRate( sampleRate );

	fileLoader.Start();      
	fileLoader.Do( mInputSignal );  
	fileLoader.Stop();

	mFrameSize = CLAM::TSize( mInputSignal.GetSampleRate()*0.02 );

}

void SimpleSpeechAnalysisSystem::GenerateAnalysisWindow()
{
	mAnalysisWindow.SetSize( mDownSampledFrameSize );
	wndGenCfg.SetType( CLAM::EWindowType::eHamming);//eBlackmanHarris92);
	wndGenCfg.SetSize( mDownSampledFrameSize );
	wndGenCfg.SetNormalize( CLAM::EWindowNormalize::eAnalysis );

}

void SimpleSpeechAnalysisSystem::SetupData()
{
	
	const unsigned lpcOrder = 22;
	mLPModel.UpdateModelOrder( lpcOrder );
	mLPModel.SetSpectralRange( mInputSignal.GetSampleRate() / 2 );
	CLAM::LPCConfig cfg;
	cfg.SetOrder( lpcOrder );
	mLPC.Configure( cfg );
	CLAM::SpecTypeFlags flags;
	flags.bMagPhase=1;
	flags.bComplex = 0;
	
	mLPSpectrum.SetSize( 100 );
	mLPSpectrum.SetSpectralRange( 5000.0 );
	mLPSpectrum.SetType( flags );
	
}

void SimpleSpeechAnalysisSystem::Run()
{

	// 20 ms length analysis frame
	CLAM::TSize hopSize = mFrameSize / 2;
	CLAM::Audio currentSampleFrame;
	currentSampleFrame.SetSize( mFrameSize );
	CLAM::Audio analysisFrame;
	analysisFrame.SetSize(  mDownSampledFrameSize );
	CLAM::TIndex currentOffset = 0;
	mLPModel.SetSpectralRange( mInputSignal.GetSampleRate()/2 );

	mDownSampler.Start();
	mWindowApplier.Start();
	mLPC.Start();

	int i = 0;

	while ( currentOffset < mInputSignal.GetSize() - hopSize )
	{
		std::cout << ".";
		std::cout.flush();
		mInputSignal.GetAudioChunk( currentOffset, currentOffset+mFrameSize,
									currentSampleFrame, true );
		mDownSampler.Do( currentSampleFrame, analysisFrame );

		audioPlot.SetData( analysisFrame.GetBuffer(), 0, 1 );

		mAnalysisWindow.SetSize( analysisFrame.GetSize() );
		wndGenCfg.SetSize( analysisFrame.GetSize() );
		winGen.Configure( wndGenCfg );
		winGen.Start();
		winGen.Do( mAnalysisWindow );
		winGen.Stop();

		mWindowApplier.Do( analysisFrame, mAnalysisWindow, analysisFrame );
		mLPC.Do( analysisFrame, mLPModel );
		mLPModel.ToSpectrum( mLPSpectrum );
		mLPSpectrum.ToDB();
		mSpeechPlot.SetData( mLPSpectrum.GetMagBuffer(), 0, 5000 );

		CLAMVM::SystemPlots::DisplayAll();

		i++;
		for ( int i = 0; i < 22; i++ )
		{
			mLPModel.GetFilterCoefficients()[i] = 0;
			mLPModel.GetReflectionCoefficients()[i] = 0;

		}
		mLPModel.SetAvgSqrFilterError( 0 );
		currentOffset += hopSize;
	}

	mLPC.Stop();
	mWindowApplier.Stop();
	mDownSampler.Stop();
}

int main( int argc, char** argv )
{
	try // Note the exception handling
	{
		CLAM::Thread               thread;
		SimpleSpeechAnalysisSystem sys;


		thread.SetThreadCode( 
			makeMemberFunctor0( sys, SimpleSpeechAnalysisSystem, Run ) );
		
		thread.Start();

		thread.Stop();

	}
	catch( CLAM::Err& e ) // Here we handle CLAM exceptions
	{
		e.Print();
		exit(-1);
	}
	catch( std::exception& e ) // Here we handle standard library exceptions
	{
		std::cerr << e.what() << std::endl;
		exit(-1);
	}

	return 0;
}