[SDL] zziplib and WAV files (RWops) [WORKING METHOD]

Nicolás Varchavsky nicomv at gmail.com
Wed Jul 20 14:06:44 PDT 2005


Hi there, I've been looking on the web some info on how to use WAV
files with zziplib (with SDL_LoadWAV_RW() ).

If this solution has already been posted I'm sorry, I couldn't find it.

Problem: I can load textures with SDL_LoadBMP_RW() without problems
using zziplib, but when I use SDL_LoadWAV_RW() to load WAV files, I
get the "Error reading from datastream" error.

My solution: I know this is not 100% efficient but it works, and I can
load WAV files from a .zip file using zziplib and SDL_LoadWAV_RW().

Any inputs would be more than appreciated!


#define READ_PACKET 65536
int load_sound(const char *filename, SDL_AudioSpec *wav_spec, Uint8
**wav_buffer, Uint32 *wav_lenght)
{
	char *mem = NULL;
	char c[READ_PACKET];
	ZZIP_FILE *zf = NULL;
	int file_size = 0, real_read = 0;
	SDL_RWops *rw = NULL;

	// open zip file
	static zzip_strings_t ext[] = { ".pak", ".PAK", ".zip", "", 0 };
	zf = zzip_open_ext_io (filename, O_RDONLY|O_BINARY, 0, ext, 0);
	
	// read READ_PACKET packets
	file_size = 0;
	while (real_read = zzip_read(zf, c, sizeof(char) * READ_PACKET))
	{
		file_size += real_read;
		mem = (char *)realloc(mem, sizeof(char) * file_size);
		memmove(mem+file_size-real_read, c, real_read);
	}

	// close zip file
	zzip_close(zf);

	// create RWops from memory
	rw = SDL_RWFromMem(mem, file_size);

	// pass the created RWops to SDL_LoadWAV_RW()
	if (NULL == SDL_LoadWAV_RW(rw, 1, wav_spec, wav_buffer, wav_lenght))
	{
		printf("Audio - [Error] Can't open file %s: %s\n", filename, SDL_GetError());
		return false;
	}
	
	// free memory
	if (mem)
		free(mem);
	
	return true;
}



NOTES: I use READ_PACKET because for some reason I couldn't get the
size of the wav file I'm trying to open. Also, I could set READ_PACKET
to a huge value so I can assure that everytime I read I read the whole
file; but 65536 works for me now.

Also, this function returns all the data needed for the OpenAL library
(that's what I use), and that's why the function parameters are those.

Afterwards, with all the structures and variables filled up I use:

	ALenum format;
	if (AUDIO_U8 == wav_spec.format || AUDIO_S8 == wav_spec.format)
	{
		if (1 == wav_spec.channels)
			format = AL_FORMAT_MONO8;
		else
			format = AL_FORMAT_STEREO8;
	}
	else
	{
		if (1 == wav_spec.channels)
			format = AL_FORMAT_MONO16;
		else
			format = AL_FORMAT_STEREO16;
	}
	
	alBufferData(buffer, format, wav_buffer, wav_lenght, wav_spec.freq);





Excuse my bad english, I'm from Argentina.

Best regards,

Jehos.-




More information about the SDL mailing list