File: AudioFileWriting_example.cxx

package info (click to toggle)
clam 1.4.0-6
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 17,836 kB
  • ctags: 20,981
  • sloc: cpp: 92,504; python: 9,721; ansic: 1,602; xml: 444; sh: 239; makefile: 153; perl: 54; asm: 15
file content (136 lines) | stat: -rw-r--r-- 4,555 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
/*
 * Copyright (c) 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
 *
 */

// Welcome to the "AudioFileWriting" example. In this example we will show you
// how to use CLAM::MultiChannelAudioFileWriter, a Processing that allows you
// to write simultaneously several channels at once from the same file.

// Before attempting to follow this example you should take a look first on the
// "FileInfo" example ( examples/FileInfo_example.cxx.cxx ) and the
// "AudioFileReading" example ( examples/AudioFileReading_example.cxx ).

// We need to include the following files to have access to classes
// AudioFile, MultiChannelAudioFileWriter and MultiChannelAudioFileWriterConfig
#include "AudioFile.hxx"
#include "MultiChannelAudioFileWriter.hxx"

// We also need the declaration of the CLAM::Audio object
#include "Audio.hxx"

// For accessing <cmath> header in a cross-platform way
#include "CLAM_Math.hxx"
#include "DataTypes.hxx"
#include "OSDefines.hxx"

#include <vector>

#include <iostream>

int main( int argc, char** argv )
{
	// As we did in the FileInfo example we will take the output file name from
	// the command line.

	if ( argc == 1 ) // No input file
	{
		std::cerr << "No input file" << std::endl;
		std::cerr << "Usage: AudioFileWriting <output file name>" << std::endl;

		exit( - 1 );
	}
	else if ( argc > 2 ) // Too many parameters
	{
		std::cerr << "Too many parameters" << std::endl;
		std::cerr << "Usage: AudioFileWriting <output file name>" << std::endl;
		
		exit( -1 );
	}
	// As we can be quite sure that we have one parameter at argv[1].
	// Lets prepare the configuration for our file writer.
	const CLAM::TData sampleRate = 44100.; // Hz
	const CLAM::TSize nChannels = 2;
	CLAM::MultiChannelAudioFileWriterConfig configObject;
	// File format (ogg, wav, aiff...) is deduced from the file name extension.
	configObject.SetTargetFile( argv[1] );
	// This is an example on how to change the number of channels
	// and the sample rate. 
	// As we are using the defaults (2 channels and 44100 Hz)
	// we don't need to do it but just as an example on how  to do it.
	configObject.SetSampleRate( sampleRate );
	configObject.SetNChannels( nChannels );

	// Now we can instantiate the MultiChannelAudioFileWriter and configure it
	CLAM::MultiChannelAudioFileWriter writer;
	if ( ! writer.Configure( configObject ) )
	{
		std::cerr << writer.GetConfigErrorMessage() << std::endl;
		exit(-1);
	}

	// Finally, we must set the data we want to write on to the file. We will just implement
	// a simple sinusoidal synthesis, where in each channel there is a sinusoid with the same
	// frequency but different phase

	const CLAM::TData sineFreq = 440.; // Hz
	// initial phases
	CLAM::TData phi[] = { -M_PI / 2.0, M_PI/2.0 };

	// Delta phase: this trick allows us to compute incrementally current sine
	// phase
	CLAM::TData dphi[] = { ( 2.0 * M_PI * sineFreq ) / sampleRate,  ( 2.0 * M_PI * sineFreq ) / sampleRate};

	// Now we must decide our 'write size', i.e. the number of samples we will write
	// at once at the same channel
	const CLAM::TSize writeSize = 2048; // 0.0464 s

	// And now we set our 'input buffers', where we will be generating the sines
	
	std::vector< CLAM::Audio > inputs;
	inputs.resize( nChannels );         // As many inputs as channels

	for ( unsigned i = 0; i < inputs.size(); i++ )
	{
		inputs[i].SetSize( writeSize );
	}

	// Now we want to synthesize approx. 1s

	writer.Start();
	
	for ( int i = 0; i < 25; i++ )
	{
		for ( unsigned int j = 0; j < inputs.size(); j++ )
			for ( int k = 0; k < inputs[j].GetSize(); k++ )
			{
				inputs[j].GetBuffer()[k] = 0.5 * cos( phi[j] );
				phi[j] += dphi[j];
			}

		// Once both input buffers have been generated we call
		// the writer Do()
		writer.Do( inputs );
	}
	
	writer.Stop();

	return 0;
}