git » fp-git.git » commit c67e5b2

tested: now again wave files may be extracted and imported

author ecalot
2006-02-24 08:16:12 UTC
committer ecalot
2006-02-24 08:16:12 UTC
parent b4ec3f4988faa2c1553662c3bab61761b642df86

tested: now again wave files may be extracted and imported

PR/src/lib/actions/import.c +1 -1
PR/src/lib/formats/wav.c +8 -6
PR/src/lib/layers/autodetect.c +1 -1
PR/src/lib/object/object.c +0 -1
PR/src/lib/object/sound/sound_common.c +5 -1

diff --git a/PR/src/lib/actions/import.c b/PR/src/lib/actions/import.c
index 499330d..fade75e 100644
--- a/PR/src/lib/actions/import.c
+++ b/PR/src/lib/actions/import.c
@@ -111,7 +111,7 @@ int import_full(const char* vFiledat, const char* vDirExt, tResourceList* r, int
 	}
 
 	/* Close file. If empty, don't save */
-	mWriteCloseDatFile(!ok,optionflag,backupExtension);
+	mWriteCloseDatFile(ok,optionflag,backupExtension);
 
 	if (hasFlag(verbose_flag)) fprintf(outputStream,PR_TEXT_IMPORT_DONE,ok,error);
 	return error;
diff --git a/PR/src/lib/formats/wav.c b/PR/src/lib/formats/wav.c
index df9b2a3..de04685 100644
--- a/PR/src/lib/formats/wav.c
+++ b/PR/src/lib/formats/wav.c
@@ -66,15 +66,15 @@ int readWav(const char* file, tBinary* snd, int *pchannels, long *psamplerate, l
 	FILE* fd;
 	int ok;
 	char magic[4];
-	long int ChunkSize;
-	long int SubChunk1Size;
+	long int ChunkSize=0; /* longs must be initialized to avoid trash in 64 bits architectures */
+	long int SubChunk1Size=0;
 	short int AudioFormat;
 	short int NumChannels;
-	long int SampleRate;
-	long int ByteRate;
+	long int SampleRate=0;
+	long int ByteRate=0;
 	short int BlockAlign;
 	short int BitsPerSample;
-	long int SubChunk2Size;
+	long int SubChunk2Size=0;
 	
 	fd=fopen(file,"rb");
 	if (!fd) return PR_RESULT_ERR_FILE_NOT_READ_ACCESS; 
@@ -101,7 +101,7 @@ int readWav(const char* file, tBinary* snd, int *pchannels, long *psamplerate, l
 	/* Validate input vars */	
   ok=ok&& (AudioFormat   == 1 ); /* PCM */
   ok=ok&& (BlockAlign    == NumChannels * BitsPerSample/8 );
-	ok=ok&& ((int)ByteRate      == (int)(SampleRate * NumChannels * BitsPerSample/8) ); /* why int? because I can't compare it with long, never tried in 32 bits */
+/*	ok=ok&& ((int)ByteRate      == (int)(SampleRate * NumChannels * BitsPerSample/8) ); * why int? because I can't compare it with long, never tried in 32 bits */
 	ok=ok&& ((int)ChunkSize     == (int)(4 + (8 + SubChunk1Size) + (8 + SubChunk2Size)) );
   ok=ok&& ((int)SubChunk1Size == (int)16 ); /* PCM chunk */
 /*	ok=ok&& (SubChunk2Size == NumSamples * NumChannels * BitsPerSample/8 );*/
@@ -111,7 +111,9 @@ int readWav(const char* file, tBinary* snd, int *pchannels, long *psamplerate, l
 		snd->size=SubChunk2Size;
 		snd->data=malloc(SubChunk2Size);
 		ok=fread(snd->data,SubChunk2Size,1,fd);
+		fclose(fd);
 	} else {
+		fclose(fd);
 		return PR_RESULT_ERR_FILE_NOT_READ_ACCESS; /* TODO: use a bad format code */
 	}
 	/* TODO: check eof */
diff --git a/PR/src/lib/layers/autodetect.c b/PR/src/lib/layers/autodetect.c
index a8ffe8f..6442f28 100644
--- a/PR/src/lib/layers/autodetect.c
+++ b/PR/src/lib/layers/autodetect.c
@@ -82,7 +82,7 @@ int verifySpeakerHeader(tBinary c) {
 int verifyWaveHeader(tBinary c) {
 	/* format: (checksum)+(0x01)+raw wave */
 	return
-		(c.size>1)&&(c.data[1]==0x01)&&((c.size%3)==2)
+		(c.size>1)&&(c.data[1]==0x01)&&((c.size%3)==2) /* TODO: use WAVE_MAGIC */
 	;
 }
 
diff --git a/PR/src/lib/object/object.c b/PR/src/lib/object/object.c
index 755b40a..27bfa5f 100644
--- a/PR/src/lib/object/object.c
+++ b/PR/src/lib/object/object.c
@@ -172,7 +172,6 @@ tObject readObject(const char* file,tResource* res,int *result) {
 			break;
 		case eResTypeWave:
 			o.obj=objWaveRead(file,result);
-printf("wav: result=%d\n",*result);
 			break;
 		case eResTypeMidi:
 			/*o.obj=objMidiRead(file,res.content,result);*/
diff --git a/PR/src/lib/object/sound/sound_common.c b/PR/src/lib/object/sound/sound_common.c
index b9c2403..9ed1f2c 100644
--- a/PR/src/lib/object/sound/sound_common.c
+++ b/PR/src/lib/object/sound/sound_common.c
@@ -39,6 +39,7 @@ wave.c: Princed Resources :
 #include "common.h"
 #include "wav.h" 
 #include <stdlib.h>
+#include <string.h> /* memcpy */
 #include "dat.h" 
 #include "reslist.h" 
 
@@ -91,7 +92,10 @@ void* objWaveRead(const char* file, int *result) {
 
 int objWaveSet(void* o,tResource* res) {
 	tBinary* wave=o;
-	res->content=*wave;
+	res->content.size=wave->size+1;
+	res->content.data=malloc(wave->size+1);
+	res->content.data[0]=0x01; /* TODO: use WAVE_MAGIC */
+	memcpy(res->content.data+1,wave->data,wave->size);
 	mWriteFileInDatFile(res);
 	return PR_RESULT_SUCCESS;
 }