/*-------------------------------------------------------------------------
* SphereInterface.c - Library of functions to read different wavefile formats
* Version: $Name: $
* Module:
*
* Purpose:
* See:
*
* Author: Hema Murthy (hema@bhairavi.iitm.ernet.in)
*
* Created: Thu 01-Mar-2007 12:19:44
* Last modified: Wed 13-Jun-2007 08:45:53 by hema
* $Id: SphereInterface.c,v 1.4 2007/06/14 08:25:49 hema Exp hema $
*
* Bugs:
*
* Change Log: <Date> <Author>
* <Changes>
-------------------------------------------------------------------------*/
#include <stdio.h>
#include <string.h>
#include "stdlib.h"
#include "SphereInterface.h"
/*****************************************************************************
function : WriteRIFF - write to a file in RIFF format assumes
data is sampled at 16KHz and quantised to 16bits/sample
Inputs : wavefilename
Outputs : short waveform, numSamples
**************************************************************************/
//=================== WriteWaveFile ===================//
int
WriteRIFF (char *waveFileName, short *waveform, long numsamples,
long samplingRate, int bytesPerSample)
{
FILE *file;
RIFFWaveFile *myRIFF = NULL;
long i = 0;
long size = 0;
int result = SUCCESS;
file = fopen (waveFileName, "w");
myRIFF = (RIFFWaveFile *) malloc (sizeof (RIFFWaveFile));
myRIFF->header = (RIFFHeader *) malloc (sizeof (RIFFHeader));
strcpy (myRIFF->header->riffChunkID, "RIFF");
myRIFF->header->riffChunkDataSize = numsamples * 2 + 36;
strcpy (myRIFF->header->riffType, "WAVE");
strcpy (myRIFF->header->fmtChunkID, "fmt ");
myRIFF->header->fmtChunkDataSize = 16;
myRIFF->header->compressionCode = 1;
myRIFF->header->numberOfChannel = 1;
myRIFF->header->sampleRate = samplingRate;
myRIFF->header->averageBytesPerSecond = bytesPerSample * samplingRate;
myRIFF->header->blockAlign = 2;
myRIFF->header->sigBitsPerSample = bytesPerSample * 8;
strcpy (myRIFF->header->dataChunkID, "data");
myRIFF->header->dataChunkDataSize = numsamples * 2;
size = myRIFF->header->dataChunkDataSize / 2;
printf ("size:%d\n", size);
myRIFF->data = (short int *) malloc (sizeof (short int) * size);
for (i = 0; i < size; i++)
{
myRIFF->data[i] = waveform[i];
}
result = WriteRIFFWaveFile (myRIFF, size * 2, file);
fclose (file); /*!! was AFTER return */
return result;
}
//=================== WriteRIFFWaveFile ===================//
int
WriteRIFFWaveFile (RIFFWaveFile * riff, int size, FILE * file)
{
int result = SUCCESS;
if (fwrite (riff->header, 44, 1, file) != 1)
{
result = FAILURE;
}
if (fwrite (riff->data, size, 1, file) != 1)
{
result = FAILURE;
}
return result;
}
int
main ()
{
short data[] = { 1, 2, 3, 4, 5 };
WriteRIFF ("wav.wav", data, 5, 123, 2 );
return 0;
}
$ gcc SphereInterface.c
In file included from SphereInterface.c:24:
SphereInterface.h:31: warning: ‘packed’ attribute ignored
$ valgrind ./a.out
==5800== Memcheck, a memory error detector.
==5800== Copyright (C) 2002-2007, and GNU GPL'd, by Julian Seward et al.
==5800== Using LibVEX rev 1804, a library for dynamic binary translation.
==5800== Copyright (C) 2004-2007, and GNU GPL'd, by OpenWorks LLP.
==5800== Using valgrind-3.3.0-Debian, a dynamic binary instrumentation framework.
==5800== Copyright (C) 2000-2007, and GNU GPL'd, by Julian Seward et al.
==5800== For more details, rerun with: -v
==5800==
size:5
==5800==
==5800== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 11 from 1)
==5800== malloc/free: in use at exit: 62 bytes in 3 blocks.
==5800== malloc/free: 4 allocs, 1 frees, 414 bytes allocated.
==5800== For counts of detected errors, rerun with: -v
==5800== searching for pointers to 3 not-freed blocks.
==5800== checked 60,188 bytes.
==5800==
==5800== LEAK SUMMARY:
==5800== definitely lost: 62 bytes in 3 blocks.
==5800== possibly lost: 0 bytes in 0 blocks.
==5800== still reachable: 0 bytes in 0 blocks.
==5800== suppressed: 0 bytes in 0 blocks.
==5800== Rerun with --leak-check=full to see details of leaked memory.
$ ls -l wav.wav
-rw-r--r-- 1 user group 54 2009-08-26 19:42 wav.wav