aboutsummaryrefslogtreecommitdiff
path: root/Src/Plugins/Input/in_cdda/CDText.cpp
blob: 21518a8083c0ab7e00c87600eaac00cbe8cecbd7 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
#include "main.h"
#include "cddb.h"
#include "api__in_cdda.h"
#ifndef IGNORE_API_GRACENOTE
#include "../primo/obj_primo.h"
#endif
//#include "CDPlay.h"
#include "DAEPlay.h"
#include "../nu/ns_wc.h"
#include "../nde/ndestring.h"

#ifndef IGNORE_API_GRACENOTE
const char *ReadLine(const char *input, char *output, size_t size, int codepage)
{
	size--; // leave room for null terminator
	while (input && *input && *input != '\r' && size)
	{
		char *next = CharNextExA(codepage, input, 0);
		while (input != next)
		{
			if (size)
			{
				*output = *input;
				output++;
				*output = 0; // safe because we left room for the null terminator
				size--;
			}
			input++;
		}
	}


	if (*input == '\r')
		input++;
	if (*input == '\n')
		input++;

	return input;
}


static bool CDText_Process(DINFO *ps, const char *title, const char *performers, const char *composers, int codepage)
{
	char thisTitle[1024] = {0};

	const char *titles = title;
	// first, get disc title
	thisTitle[0] = 0;
	titles = ReadLine(titles, thisTitle, 1024, codepage);

	if (thisTitle[0])
	{
		ndestring_release(ps->title);
		int count = MultiByteToWideChar(codepage, 0, thisTitle, -1, 0, 0);
		ps->title = ndestring_malloc(count*sizeof(wchar_t));
		MultiByteToWideChar(codepage, 0, thisTitle, -1, ps->title, count);
	}

	// now get track titles
	int trackNum = 0;
	while (titles && *titles)
	{
		if (trackNum == ps->ntracks)
			break;
		TRACKINFO &trackInfo = ps->tracks[trackNum];
		thisTitle[0] = 0;
		titles = ReadLine(titles, thisTitle, 1024, codepage);

		if (thisTitle[0])
		{
			ndestring_release(trackInfo.title);
			int count = MultiByteToWideChar(codepage, 0, thisTitle, -1, 0, 0);
			trackInfo.title = ndestring_malloc(count*sizeof(wchar_t));
			MultiByteToWideChar(codepage, 0, thisTitle, -1, trackInfo.title, count);
		}

		trackNum++;
	}

	titles = performers;
	// now get disc artist
	thisTitle[0] = 0;
	titles = ReadLine(titles, thisTitle, 1024, codepage);

	if (thisTitle[0])
	{
		ndestring_release(ps->artist);
		int count = MultiByteToWideChar(codepage, 0, thisTitle, -1, 0, 0);
		ps->artist = ndestring_malloc(count*sizeof(wchar_t));
		MultiByteToWideChar(codepage, 0, thisTitle, -1, ps->artist, count);
	}

	// now get track artists
	trackNum = 0;
	while (titles && *titles)
	{
		if (trackNum == ps->ntracks)
			break;
		TRACKINFO &trackInfo = ps->tracks[trackNum];

		thisTitle[0] = 0;
		titles = ReadLine(titles, thisTitle, 1024, codepage);

		if (thisTitle[0])
		{
			ndestring_release(trackInfo.artist);
			int count = MultiByteToWideChar(codepage, 0, thisTitle, -1, 0, 0);
			trackInfo.artist = ndestring_malloc(count*sizeof(wchar_t));
			MultiByteToWideChar(codepage, 0, thisTitle, -1, trackInfo.artist, count);
		}

		trackNum++;
	}

	titles = composers;
	// now get disc composer
	thisTitle[0] = 0;
	titles = ReadLine(titles, thisTitle, 1024, codepage);

	if (thisTitle[0])
	{
		ndestring_release(ps->composer);
		int count = MultiByteToWideChar(codepage, 0, thisTitle, -1, 0, 0);
		ps->composer = ndestring_malloc(count*sizeof(wchar_t));
		MultiByteToWideChar(codepage, 0, thisTitle, -1, ps->composer, count);
	}

	// now get track composers
	trackNum = 0;
	while (titles && *titles)
	{
		if (trackNum == ps->ntracks)
			break;
		TRACKINFO &trackInfo = ps->tracks[trackNum];

		thisTitle[0] = 0;
		titles = ReadLine(titles, thisTitle, 1024, codepage);

		if (thisTitle[0])
		{
			ndestring_release(trackInfo.composer);
			int count = MultiByteToWideChar(codepage, 0, thisTitle, -1, 0, 0);
			trackInfo.composer = ndestring_malloc(count*sizeof(wchar_t));
			MultiByteToWideChar(codepage, 0, thisTitle, -1, trackInfo.composer, count);
		}

		trackNum++;
	}

	ps->populated = true;
	return true;
}

bool DoCDText(DINFO *ps, char device)
{
	if (!device)
		return false;

	if (config_use_veritas) 
	{
		obj_primo *primo=0;
		waServiceFactory *sf = line.service->service_getServiceByGuid(obj_primo::getServiceGuid());
		if (sf) primo = reinterpret_cast<obj_primo *>(sf->getInterface());

		if (!primo)
			return false;

		DWORD unit = device;
		DWORD tracks;
		if (primo->DiscInfoEx(&unit, 0, NULL, NULL, NULL, &tracks, NULL, NULL) != PRIMOSDK_OK) // CDTextInfoEJ suggest that this needs to be called first
		{
			sf->releaseInterface(primo);
			return false;
		}
		if (ps->ntracks == 0) // go ahead and set if it's not set yet
			ps->ntracks = tracks;
		char titleE[8192] = "", performerE[8192] = "", composerE[8192] = "", titleJ[2000] = "", performerJ[2000] = "", composerJ[2000] = "";
		if (primo->CDTextInfoEJ(&unit, (PBYTE)titleE, (PBYTE)performerE, (PBYTE)composerE, (PBYTE)titleJ, (PBYTE)performerJ, (PBYTE)composerJ) == PRIMOSDK_OK)
		{
			sf->releaseInterface(primo);
			// read titles
			if (titleE[0])
				return CDText_Process(ps, titleE, performerE, composerE, 28591);
			else
				return CDText_Process(ps, titleJ, performerJ, composerJ, 932);
		}
	}

	return false;
}
#else
bool DoCDText(DINFO *ps, wchar_t device)
{
	if (!device)
		return false;

	DAEPlay *dae = (g_cdplay && g_cdplay == daePlayer && g_cdplay->g_drive == device ? daePlayer : new DAEPlay);
	if (dae)
	{
		if (dae != daePlayer)
		{
			if (dae->open(device, 1))
			{
				delete(dae);
				return false;
			}
		}

		DAEPlay::CDTextArray* cd_text = dae->getCDText();
		if ((int)cd_text > 0)
		{
			if (ps)
			{
				ps->ntracks = cd_text->size() - 1;

				ndestring_release(ps->title);
				ps->title = ndestring_wcsdup(cd_text[DAEPlay::CD_TEXT_TITLE][0]);

				ndestring_release(ps->artist);
				ps->artist = ndestring_wcsdup(cd_text[DAEPlay::CD_TEXT_PERFORMER][0]);

				// TODO match this to the supported list of genres...
				/*ndestring_release(ps->genre);
				ps->genre = ndestring_wcsdup(cd_text[DAEPlay::CD_TEXT_GENRE][0]);*/

				ndestring_release(ps->composer);
				ps->composer = ndestring_wcsdup(cd_text[DAEPlay::CD_TEXT_COMPOSER][0]);

				for (size_t i = 1; i < cd_text->size(); i++)
				{
					if (i == ps->ntracks + 1)
						break;

					TRACKINFO &trackInfo = ps->tracks[i-1];

					// TODO improve error handling
					ndestring_release(trackInfo.artist);
					trackInfo.artist = ndestring_wcsdup(cd_text[DAEPlay::CD_TEXT_PERFORMER][i]);
					//free(cd_text[DAEPlay::CD_TEXT_PERFORMER][i]);

					ndestring_release(trackInfo.title);
					trackInfo.title = ndestring_wcsdup(cd_text[DAEPlay::CD_TEXT_TITLE][i]);
					//free(cd_text[DAEPlay::CD_TEXT_TITLE][i]);

					ndestring_release(trackInfo.genre);
					trackInfo.genre = ndestring_wcsdup(cd_text[DAEPlay::CD_TEXT_GENRE][i]);
					//free(cd_text[DAEPlay::CD_TEXT_GENRE][i]);

					ndestring_release(trackInfo.composer);
					trackInfo.composer = ndestring_wcsdup(cd_text[DAEPlay::CD_TEXT_COMPOSER][i]);
					//free(cd_text[DAEPlay::CD_TEXT_COMPOSER][i]);
				}

				ps->populated = true;
			}

			if (dae != daePlayer) delete(dae);
			return true;
		}
		if (dae != daePlayer) delete(dae);
	}

	return false;
}
#endif