diff music2/wavfile.cc @ 52:15a18fbe6f21

* Known bugs added to the README * Code cleaning (0 -> NULL when needed, indentation, spaces, ...)
author thib
date Sat, 18 Apr 2009 18:35:39 +0000
parents ed6c21dde840
children 4416cfac86ae
line wrap: on
line diff
--- a/music2/wavfile.cc
+++ b/music2/wavfile.cc
@@ -23,19 +23,19 @@
  *
 */
 
-#include        <stdarg.h>
-#include  	<stdio.h>
-#include  	<stdlib.h>
-#include	<errno.h>
-#include	<sys/types.h>
-#include	<unistd.h>
-#include  	<string.h>
-#include        "wavfile.h"
-#include        "system/file.h"
-#include        "music.h"
+#include <stdarg.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <errno.h>
+#include <sys/types.h>
+#include <unistd.h>
+#include <string.h>
+#include "wavfile.h"
+#include "system/file.h"
+#include "music.h"
 
-#define		BUFFERSIZE   		1024
-#define		PCM_WAVE_FORMAT   	1
+#define BUFFERSIZE			1024
+#define PCM_WAVE_FORMAT		1
 
 /*******************************************************
 **
@@ -74,7 +74,8 @@ inline void LittleEndian_putW(int num, c
 	num %= 65536;
 	c0 = num % 256;
 	c1 = num / 256;
-	b[index] = c0; b[index+1] = c1;
+	b[index] = c0;
+	b[index+1] = c1;
 }
 
 typedef  struct
@@ -85,7 +86,7 @@ typedef  struct
 	u_long     dwAvgBytesPerSec ;
 	u_short    wBlockAlign ;
 	u_short    wBitsPerSample ;
-} WAVEFORMAT ;
+} WAVEFORMAT;
 
 typedef  struct
 {	char    	RiffID [4] ;
@@ -101,31 +102,31 @@ typedef  struct
 	u_short		wBitsPerSample ;
 	char		DataID [4] ;
 	u_long		nDataBytes ;
-} WAVE_HEADER ;
+} WAVE_HEADER;
 
 
-static void waveFormatCopy( WAVEFORMAT* wav, char *ptr );
-static char*  findchunk (char* s1, const char* s2, size_t n) ;
+static void waveFormatCopy(WAVEFORMAT* wav, char *ptr );
+static char* findchunk(char* s1, const char* s2, size_t n) ;
 
-static int  WaveHeaderCheck  (char *wave_buf,int* channels, u_long* samplerate, int* samplebits, u_long* samples,u_long* datastart)
-{	
-	static  WAVEFORMAT  waveformat ;
-	char*   ptr ;
-	u_long  databytes ;
+static int WaveHeaderCheck(char *wave_buf,int* channels, u_long* samplerate, int* samplebits, u_long* samples,u_long* datastart)
+{
+	static WAVEFORMAT waveformat;
+	char* ptr;
+	u_long databytes;
 
-	if (findchunk (wave_buf, "RIFF", BUFFERSIZE) != wave_buf) {
+	if (findchunk(wave_buf, "RIFF", BUFFERSIZE) != wave_buf) {
 		fprintf(stderr, "Bad format: Cannot find RIFF file marker");
 		return  WR_BADRIFF ;
 	}
 
-	if (! findchunk (wave_buf, "WAVE", BUFFERSIZE)) {
+	if (findchunk(wave_buf, "WAVE", BUFFERSIZE) == NULL) {
 		fprintf(stderr, "Bad format: Cannot find WAVE file marker");
 		return  WR_BADWAVE ;
 	}
 
-	ptr = findchunk (wave_buf, "fmt ", BUFFERSIZE) ;
+	ptr = findchunk(wave_buf, "fmt ", BUFFERSIZE) ;
 
-	if (! ptr) {
+	if (ptr == NULL) {
 		fprintf(stderr, "Bad format: Cannot find 'fmt' file marker");
 		return  WR_BADFORMAT ;
 	}
@@ -143,9 +144,9 @@ static int  WaveHeaderCheck  (char *wave
 		return  WR_NOTPCMFORMAT ;
 	}
 
-	ptr = findchunk (wave_buf, "data", BUFFERSIZE) ;
+	ptr = findchunk(wave_buf, "data", BUFFERSIZE) ;
 
-	if (! ptr) {
+	if (ptr == NULL) {
 		fprintf(stderr,"Bad format: unable to find 'data' file marker");
 		return  WR_NODATACHUNK ;
 	}
@@ -172,30 +173,30 @@ static int  WaveHeaderCheck  (char *wave
 		return  WR_BADFORMATDATA ;
 	}
 
-	return  0 ;
-} ; /* WaveHeaderCheck*/
+	return 0;
+} /* WaveHeaderCheck*/
 
 
-static char* findchunk  (char* pstart, const char* fourcc, size_t n)
-{	char	*pend ;
-	int		k, test ;
+static char* findchunk(char* pstart, const char* fourcc, size_t n) {
+	char *pend;
+	int k, test;
 
-	pend = pstart + n ;
+	pend = pstart + n;
 
 	while (pstart < pend)
 	{ 
-		if (*pstart == *fourcc)       /* found match for first char*/
-		{	test = 1 ;
-			for (k = 1 ; fourcc [k] != 0 ; k++)
-				test = (test ? ( pstart [k] == fourcc [k] ) : 0) ;
+		if (*pstart == *fourcc) {     /* found match for first char*/
+			test = 1 ;
+			for (k = 1 ; fourcc[k] != 0 ; k++)
+				test = (test ? ( pstart[k] == fourcc[k] ) : 0) ;
 			if (test)
 				return  pstart ;
-			} ; /* if*/
-		pstart ++ ;
-		} ; /* while lpstart*/
+		} ; /* if*/
+		pstart++;
+	} /* while lpstart*/
 
-	return  NULL ;
-} ; /* findchuck*/
+	return NULL;
+} /* findchuck*/
 
 static void waveFormatCopy( WAVEFORMAT* wav, char *ptr ) {
 	wav->dwSize           = LittleEndian_getDW( ptr,  0 );
@@ -219,9 +220,9 @@ static char* WavGetInfo(WAVFILE* wfile, 
 				  &channels,&samplerate,
 				  &sample_bits,&samples,&datastart) != 0 )) {
 		fprintf(stderr,"WavGetInfo(): Reading WAV header\n");
-		return 0;
+		return NULL;
 	}
-	
+
 	/*
 	 * Copy WAV data over to WAVFILE struct:
 	 */
@@ -238,11 +239,11 @@ static char* WavGetInfo(WAVFILE* wfile, 
 **	WAVFILE stream reader
 */
 
-#include<SDL_mixer.h>
+#include <SDL_mixer.h>
 WAVFILE::WAVFILE(void) {
-	wavinfo.SamplingRate=0;
-	wavinfo.Channels=1;
-	wavinfo.DataBits=0;
+	wavinfo.SamplingRate = 0;
+	wavinfo.Channels = 1;
+	wavinfo.DataBits = 0;
 }
 
 int WAVFILE_Stream::Read(char* in_buf, int blksize, int length) {
@@ -271,14 +272,16 @@ int WAVFILE_Stream::Read(char* in_buf, i
 	data_length = 0;
 	return blklen;
 }
+
 void WAVFILE_Stream::Seek(int count) {
         int blksize = 1;
         /* block size の設定 */
 	blksize *= wavinfo.Channels * (wavinfo.DataBits/8);
 	data_length = 0;
 	stream_length = stream_length_orig - stream_top - count*blksize;
-	fseek(stream, count*blksize+stream_top, 0);
+	fseek(stream, count*blksize+stream_top, SEEK_SET);
 }
+
 WAVFILE_Stream::WAVFILE_Stream(FILE* _stream, int _length) {
 	stream = _stream;
 	stream_length = _length;
@@ -300,13 +303,13 @@ WAVFILE_Stream::WAVFILE_Stream(FILE* _st
 	}
 	stream_top = data - data_orig;
 	data_length -= data - data_orig;
-	return;
 }
+
 WAVFILE_Stream::~WAVFILE_Stream() {
 	if (data_orig) delete data_orig;
 	if (stream) fclose(stream);
-	return;
 }
+
 /************************************************************:
 **
 **	WAVE format converter with SDL_audio
@@ -338,6 +341,7 @@ WAVFILE* WAVFILE::MakeConverter(WAVFILE*
 	WAVFILE_Converter* conv = new WAVFILE_Converter(new_reader, cvt);
 	return conv;
 }
+
 WAVFILE_Converter::WAVFILE_Converter(WAVFILE* _orig, SDL_AudioCVT* _cvt) {
 	original = _orig;
 	cvt = _cvt;
@@ -347,21 +351,22 @@ WAVFILE_Converter::WAVFILE_Converter(WAV
 	cvt->len = 0;
 	tmpbuf = new char[datasize*cvt->len_mult + 1024];
 	memset(tmpbuf, 0, datasize*cvt->len_mult+1024);
-};
+}
 
 static int conv_wave_rate(short* in_buf, int length, int in_rate, int out_rate, char* tmpbuf);
 WAVFILE_Converter::~WAVFILE_Converter() {
-	if (cvt) {
+	if (cvt != NULL) {
 		if (cvt->buf) delete[] cvt->buf;
 		delete cvt;
-		cvt = 0;
+		cvt = NULL;
 	}
 	delete[] tmpbuf;
 	if (original) delete original;
-	original = 0;
+	original = NULL;
 }
+
 int WAVFILE_Converter::Read(char* buf, int blksize, int blklen) {
-	if (original == 0 || cvt == 0) return -1;
+	if (original == NULL || cvt == NULL) return -1;
 	int copied_length = 0;
 	if (cvt->len < blksize*blklen) {
 		memcpy(buf, cvt->buf, cvt->len);
@@ -406,7 +411,7 @@ static int conv_wave_rate(short* in_buf,
 	double input_rate_d = input_rate, output_rate_d = output_rate;
 	double dtime; int outlen; short* out, * out_orig; int next_sample1, next_sample2;
 	short* in_buf_orig = in_buf;
-	int i; int time;
+	int i, time;
 
 	if (input_rate == output_rate) return length;
 	if (length <= 0) return 0;
@@ -436,10 +441,10 @@ static int conv_wave_rate(short* in_buf,
 	}
 	dtime -= input_rate_d*outlen; /* 次の prev_time */
 
-	time=0;
+	time = 0;
 	next_sample1 = short(read_little_endian_short((char*)(in_buf++)));
 	next_sample2 = short(read_little_endian_short((char*)(in_buf++)));
-	for (i=0; i<outlen; i++) {
+	for (i=0; i < outlen; i++) {
 		/* double で計算してみたけどそう簡単には高速化は無理らしい */
 		/* なお、変換は 1分のデータに1秒程度かかる(Celeron 700MHz) */
 		time += input_rate;
@@ -480,7 +485,7 @@ int WAVFILE::format = MIX_DEFAULT_FORMAT
 
 #if HAVE_LIBMAD
 
-#include<mad.h>
+#include <mad.h>
 #define MPEG_BUFSZ       40000   /* 2.5 s at 128 kbps; 1 s at 320 kbps */
 struct MP3FILE_impl {
 	enum { PREPARE, RUN, WRITE, DONE} status;
@@ -506,7 +511,7 @@ MP3FILE_impl::MP3FILE_impl(FILE* _stream
 	data = new char[MPEG_BUFSZ];
 	data_len = 0;
 	src_pointer = 0;
-	write_data = 0;
+	write_data = NULL;
 	write_data_len = 0;
 	write_pointer = 0;
 
@@ -529,7 +534,6 @@ MP3FILE_impl::~MP3FILE_impl() {
 	free(decoder.sync);
 	mad_decoder_finish(&decoder);
 	delete[] data;
-	return;
 }
 
 void MP3FILE_impl::run(void) {
@@ -639,6 +643,7 @@ enum mad_flow MP3FILE_impl::callback_err
 	/* return MAD_FLOW_BREAK here to stop decoding (and propagate an error) */
 	return MAD_FLOW_CONTINUE;
 }
+
 signed int scale(mad_fixed_t sample)
 {
 	/* round */
@@ -653,11 +658,13 @@ signed int scale(mad_fixed_t sample)
 	/* quantize */
 	return sample >> (MAD_F_FRACBITS + 1 - 16);
 }
+
 enum mad_flow MP3FILE_impl::callback_write(void *data, struct mad_header const *header, struct mad_pcm *pcm)
 {
 	MP3FILE_impl* pimpl = (MP3FILE_impl*)data;
 	return pimpl->callback_write_impl(pcm);
 }
+
 enum mad_flow MP3FILE_impl::callback_write_impl(struct mad_pcm *pcm)
 {
 	if (write_data_len == 0) return MAD_FLOW_IGNORE;
@@ -671,7 +678,7 @@ enum mad_flow MP3FILE_impl::callback_wri
 	}
 	write_data_len &= ~(nchannels*2-1);	/* write_data_len はあらかじめ丸めておく */
 	src_pointer += nsamples;
-	if (write_data == 0) { // skip data write
+	if (write_data == NULL) { // skip data write
 		write_pointer += nsamples*2*2;
 	} else while(nsamples--) {
 		signed int sample = scale(*left_ch++);
@@ -691,7 +698,7 @@ MP3FILE::MP3FILE(FILE* stream, int len) 
 	pimpl = new MP3FILE_impl(stream);
 	if (pimpl->status == MP3FILE_impl::DONE) {
 		delete pimpl;
-		pimpl = 0;
+		pimpl = NULL;
 		fclose(stream);
 		return;
 	}
@@ -699,16 +706,18 @@ MP3FILE::MP3FILE(FILE* stream, int len) 
 	wavinfo.Channels = 2;
 	wavinfo.DataBits = 16;
 }
+
 MP3FILE::~MP3FILE() {
 	if (pimpl) {
 		FILE* s = pimpl->stream;
 		delete pimpl;
 		fclose(s);
 	}
-	pimpl = 0;
+	pimpl = NULL;
 }
+
 int MP3FILE::Read(char* buf, int blksize, int blklen) {
-	if (pimpl == 0) return -1;
+	if (pimpl == NULL) return -1;
 	pimpl->write_data = buf;
 	pimpl->write_data_len = blksize*blklen;
 	pimpl->write_pointer = 0;
@@ -720,17 +729,17 @@ int MP3FILE::Read(char* buf, int blksize
 void MP3FILE::Seek(int count) {
 	FILE* stream = pimpl->stream;
 	delete pimpl;
-	fseek(stream,0,0);
+	fseek(stream, 0, SEEK_SET);
 	pimpl = new MP3FILE_impl(stream);
 	if (pimpl->status == MP3FILE_impl::DONE) {
 		delete pimpl;
-		pimpl = 0;
+		pimpl = NULL;
 		fclose(stream);
 		return;
 	}
 	int blksize = 1;
 	blksize *= wavinfo.Channels * (wavinfo.DataBits/8);
-	pimpl->write_data = 0;
+	pimpl->write_data = NULL;
 	pimpl->write_data_len = count * blksize;
 	pimpl->write_pointer = 0;
 	do {
@@ -738,8 +747,9 @@ void MP3FILE::Seek(int count) {
 	} while(pimpl->status != MP3FILE_impl::DONE && pimpl->write_pointer < pimpl->write_data_len);
 	return;
 }
+
 #elif USE_SMPEG
-#include<smpeg/smpeg.h>
+#include <smpeg/smpeg.h>
 
 struct MP3FILE_impl {
 	SMPEG* info;
@@ -751,7 +761,7 @@ MP3FILE_impl::MP3FILE_impl(FILE* _stream
 	stream = _stream;
 	info = SMPEG_new_descr(fileno(stream), NULL, 0);
 fprintf(stderr,"mp3 %p\n",info);
-	if (info && SMPEG_error(info) ) info = 0;
+	if (info != NULL && SMPEG_error(info) ) info = NULL;
 	SMPEG_enableaudio(info, 0);
 	SMPEG_enableaudio(info, 1);
 	SMPEG_play(info);
@@ -759,7 +769,7 @@ fprintf(stderr,"mp3 %p\n",info);
 
 MP3FILE::MP3FILE(FILE* stream, int len) {
 	pimpl = new MP3FILE_impl(stream);
-	if (pimpl->info == 0) {
+	if (pimpl->info == NULL) {
 		delete pimpl;
 		fclose(stream);
 		return;
@@ -770,6 +780,7 @@ MP3FILE::MP3FILE(FILE* stream, int len) 
 	wavinfo.Channels = fmt.channels;
 	wavinfo.DataBits = (fmt.format == AUDIO_S8) ? 8:16;
 }
+
 MP3FILE::~MP3FILE() {
 	if (pimpl && pimpl->info) {
 		if (SMPEG_status(pimpl->info) == SMPEG_PLAYING) SMPEG_stop(pimpl->info);
@@ -778,19 +789,21 @@ MP3FILE::~MP3FILE() {
 	if (pimpl) {
 		fclose(pimpl->stream);
 		delete pimpl;
-		pimpl = 0;
+		pimpl = NULL;
 	}
 }
+
 int MP3FILE::Read(char* buf, int blksize, int blklen) {
-	if (pimpl == 0 || pimpl->info == 0) return -1;
+	if (pimpl == NULL || pimpl->info == NULL) return -1;
 	int r = SMPEG_playAudio(pimpl->info, (Uint8*)buf, blksize*blklen);
 	if (r <= 0) { // end of file
 		return -1;
 	}
 	return r / blksize;
 }
+
 void MP3FILE::Seek(int count) {
-	if (pimpl == 0 || pimpl->info == 0) return;
+	if (pimpl == NULL || pimpl->info == NULL) return;
 	SMPEG_stop(pimpl->info);
 	SMPEG_rewind(pimpl->info);
 	SMPEG_play(pimpl->info);
@@ -801,8 +814,9 @@ void MP3FILE::Seek(int count) {
 	delete[] d;
 	return;
 }
+
 #else /* SMPEG */
-MP3FILE::MP3FILE(FILE* stream, int len) {pimpl = 0;}
+MP3FILE::MP3FILE(FILE* stream, int len) {pimpl = NULL;}
 MP3FILE::~MP3FILE(){}
 void MP3FILE::Seek(int count){}
 int MP3FILE::Read(char* buf, int blksize, int blklen){return -1;}