LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

How to convert data from graph to wavefile?

I'm having trouble with converting my data from graph to wavefile. This program I have below will make a wavefile with the right header (I think) and add in the raw data from graph at the end. But for some reason, it doesn't play at all. I wonder if anyone would please take a look at this to see what's wrong with it. I made up the code based on: http://www.technology.niagarac.on.ca/courses/comp630/WavFileFormat.html and a few other sources.

Code:
=====

#include "toolbox.h"
#include "expdef_n.h"
#include
#include

// Constants
const int sampleRate = 22050; // Set sample rate
const int sampleBits = 8; // Set sample bits
const int numChannels = 1; // Set number of channel
const int goToRiffLength = 4; // The position to go back to add package length
const int goToDataLength = 40; // The position to go back to add data length

// Little Endian
void writeToWave (FILE *waveFile, unsigned int x, int len)
{
int i;

for (i=0; i {
unsigned int r = x & 0xFF;
x >>= 8;
fputc (r, waveFile);
}
}

// Put Raw Data into the wave file
int PutInWaveRawData (float *data, int count, char *newfilename)
{

FILE *outfile;
int check_count;

outfile = fopen (newfilename, "ab");
check_count = fwrite (data, sizeof (float), count, outfile);
if (check_count != count)
{
fclose (outfile);
return (-1);
}
fclose(outfile);
return (0);
}

// Print Header File and Sample Data
void initWaveFile (FILE *waveFile, float *data, int datacount, char *name)
{
fprintf(waveFile, "RIFF"); // Print RIFF ASCII
writeToWave (waveFile, 0, 4); // Print unknown length of package (binary, little endian)
fprintf(waveFile, "WAVEfmt "); // Print WAVEfmt ASCII
writeToWave (waveFile, 16, 4); // Print format chunk length (binary, always 0x10)
writeToWave (waveFile, 1, 2); // Print PCM data (always 0x01)
writeToWave (waveFile, numChannels, 2); // Print number of channels (always 0x01=Mono, 0x02=Stereo)
writeToWave (waveFile, sampleRate, 4); // Print Sample Rate Sample Rate (binary, in Hz)
writeToWave (waveFile, sampleRate * numChannels * (sampleBits/8), 4); // Print data rate
writeToWave (waveFile, numChannels * (sampleBits/8), 2); // Print align
writeToWave (waveFile, sampleBits, 2); // Print sample bits
fprintf(waveFile, "data"); // Print data ASCII
writeToWave (waveFile, 0, 4); // Print unknown data length

PutInWaveRawData (data, datacount, name); // Add raw data after unknown data length
}

// Go Back, edit Header File
void editHeader (FILE *waveFile)
{
int length = ftell (waveFile); // Find the file length

fseek (waveFile, goToRiffLength, SEEK_SET); // Go to position after "RIFF"
writeToWave (waveFile, length - 8, 4); // Write length of package - 8 (binary, little endian)
fseek (waveFile, goToDataLength, SEEK_SET); // Go to position after "data"
writeToWave (waveFile, length - 44, 4); // Write data length - 44

fclose (waveFile);
}

// Make wave file
int MakeWavFile (float *data, int datacount, char *wavfilename)
{
FILE *BinWavFile;
char tempfilename[260];

strcpy (tempfilename, wavfilename);
strcat (tempfilename, ".wav");

// Open and create wave file
BinWavFile = fopen (tempfilename, "w");
initWaveFile(BinWavFile, data, datacount, tempfilename);

editHeader (BinWavFile);

return 0;
}
0 Kudos
Message 1 of 4
(3,753 Views)
The first problem you have is that you open the WAV file twice, once in MakeWavFile() and again in PutInWaveRawData(). This is unlikely to work.

The second problem is that you are specifying a WAV file format that is 8-bit integer PCM (sampleBits=8) but you are writing floating point data to the file. You will need to convert the data to the right format before writing it.

I think you need to look at the code a little more closely!

Attached is a DOS utility for analysing the structure of a RIFF file, that might help you in your subsequent development.

Martin.
--
Martin
Certified CVI Developer
0 Kudos
Message 2 of 4
(3,752 Views)
1º - Excuse for my english
2º - i work with CVI and i need record from microphone and save in .wav format, somebody help me please or samebody know any example.
thanks...
0 Kudos
Message 3 of 4
(3,674 Views)

http://ccrma.stanford.edu/CCRMA/Courses/422:1998/projects/WaveFormat/
--
Regards,

John Cameron.
Type softly, read gently.
0 Kudos
Message 4 of 4
(3,670 Views)