music_flac.c
changeset 521 565549e046b0
parent 518 8bc9b5fd2aae
child 522 a70c0ee38c9f
equal deleted inserted replaced
520:61a1516bc06a 521:565549e046b0
    58 	rw = SDL_RWFromFile (file, "rb");
    58 	rw = SDL_RWFromFile (file, "rb");
    59 	if (rw == NULL) {
    59 	if (rw == NULL) {
    60 		SDL_SetError ("Couldn't open %s", file);
    60 		SDL_SetError ("Couldn't open %s", file);
    61 		return NULL;
    61 		return NULL;
    62 	}
    62 	}
    63 	return FLAC_new_RW (rw);
    63 	return FLAC_new_RW (rw, 1);
    64 }
    64 }
    65 
    65 
    66 static FLAC__StreamDecoderReadStatus flac_read_music_cb(
    66 static FLAC__StreamDecoderReadStatus flac_read_music_cb(
    67 									const FLAC__StreamDecoder *decoder,
    67 									const FLAC__StreamDecoder *decoder,
    68 									FLAC__byte buffer[],
    68 									FLAC__byte buffer[],
   311 		break;
   311 		break;
   312 	}
   312 	}
   313 }
   313 }
   314 
   314 
   315 /* Load an FLAC stream from an SDL_RWops object */
   315 /* Load an FLAC stream from an SDL_RWops object */
   316 FLAC_music *FLAC_new_RW(SDL_RWops *rw)
   316 FLAC_music *FLAC_new_RW(SDL_RWops *rw, int freerw)
   317 {
   317 {
   318 	FLAC_music *music;
   318 	FLAC_music *music;
   319 	int init_stage = 0;
   319 	int init_stage = 0;
   320 	int was_error = 1;
   320 	int was_error = 1;
   321 
   321 
   325 		memset (music, 0, (sizeof (*music)));
   325 		memset (music, 0, (sizeof (*music)));
   326 		FLAC_stop (music);
   326 		FLAC_stop (music);
   327 		FLAC_setvolume (music, MIX_MAX_VOLUME);
   327 		FLAC_setvolume (music, MIX_MAX_VOLUME);
   328 		music->section = -1;
   328 		music->section = -1;
   329 		music->rwops = rw;
   329 		music->rwops = rw;
       
   330 		music->freerw = freerw;
   330 		music->flac_data.max_to_read = 0;
   331 		music->flac_data.max_to_read = 0;
   331 		music->flac_data.overflow = NULL;
   332 		music->flac_data.overflow = NULL;
   332 		music->flac_data.overflow_len = 0;
   333 		music->flac_data.overflow_len = 0;
   333 		music->flac_data.overflow_read = 0;
   334 		music->flac_data.overflow_read = 0;
   334 		music->flac_data.data = NULL;
   335 		music->flac_data.data = NULL;
   373 				case 2:
   374 				case 2:
   374 					flac.FLAC__stream_decoder_delete( music->flac_decoder );
   375 					flac.FLAC__stream_decoder_delete( music->flac_decoder );
   375 				case 1:
   376 				case 1:
   376 				case 0:
   377 				case 0:
   377 					free(music);
   378 					free(music);
   378 					SDL_RWclose(rw);
   379 					if (freerw) {
       
   380 						SDL_RWclose(rw);
       
   381 					}
   379 					break;
   382 					break;
   380 			}
   383 			}
   381 			return NULL;
   384 			return NULL;
   382 		}
   385 		}
   383 	}
   386 	} else {
   384 	else {
       
   385 		SDL_OutOfMemory();
   387 		SDL_OutOfMemory();
       
   388 		if (freerw) {
       
   389 			SDL_RWclose(rw);
       
   390 		}
       
   391 		return NULL;
   386 	}
   392 	}
   387 
   393 
   388 	return music;
   394 	return music;
   389 }
   395 }
   390 
   396 
   554 
   560 
   555 		if (music->cvt.buf) {
   561 		if (music->cvt.buf) {
   556 			free (music->cvt.buf);
   562 			free (music->cvt.buf);
   557 		}
   563 		}
   558 
   564 
       
   565 		if (music->freerw) {
       
   566 			SDL_RWclose(music->rwops);
       
   567 		}
   559 		free (music);
   568 		free (music);
   560 	}
   569 	}
   561 }
   570 }
   562 
   571 
   563 /* Jump (seek) to a given position (time is in seconds) */
   572 /* Jump (seek) to a given position (time is in seconds) */