File: mac_engine.cpp

package info (click to toggle)
alsaplayer 0.99.82-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 5,892 kB
  • sloc: ansic: 40,741; cpp: 14,400; makefile: 983; sh: 796; lex: 751; asm: 45; python: 29; sed: 16
file content (244 lines) | stat: -rw-r--r-- 5,882 bytes parent folder | download | duplicates (2)
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
/*
 *  mac_engine.cpp (C) 2005-2007 by Peter Lemenkov <lemenkov@gmail.com>
 *
 *   This program is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation; either version 2 of the License, or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program; if not, write to the Free Software
 *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 */

#include <string.h> /* memset */

#ifdef __BIG_ENDIAN
#include <unistd.h> /* swab */
#endif

#include <alsaplayer/alsaplayer_error.h>
#include <alsaplayer/input_plugin.h>
#include <alsaplayer/reader.h>

#include <mac/All.h>
#include <mac/MACLib.h>
//#include <mac/APETag.h>
#include <mac/CharacterHelper.h>

//#include <taglib/tag_c.h>

#define BLOCK_SIZE 4096	/* We can use any block size we like */

static int ape_init(void) 
{
	return 1;
}

static void ape_shutdown(void)
{
	return;
}

static float ape_can_handle(const char *path)
{
	int ret = 0;
	wchar_t* pUTF16 = GetUTF16FromANSI (path);
	IAPEDecompress* temp = CreateIAPEDecompress(pUTF16, &ret);
	delete temp;
	free (pUTF16);
	if(ret == ERROR_SUCCESS)
		return 1.0;

	return 0.0;
}


static int ape_open(input_object *obj, const char *path)
{
	int ret = 0;

	void* datasource = NULL;
	
	if (!obj)
		return 0;
	
	if ((datasource = reader_open(path, NULL, NULL)) == NULL) {
		return 0;
	}

	obj->flags = 0;
	if (reader_seekable ((reader_type*)datasource)) {
		obj->flags |= P_SEEK;
		obj->flags |= P_PERFECTSEEK;
		obj->flags |= P_FILEBASED;
	} 
	else 
		obj->flags |= P_STREAMBASED;
	
	wchar_t* pUTF16 = GetUTF16FromANSI (path);
	obj->local_data = CreateIAPEDecompress(pUTF16, &ret);
	free (pUTF16);
	
	obj->nr_channels = ((IAPEDecompress*) obj->local_data)->GetInfo(APE_INFO_CHANNELS);
	obj->nr_tracks   = 1;
	obj->frame_size = BLOCK_SIZE;

	return 1;
}

static void ape_close(input_object *obj)
{
	if (!obj || !(obj->local_data))
		return;
	
	free (obj->local_data);
	obj->local_data = NULL;
}

static long ape_frame_to_sec (input_object *obj, int frame)
{
	if (!obj || !(obj->local_data))
		return 0;

	return (frame * obj->frame_size) / \
	      (((IAPEDecompress*) obj->local_data)->GetInfo(APE_INFO_SAMPLE_RATE) * \
	       obj->nr_channels * \
	       ((IAPEDecompress*) obj->local_data)->GetInfo(APE_INFO_BYTES_PER_SAMPLE) / 100);
}

static int ape_sample_rate(input_object *obj)
{
	if (!obj || !(obj->local_data))
		return 0;

	return ((IAPEDecompress*) obj->local_data)->GetInfo(APE_INFO_SAMPLE_RATE);
}

static int ape_channels(input_object *obj)
{
	if (!obj || !(obj->local_data))
		return 0;

	return obj->nr_channels;
}

static int ape_stream_info (input_object *obj, stream_info *info)
{
	if (!obj || !(obj->local_data) || !info)
		return 0;

//	CAPETag *tag = (CAPETag *)((IAPEDecompress*) obj->local_data)->GetInfo(APE_INFO_TAG);
//	TagLib_File* tag_file = taglib_file_new(const char *filename);

	sprintf(info->stream_type, "APE version %.2f", float (((IAPEDecompress*) obj->local_data)->GetInfo(APE_INFO_FILE_VERSION)) / 1000);
	
	strcpy(info->status, "playing...");
	strcpy(info->artist, "Artist");
	strcpy(info->title, "Title");
	strcpy(info->album, "Album");
	strcpy(info->genre, "Genre");
	strcpy(info->year, "Year");
	strcpy(info->track, "Tracknum");
	strcpy(info->comment, "Comment");
	strcpy(info->path, obj->path);

	info->channels = obj->nr_channels;
	info->tracks = 1; // number of tracks
	info->current_track = 1; // number of current track
	info->sample_rate = ((IAPEDecompress*) obj->local_data)->GetInfo(APE_INFO_SAMPLE_RATE);
	info->bitrate = ((IAPEDecompress*) obj->local_data)->GetInfo(APE_INFO_AVERAGE_BITRATE);

//	printf ("ape: artist[%s], title[%s], path[%s]\n", info->artist, info->title, info->path);

	return 1;
}

static int ape_nr_frames(input_object *obj)
{
	if (!obj || !(obj->local_data))
		return 0;
	
	return (((IAPEDecompress*) obj->local_data)->GetInfo(APE_INFO_WAV_DATA_BYTES) / obj->frame_size);
}

static int ape_frame_size(input_object *obj)
{
	if (!obj || !(obj->local_data))
		return 0;
	
	return obj->frame_size;
}

static int ape_frame_seek(input_object *obj, int frame)
{
	int64_t pos;

	if (!obj || !(obj->local_data))
		return 0;

	if (obj->flags & P_STREAMBASED)
		return 0;
	
	((IAPEDecompress*) obj->local_data)->Seek(frame * obj->frame_size / 4); 
	return frame;
}

static int ape_play_frame (input_object *obj, char *buf)
{
	int nRead = 0;
	int i;

	if (!obj || !(obj->local_data))
		return 0;

	((IAPEDecompress*) obj->local_data)->GetData (buf, 1024, &nRead);
#ifdef __BIG_ENDIAN__
	swab(buf, buf, (nRead * 4));
#endif
	if (nRead != 0)
		return 1;

	return 0;
}

static input_plugin ape_plugin;

#ifdef __cplusplus
extern "C" {
#endif

input_plugin *input_plugin_info (void)
{
	memset(&ape_plugin, 0, sizeof(input_plugin));

	ape_plugin.version 	= INPUT_PLUGIN_VERSION;
	ape_plugin.name 	= "Monkey's Audio plugin ver. 0.0.0.7";
	ape_plugin.author 	= "Peter Lemenkov";
	ape_plugin.init 	= ape_init; 
	ape_plugin.shutdown 	= ape_shutdown;
	ape_plugin.can_handle 	= ape_can_handle;
	ape_plugin.open 	= ape_open; 
	ape_plugin.close 	= ape_close;
	ape_plugin.play_frame 	= ape_play_frame; 
	ape_plugin.frame_seek 	= ape_frame_seek;
	ape_plugin.frame_size 	= ape_frame_size;
	ape_plugin.nr_frames 	= ape_nr_frames; 
	ape_plugin.frame_to_sec = ape_frame_to_sec;
	ape_plugin.sample_rate 	= ape_sample_rate;
	ape_plugin.channels 	= ape_channels; 
	ape_plugin.stream_info 	= ape_stream_info; /* TODO */ 

	return &ape_plugin;
}

#ifdef __cplusplus
}
#endif