playlist.c 5.8 KB
Newer Older
1
2
3
#include <stdlib.h>
#include <boolean.h>
#include <string.h>
4
#include "file/file_path.h"
5

6
#include "playlist.h"
7
#include "log.h"
8

9
bool get_playlist(const char *path, playlist **dest_pl)
10
{
11
	//local variables
jdgleaver's avatar
jdgleaver committed
12
13
14
15
	int i,j;
	int position = 0;
	Music_Emu *temp_emu = NULL;
	gme_file_data *gfd = NULL;
16
	gme_err_t err_msg;
jdgleaver's avatar
jdgleaver committed
17
	playlist *pl = NULL;
18
	//init playlist
jdgleaver's avatar
jdgleaver committed
19
	pl = (playlist *)calloc(1, sizeof(playlist));
20
	//load libretro content file
21
	if(get_playlist_gme_files(path,&(pl->files),&(pl->num_files),&(pl->num_tracks)))
22
	{
jdgleaver's avatar
jdgleaver committed
23
		pl->tracks = (gme_track_data **)calloc(pl->num_tracks, sizeof(gme_track_data*));
24
		//read tracks
25
		for(i=0;i<pl->num_files;i++)
26
		{
27
			gfd = pl->files[i];
28
29
30
31
32
33
			temp_emu = gme_new_emu(gfd->file_type,gme_info_only);
			err_msg = gme_load_data(temp_emu,gfd->data,gfd->length);
			if(err_msg==NULL)
			{
				for(j=0;j<gfd->num_tracks;j++)
				{
34
					if(get_track_data(temp_emu,i,j,gfd->name,&(pl->tracks[position])))
35
36
37
					{
						position++;
					}
38
39
				}				
			}
40
			else
jdgleaver's avatar
jdgleaver committed
41
42
43
44
				goto error;

			gme_delete(temp_emu);
			temp_emu = NULL;
45
46
47
		}
	}
	else
jdgleaver's avatar
jdgleaver committed
48
49
		goto error;

50
	*dest_pl = pl;
51
	return true;
jdgleaver's avatar
jdgleaver committed
52
53
54
55
56
57
58
59
60

error:
	if (temp_emu)
		gme_delete(temp_emu);

	if (pl)
		cleanup_playlist(pl);

	return false;
61
62
}

63
bool get_playlist_gme_files(const char *path,gme_file_data ***dest_files,int *dest_num_files, int *dest_num_tracks)
64
65
{
	int i;
Libretro-Admin's avatar
Libretro-Admin committed
66
67
	bool success              = true;
	file_data **files         = NULL;
68
	gme_file_data **gme_files = NULL;
Libretro-Admin's avatar
Libretro-Admin committed
69
70
71
	int num_files             = 0;
	int num_tracks            = 0;

72
73
74
	if(get_file_data(path,&files,&num_files)) {
		gme_files = malloc(sizeof(gme_file_data*) * num_files);
		for(i=0;i< num_files;i++)
75
		{
76
77
			gme_files[i] = NULL;
			if(!get_gme_file_data(files[i],&(gme_files[i])))
78
			{
79
80
				success = false;
				break;
81
			}
82
			free(files[i]);
83
			if(gme_files[i]==NULL)
84
85
86
87
			{
				success = false;
				break;
			}
88
			num_tracks += gme_files[i]->num_tracks;
89
		}
90
		free(files);
91
92
	}
	else
93
		success = false;
94
95
96
	*dest_files = gme_files;
	*dest_num_files = num_files;
	*dest_num_tracks = num_tracks;
97
	return success;
98
99
}

100
bool get_gme_file_data(file_data *fd,gme_file_data **dest_gfd)
101
{
102
	Music_Emu* temp_emu;
103
	gme_err_t err_msg;
104
	char *ext;
105
106
	gme_file_data *gfd;
	gfd = malloc(sizeof(gme_file_data));
107
	//set playlist type
108
	ext = strrchr(fd->name,'.') +1;
109
	//check extension to determine player type
110
	if(strcmp(ext,"ay")==0 || strcmp(ext,"AY")==0)
111
		gfd->file_type = gme_ay_type;
112
	else if(strcmp(ext,"gbs")==0 || strcmp(ext,"GBS")==0)
113
		gfd->file_type = gme_gbs_type;
114
	else if(strcmp(ext,"gym")==0 || strcmp(ext,"GYM")==0)
115
		gfd->file_type = gme_gym_type;
116
	else if(strcmp(ext,"hes")==0 || strcmp(ext,"HES")==0)
117
		gfd->file_type = gme_hes_type;
118
	else if(strcmp(ext,"kss")==0 || strcmp(ext,"KSS")==0)
119
		gfd->file_type = gme_kss_type;
120
	else if(strcmp(ext,"nsf")==0 || strcmp(ext,"NSF")==0)
121
		gfd->file_type = gme_nsf_type;
122
	else if(strcmp(ext,"nsfe")==0 || strcmp(ext,"NSFE")==0)
123
		gfd->file_type = gme_nsfe_type;
124
	else if(strcmp(ext,"sap")==0 || strcmp(ext,"SAP")==0)
125
		gfd->file_type = gme_sap_type;
126
	else if(strcmp(ext,"spc") == 0 || strcmp(ext,"SPC")==0)
127
		gfd->file_type = gme_spc_type;
128
	else if(strcmp(ext,"vgm") == 0 || strcmp(ext,"VGM")==0)
129
		gfd->file_type = gme_vgm_type;
130
	else if(strcmp(ext,"vgz") == 0 || strcmp(ext,"VGZ")==0)
131
		gfd->file_type = gme_vgz_type;
132
133
	else
		return false;
134
	temp_emu = gme_new_emu(gfd->file_type,gme_info_only);
135
136
	err_msg = gme_load_data(temp_emu,fd->data,fd->length);
	if(err_msg==NULL)
137
		gfd->num_tracks = gme_track_count(temp_emu);
138
	else
139
	{
140
		handle_error(err_msg);
141
		return false;
142
	}
143
144
	gme_delete( temp_emu );
	//deep copy file data
145
	gfd->name = calloc(strlen(fd->name)+1,sizeof(char));
146
147
148
149
150
	strcpy(gfd->name,fd->name);
	gfd->data = malloc(fd->length * sizeof(char));
	memcpy(gfd->data,fd->data,fd->length);
	gfd->length = fd->length;
	*dest_gfd = gfd;
151
	return true;
152
153
}

154
bool get_track_data(Music_Emu* emu, int fileid, int trackid, char *filename,gme_track_data **dest_gtd)
155
156
{
	gme_info_t* ti;
157
158
159
160
	gme_track_data *gtd;
	gtd = malloc(sizeof(gme_track_data));
	gtd->file_id = fileid;
	gtd->track_id = trackid;
161
162
163
	gme_track_info(emu, &ti,trackid);
	//game name
	if(strcmp(ti->game,"")==0)
164
	{
165
166
		gtd->game_name = calloc(strlen(filename)+1,sizeof(char));
		strcpy(gtd->game_name,filename);
167
168
169
	}
	else
	{
170
171
		gtd->game_name = calloc(strlen(ti->game)+1,sizeof(char));
		strcpy(gtd->game_name,ti->game);
172
173
	}
	//track length
174
175
176
177
178
	gtd->track_length = ti->length;
	if ( gtd->track_length <= 0 )
		gtd->track_length = ti->intro_length + ti->loop_length * 2;
	if ( gtd->track_length <= 0 )
		gtd->track_length = (long) (2.5 * 60 * 1000);
179
180
181
	//track name
	if(strcmp(ti->song,"") == 0)
	{
182
183
		gtd->track_name = calloc(10,sizeof(char));
		sprintf(gtd->track_name, "Track %i",trackid+1);
184
	}
185
	else
186
	{
187
188
		gtd->track_name = calloc(strlen(ti->song)+1,sizeof(char));
		strcpy(gtd->track_name, ti->song);
189
	}
190
	gme_free_info(ti);
191
	*dest_gtd = gtd;
192
	return true;
193
}
194
195

bool cleanup_playlist(playlist *playlist)	
196
197
198
{
	int i;
	if(playlist->tracks!=NULL)
199
	{
200
201
202
203
204
205
206
207
208
209
210
211
		for(i=0;i<playlist->num_tracks;i++)
      	{
         	if(playlist->tracks[i] != NULL)
         	{
				if(playlist->tracks[i]->game_name != NULL)
	            	free(playlist->tracks[i]->game_name);
				if(playlist->tracks[i]->track_name != NULL)
	            	free(playlist->tracks[i]->track_name);
	         	free(playlist->tracks[i]);
         	}
      	}
      	free(playlist->tracks);
212
	}
213
	if(playlist->files!=NULL)
214
	{
215
216
217
218
219
220
221
222
223
224
225
226
		for(i=0;i<playlist->num_files;i++)
		{
			if(playlist->files[i] != NULL)
			{
				if(playlist->files[i]->data != NULL)
					free(playlist->files[i]->data);
				if(playlist->files[i]->name != NULL)
					free(playlist->files[i]->name);
				free(playlist->files[i]);
			}
		}
		free(playlist->files);
227
	}
228
	free(playlist);
229
	return true;
230
}