File: oggplayer.cc

package info (click to toggle)
mp3blaster 1:3.2.5-3
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd, squeeze, wheezy
  • size: 2,156 kB
  • ctags: 3,421
  • sloc: cpp: 18,610; ansic: 1,452; sh: 701; makefile: 91
file content (283 lines) | stat: -rw-r--r-- 5,366 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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
/* (C) Bram Avontuur, bram@mp3blaster.avontuur.org
 * For more information about OggVorbis, check http://www.xiph.org/ogg/vorbis/
 */
#include <mpegsound.h>
#include "mpegsound_locals.h"

#ifdef INCLUDE_OGG

#ifdef HAVE_BOOL_H
//#include <bool.h>
#endif
#include <vorbis/codec.h>
#include <vorbis/vorbisfile.h>
#include <stdio.h>
#include <string.h>

Oggplayer::Oggplayer(audiodriver_t driver)
{
	of = NULL;
	wordsize = 2; //2 bytes
#ifdef WORDS_BIGENDIAN
	bigendian = 1;
#else
	bigendian = 0;
#endif
	signeddata = 1;
	mono = 0;
	downfreq = 0;
	resetsound = 1;
	set_driver(driver);
}

Oggplayer::~Oggplayer()
{
	if (of)
	{
		ov_clear(of);
		delete of;
		of = NULL;
	}
}

bool Oggplayer::openfile(const char *filename, const char *device, soundtype write2file)
{
	FILE *f;
	int openres;

	if (of)
	{
		ov_clear(of);
		delete of;
		of = NULL;
	}

	//initialize player object that writes to sound device/file
	if (!opendevice(device, write2file))
		return seterrorcode(SOUND_ERROR_DEVOPENFAIL);
	
	if (!(f = fopen(filename, "r")))
		return seterrorcode(SOUND_ERROR_FILEOPENFAIL);
	
	of = new OggVorbis_File;

	//initialize OggVorbis structure
	if ( (openres = ov_open(f, of, NULL, 0)) < 0)
	{
		switch(openres)
		{
		case OV_EREAD: seterrorcode(SOUND_ERROR_FILEREADFAIL); break;
		case OV_EVERSION:
		case OV_ENOTVORBIS: seterrorcode(SOUND_ERROR_BAD); break;
		case OV_EBADHEADER: seterrorcode(SOUND_ERROR_BADHEADER); break;
		default: seterrorcode(SOUND_ERROR_UNKNOWN);
		}

		delete of;
		of = NULL;

		fclose(f); //if ov_open fails, it's safe to use fclose()
		return false;
	}

	vorbis_info *vi = ov_info(of, -1);
	channels = vi->channels - 1;
	srate = vi->rate;
	resetsound = 1;
	info.bitrate = (int)vi->bitrate_nominal;
	if (info.bitrate > 1000)
		info.bitrate /= 1000;
	info.mp3_version = vi->version;
	info.samplerate = (int)vi->rate;
	double totaltime = ov_time_total(of, -1);
	if (totaltime > 0)
		info.totaltime = int(totaltime);

	//retrieve interesting information from the user comments.
	char **ptr = ov_comment(of, -1)->user_comments;
	while (*ptr)
	{
		char hdr[strlen(*ptr)+1], value[strlen(*ptr)+1];
		int sval = sscanf(*ptr, "%[^=]=%[^\n]", hdr, value); //lazy, but works.

		if (sval == 2)
		{
			if (!strcasecmp(hdr, "album"))
			{
				strncpy(info.album, value, 30);
				info.album[30] = '\0';
			}
			else if (!strcasecmp(hdr, "artist"))
			{
				strncpy(info.artist, value, 30);
				info.artist[30] = '\0';
			}
			else if (!strcasecmp(hdr, "title"))
			{
				strncpy(info.songname, value, 30);
				info.songname[30] = '\0';
			}
			else
			{
				debug("OggVorbis Unkown Fieldname '%s' with value '%s'\n", hdr, value);
			}
		}
		++ptr;
	}

	return true;
}

void Oggplayer::closefile()
{
	if (of)
		ov_clear(of);
		delete of;
		of = NULL;
}

void Oggplayer::setforcetomono(short flag)
{
	if (flag)
		mono = 1;
	//TODO: make a function in soundplayer class to force mono.
}

void Oggplayer::set8bitmode()
{
	wordsize = 1;
	player->set8bitmode();
}

void Oggplayer::setdownfrequency(int value)
{
	downfreq = (value ? 1 : 0);
	//TODO: This doesn't do anything yet.
}

bool Oggplayer::playing()
{
	return true;
}

bool Oggplayer::run(int sec)
{
	int bitstream;
	long bytes_read = ov_read(of, soundbuf, 4096, bigendian, wordsize, signeddata,
		&bitstream);

	if (sec); //prevent warning

	if (bytes_read < 0)
		return seterrorcode(SOUND_ERROR_BAD);
	if (!bytes_read)
		return seterrorcode(SOUND_ERROR_FINISH);
	
	vorbis_info *vi = ov_info(of, bitstream);

	if (channels != vi->channels)
		channels = vi->channels, resetsound++;
	if (srate != vi->rate)
		srate = vi->rate, resetsound++;
		
	if (resetsound)
	{
		debug("OggVorbis channels/samplerate changed.\n");
		player->setsoundtype(vi->channels - 1, (wordsize == 2 ? 16 : 8),
			vi->rate >> downfreq);
		char bla[100];
		sprintf(bla, "channels/wordsize/rate=%d/%d/%ld\n",vi->channels,wordsize,
			vi->rate >> downfreq);
		debug(bla);
		resetsound = 0;
	}

	//If downsampling, we only need every other frame. We do this by
	//removing every other frame.
	//TODO: optimize!
	if (downfreq)
	{
		char *src, *dst, ssize = 1;
		int c, i;

		src=dst=(char*)soundbuf;
		ssize *= vi->channels;
		ssize *= wordsize;

		//ssize is #bytes for each sample.
		c = bytes_read / (ssize<<1); //nr. of samples to chop == half of total#
		while (c--)
		{
			i = ssize;
			while (--i > -1) //copy ssize bytes of each sample.
				*(dst + i) = *(src + i);
			dst += ssize;
			src += (ssize<<1); //skip 50% of samples.
		}
	}

	if (!player->putblock(soundbuf, (unsigned int)(bytes_read>>downfreq)))
		return seterrorcode(SOUND_ERROR_UNKNOWN);

	return true;
}

void Oggplayer::skip(int sec)
{
	double current_secs = elapsed_time();
	current_secs += (double)sec;
	ov_time_seek(of, current_secs);
}

bool Oggplayer::initialize(void *data)
{
	if (data);
	return true;
}
 
bool Oggplayer::forward(int sec)
{
	skip(sec);
	return true;
}
 
bool Oggplayer::rewind(int sec)
{
	skip(-sec);
	return true;
}
 
bool Oggplayer::pause()
{
	player->releasedevice();
	return true;
}
 
bool Oggplayer::unpause()
{
	return player->attachdevice();
}
 
bool Oggplayer::stop()
{
	return true;
}
 
bool Oggplayer::ready()
{
	return true;
}
 
int Oggplayer::elapsed_time()
{
	double elapsed = ov_time_tell(of);

	return (int)elapsed;
}
 
int Oggplayer::remaining_time()
{
	return info.totaltime - elapsed_time();
}
 
#endif /* INCLUDE_OGG */