music_flac.c
changeset 524 0f6aa6179b1d
parent 522 a70c0ee38c9f
child 542 3de4970b36d4
equal deleted inserted replaced
523:3214c1b0f0f4 524:0f6aa6179b1d
   412 	SDL_AudioCVT *cvt;
   412 	SDL_AudioCVT *cvt;
   413 
   413 
   414 	/* GET AUDIO WAVE DATA */
   414 	/* GET AUDIO WAVE DATA */
   415 	// set the max number of characters to read
   415 	// set the max number of characters to read
   416 	music->flac_data.max_to_read = 8192;
   416 	music->flac_data.max_to_read = 8192;
   417 
       
   418 	// clear out the data buffer if it exists
       
   419 	if (music->flac_data.data) {
       
   420 		free (music->flac_data.data);
       
   421 	}
       
   422 
       
   423 	music->flac_data.data_len = music->flac_data.max_to_read;
   417 	music->flac_data.data_len = music->flac_data.max_to_read;
   424 	music->flac_data.data_read = 0;
   418 	music->flac_data.data_read = 0;
   425 	music->flac_data.data = (char *)malloc (music->flac_data.data_len);
   419 	if (!music->flac_data.data) {
       
   420 		music->flac_data.data = (char *)malloc (music->flac_data.data_len);
       
   421 	}
   426 
   422 
   427 	// we have data to read
   423 	// we have data to read
   428 	while(music->flac_data.max_to_read > 0) {
   424 	while(music->flac_data.max_to_read > 0) {
   429 		// first check if there is data in the overflow from before
   425 		// first check if there is data in the overflow from before
   430 		if (music->flac_data.overflow) {
   426 		if (music->flac_data.overflow) {
   432 
   428 
   433 			if (overflow_len > music->flac_data.max_to_read) {
   429 			if (overflow_len > music->flac_data.max_to_read) {
   434 				size_t overflow_extra_len = overflow_len -
   430 				size_t overflow_extra_len = overflow_len -
   435 												music->flac_data.max_to_read;
   431 												music->flac_data.max_to_read;
   436 
   432 
   437 				char* new_overflow = (char *)malloc (overflow_extra_len);
       
   438 				memcpy (music->flac_data.data+music->flac_data.data_read,
   433 				memcpy (music->flac_data.data+music->flac_data.data_read,
   439 					music->flac_data.overflow, music->flac_data.max_to_read);
   434 					music->flac_data.overflow, music->flac_data.max_to_read);
   440 				music->flac_data.data_read += music->flac_data.max_to_read;
   435 				music->flac_data.data_read += music->flac_data.max_to_read;
   441 				memcpy (new_overflow,
   436 				memcpy (music->flac_data.overflow,
   442 					music->flac_data.overflow + music->flac_data.max_to_read,
   437 					music->flac_data.overflow + music->flac_data.max_to_read,
   443 					overflow_extra_len);
   438 					overflow_extra_len);
   444 				free (music->flac_data.overflow);
       
   445 				music->flac_data.overflow = new_overflow;
       
   446 				music->flac_data.overflow_len = overflow_extra_len;
   439 				music->flac_data.overflow_len = overflow_extra_len;
   447 				music->flac_data.overflow_read = overflow_extra_len;
   440 				music->flac_data.overflow_read = overflow_extra_len;
   448 				music->flac_data.max_to_read = 0;
   441 				music->flac_data.max_to_read = 0;
   449 			}
   442 			}
   450 			else {
   443 			else {