buffered_reader_t *buffered_reader_open(const char *path, int32_t buffer_size, int32_t seek_mode)
{
	buffered_reader_t *reader = malloc(sizeof(buffered_reader_t));
	long long result;

	if (reader == 0)
		return 0;

	memset(reader, 0, sizeof(buffered_reader_t));
	reader->cache_enabled = 1;
	reader->handle = -1;
	reader->buffer_size = buffer_size;
	reader->seek_mode = seek_mode;

	reader->buffer_0 = malloc_64(reader->buffer_size);
	if (reader->buffer_0 == 0) {
		buffered_reader_close(reader);
		return 0;
	}

	reader->buffer_1 = malloc_64(reader->buffer_size);
	if (reader->buffer_1 == 0) {
		buffered_reader_close(reader);
		return 0;
	}

	reader->buffer_2 = malloc_64(reader->buffer_size);
	if (reader->buffer_2 == 0) {
		buffered_reader_close(reader);
		return 0;
	}

	reader->handle = sceIoOpen(path, PSP_O_RDONLY, 0777);

	if (reader->handle < 0) {
		buffered_reader_close(reader);
		return 0;
	}
	if (sceIoChangeAsyncPriority(reader->handle, 0x10) < 0) {
		buffered_reader_close(reader);
		return 0;
	}

	reader->length = sceIoLseek32(reader->handle, 0, PSP_SEEK_END);

	reader->first_buffer = reader->buffer_0;
	reader->second_buffer = reader->buffer_1;
	reader->third_buffer = reader->buffer_2;

	sceIoLseek32(reader->handle, reader->position_0, PSP_SEEK_SET);
	sceIoReadAsync(reader->handle, reader->first_buffer, reader->position_1 - reader->position_0);
	sceIoWaitAsync(reader->handle, &result);
	sceIoLseek32(reader->handle, reader->position_1, PSP_SEEK_SET);
	sceIoReadAsync(reader->handle, reader->second_buffer, reader->position_2 - reader->position_1);
	sceIoWaitAsync(reader->handle, &result);
	sceIoLseek32(reader->handle, reader->position_2, PSP_SEEK_SET);
	sceIoReadAsync(reader->handle, reader->third_buffer, reader->position_3 - reader->position_2);
	return reader;
}
Exemple #2
0
/**
 * 停止MP3音乐文件的播放,销毁所占有的线程、资源等
 *
 * @note 不可以在播放线程中调用,必须能够多次重复调用而不死机
 *
 * @return 成功时返回0
 */
static int mp3_end(void)
{
	dbg_printf(d, "%s", __func__);

	__end();

	xAudioEnd();

	g_status = ST_STOPPED;

	mad_stream_finish(&stream);
	mad_synth_finish(&synth);
	mad_frame_finish(&frame);

	if (use_me) {
		if (mp3_getEDRAM)
			xrAudiocodecReleaseEDRAM(mp3_codec_buffer);
	}

	free_mp3_info(&mp3info);
	free_bitrate(&g_inst_br);

	if (mp3_data.use_buffer) {
		if (mp3_data.r != NULL) {
			buffered_reader_close(mp3_data.r);
			mp3_data.r = NULL;
		}
	} else {
		if (mp3_data.fd >= 0) {
			xrIoClose(mp3_data.fd);
			mp3_data.fd = -1;
		}
	}

	if (g_buff != NULL) {
		xAudioFree(g_buff);
		g_buff = NULL;
	}

	generic_end();

	return 0;
}