[Stk] Reading and writing disk wav file and the real time recorded wav file

alpana jui lpnjui at googlemail.com
Tue Oct 7 15:26:50 PDT 2008


Dear STK:

 Thanks for the support so far.

I am still trying to use Stk for my application.



 At present, I could only use that is available on XCode due to some
time restriction and accordingly, the version of this is STK-4.2.1(and
the package of
 stk-4.2.1+StkX ).  I think this has all that i need for my present
application.

I tried to wrote a small program following some examples but I do not
know if I am in the right tract to manage my audio interface for
reading my disk stored wav file and to write and read the audio file
by Stk.

The task is with Stk:
1. read wavfile from disk in a vector form and pass this to another
signal prototype written by some colleague in the University. But this
only accepts vector form of the audio file as a float value.

2. record some commands and pass this to the signal analysis
component. In this case also, reading the recorded file by Stk is
required. I do not need to play back the audio wav file at all except
for my testing if this is recording the right command.


#include "RtWvIn.h"
#include "FileWvOut.h"
#include "FileWvIn.h"
#include "Signal.h"
#include <cstring>
#include <iostream>
#include <fstream>


using namespace std;

//pass through the calback function
int pass(char *buffer, int bufferSize, void *appData){

	return 0;
}

struct AudIoIn{
	int channels;
	double sample_rate;
	char fileName[];
	double time;
	
};

int main (int argc, char* const argv[]) {
	
	Stk::setSampleRate( 44100.0 );
	
	StkFrames sampleData;
	//char *trainingInput;

	/**
	
	 The first task of this audioIn is to read the
	 disk storage audio file(in wav format at present)
	
	 And the following program does this just for one sample wav file
	 but this requires to read all the wav files in the disk for this application
	 **/
	string trainingfile_name =
"/Users/Desktop/tukl_project/stkl/lich.wav"; //"licht.wav", "an.wav",
"licht.an", "notruf.wav"
	FileWvIn *training_input = new FileWvIn; //(trainingInput, false);

	
	try {
		
		
		training_input->openFile(trainingfile_name, false);
	
	}
	catch(StkError &)
	{
		exit(1);
	}
	 /** the following interface is not giving me error as no matching function**/
	
	//training_input.computeFrame();
	
	int  trainingInput_length = training_input->getSize();
	ofstream trainingIn("lichtan.txt");
	for(int i = 0;  i < trainingInput_length; i++){
	
	 	StkFloat trainingIn_Buffer = training_input->tick();
		trainingIn << trainingIn_Buffer <<endl;
		
	}
	

	
	
	/**
	 The second part of the audioInput is to record the command from the
	 speaker and then write it to the disk for reading
	
	 and the following program does this so far for recording
	
	 ?? but not known to me yet how I can read this to pass this for
signal analysis
	
	 **/
	char* MicIn = "testin.wav";
	float length = 3;
		RtWvIn adc;	// RtWvIn: realtime input from ADC
	
	FileWvOut output;
	output.openFile( MicIn, 1, FileWrite::FILE_WAV, Stk::STK_SINT16 );
	
	for ( int i=0; i<(44100*length); i++ )
		output.tick( adc.tick() );
	
	output.closeFile();
	//testing part for the spoken command processing start from here
	FileWvIn testin;
	
	
	try {
		
		
		testin.openFile("/Users/Desktop/tukl_project/stkl/build/Debug/testin.wav",
false);
		
	}
	catch(StkError &)
	{
		exit(1);
	}
	ofstream testIn("testin.txt");
	for(int i = 0;  i < testin.getSize(); i++){
		
	 	StkFloat testIn_Buffer = testin.tick();
		testIn << testIn_Buffer <<endl;
		
	}
	return 0;
}


I tried to see the output of the disk storage file and this is as follows

some samples are attached:
for disk stoage file:
-0.0287633
-0.0288629
-0.0289626
-0.0290622
-0.0291619
-0.0292615
-0.0293774
-0.0295102
-0.0296431
-0.029776
-0.0299088
-0.0300417
...
.
.

But when I tried to check recorded wav txt file, it has all zeros.

0
0
0
0
0
0
0
0
0
0
0
0
0
0
0...
..

But the file is recording well and it plays what I tried to record.
But trying to read this recording gives me "0" value.  I do not know
yet what I am doing wrong.  I am working to improve my C++ skills and
the attached program perhaps is missing a well structured programming
style.  I apologize for the convenience. I tried to go through the
forum but I could not find the similar case that would give me some
idea to try.  I need some help and advice for making this work as
required.

Further,  I could not manage to compute the frame length of the wav
file. How  can I do that?

I have also some questions on the sample text, is the sample is in
vector form or it is just a single sample? How many samples in each
frame ? How can I received the samples in a frame as a vector.


Can FileRead and FileWrite be directly used for my purpose of using
Stk,, i.e., reading and writing the disk files and recorded files,  if
so , would this be possible to get some hints on this, please?



I am not clear about the "tick " method or function or the purpose of
this. I do not know if I need this for my purposes. Could anybody make
comment on this, please?

Do I need to use callback function for my purposes?

I will appreciate any response and support as well as advice  for my
success in my application while trying to use Stk-4.2.1.

Thank you,
Jui



More information about the Stk mailing list