Debian Codegraph @

Bundles/Ogg/Ogg.m

raw
1 /* Ogg.m - this file is part of Cynthiune
2 *
3 * Copyright (C) 2002, 2003 Wolfgang Sourdeau
4 *
5 * Author: Wolfgang Sourdeau <[email protected]>
6 *
7 * This file is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2, or (at your option)
10 * any later version.
11 *
12 * This file is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; see the file COPYING. If not, write to
19 * the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 * Boston, MA 02111-1307, USA.
21 */
22
23 #import <Foundation/Foundation.h>
24
25 #import <string.h>
26 #import <vorbis/vorbisfile.h>
27
28 #import <Cynthiune/CynthiuneBundle.h>
29 #import <Cynthiune/Format.h>
30
31 #import "Ogg.h"
32
33 #define LOCALIZED(X) _b ([Ogg class], X)
34
35 static size_t
36 oggReadFunc (void *ptr, size_t size, size_t nmemb, void *datasource)
37 {
38 NSFileHandle *fileHandle;
39 NSData *data;
40 size_t length;
41
42 fileHandle = datasource;
43 data = [fileHandle readDataOfLength: size * nmemb];
44 length = [data length];
45 memcpy (ptr, [data bytes], length);
46
47 return length;
48 }
49
50 static int
51 oggSeekFunc (void *datasource, ogg_int64_t offset, int whence)
52 {
53 NSFileHandle *fileHandle;
54 unsigned long long realOffset;
55
56 fileHandle = datasource;
57
58 switch (whence)
59 {
60 case SEEK_SET:
61 realOffset = offset;
62 break;
63 case SEEK_CUR:
64 realOffset = [fileHandle offsetInFile] + offset;
65 break;
66 case SEEK_END:
67 realOffset = [fileHandle seekToEndOfFile] + offset;
68 break;
69 default:
70 NSLog (@"unrecognize value for whence: %d", whence);
71 realOffset = [fileHandle offsetInFile];
72 }
73
74 [fileHandle seekToFileOffset: realOffset];
75
76 return 0;
77 }
78
79 static int
80 oggCloseFunc (void *datasource)
81 {
82 NSFileHandle *fileHandle;
83
84 fileHandle = datasource;
85 [fileHandle closeFile];
86 [fileHandle release];
87
88 return 0;
89 }
90
91 static long
92 oggTellFunc (void *datasource)
93 {
94 NSFileHandle *fileHandle;
95
96 fileHandle = datasource;
97
98 return [fileHandle offsetInFile];
99 }
100
101 static ov_callbacks oggCallbacks = { oggReadFunc, oggSeekFunc,
102 oggCloseFunc, oggTellFunc };
103
104 @implementation Ogg : NSObject
105
106 + (NSString *) bundleDescription
107 {
108 return @"Extension plug-in for the Ogg Vorbis audio format";
109 }
110
111 + (NSArray *) bundleCopyrightStrings
112 {
113 return [NSArray arrayWithObjects:
114 @"Copyright (C) 2002-2005 Wolfgang Sourdeau",
115 nil];
116 }
117
118 + (NSArray *) compatibleTagBundles
119 {
120 return [NSArray arrayWithObjects: @"VorbisTags", @"Taglib", nil];
121 }
122
123 - (BOOL) streamOpen: (NSString *) fileName
124 {
125 NSFileHandle *_fileHandle;
126
127 _fileHandle = [NSFileHandle fileHandleForReadingAtPath: fileName];
128 bitStream = 0;
129
130 if (_fileHandle)
131 {
132 _ov_file = calloc (sizeof (OggVorbis_File), 1);
133 lastError = ov_open_callbacks (_fileHandle, _ov_file, NULL, 0L,
134 oggCallbacks);
135 if (lastError)
136 [_fileHandle closeFile];
137 else
138 [_fileHandle retain];
139 }
140 else
141 {
142 NSLog (@"No native handle...");
143 lastError = OV_EREAD;
144 }
145
146 return (lastError == 0);
147 }
148
149 + (BOOL) canTestFileHeaders
150 {
151 return YES;
152 }
153
154 + (BOOL) streamTestOpen: (NSString *) fileName
155 {
156 NSFileHandle *_fileHandle;
157 BOOL result = NO;
158 int vorbisError;
159 OggVorbis_File *_ov_test_file;
160
161 _fileHandle = [NSFileHandle fileHandleForReadingAtPath: fileName];
162
163 if (_fileHandle)
164 {
165 _ov_test_file = calloc (sizeof (OggVorbis_File), 1);
166 vorbisError = ov_open_callbacks (_fileHandle, _ov_test_file, NULL, 0L,
167 oggCallbacks);
168 if (vorbisError)
169 {
170 if (vorbisError != OV_ENOTVORBIS)
171 NSLog (@"Ogg: streamTestOpen: %@", [self errorText: vorbisError]);
172 [_fileHandle closeFile];
173 }
174 else
175 {
176 result = YES;
177 [_fileHandle retain];
178 ov_clear (_ov_test_file);
179 }
180 }
181
182 return result;
183 }
184
185 + (NSString *) errorText: (int) error
186 {
187 char *cErrorMessage;
188
189 switch (error)
190 {
191 case OV_EREAD:
192 cErrorMessage = "A read from media returned an error";
193 break;
194 case OV_ENOTVORBIS:
195 cErrorMessage = "Bitstream is not Vorbis data";
196 break;
197 case OV_EVERSION:
198 cErrorMessage = "Vorbis version mismatch";
199 break;
200 case OV_EBADHEADER:
201 cErrorMessage = "Invalid Vorbis bitstream header";
202 break;
203 case OV_EFAULT:
204 cErrorMessage = "Internal logic fault";
205 break;
206 case OV_ENOSEEK:
207 cErrorMessage = "Bitstream is not seekable";
208 break;
209 case OV_EINVAL:
210 cErrorMessage = "Invalid argument value";
211 break;
212 case OV_EBADLINK:
213 cErrorMessage = "Invalid stream section supplied to libvorbisfile, or"
214 " the requested link is corrupt";
215 break;
216 case 0:
217 cErrorMessage = "(No error)";
218 break;
219 default:
220 cErrorMessage = "(undefined error)";
221 }
222
223 return [NSString stringWithUTF8String: cErrorMessage];
224 }
225
226 - (int) readNextChunk: (unsigned char *) buffer
227 withSize: (unsigned int) bufferSize
228 {
229 int bytes_read;
230
231 if (_ov_file)
232 {
233 bytes_read = ov_read (_ov_file, (char *) buffer, bufferSize,
234 0, 2, 1, &bitStream);
235 if (bytes_read < 0)
236 lastError = bytes_read;
237 }
238 else
239 {
240 lastError = OV_EFAULT;
241 bytes_read = -1;
242 }
243
244 return bytes_read;
245 }
246
247 - (int) lastError
248 {
249 return lastError;
250 }
251
252 - (unsigned int) readChannels
253 {
254 return (_ov_file->vi->channels);
255 }
256
257 - (unsigned long) readRate
258 {
259 return (_ov_file->vi->rate);
260 }
261
262 - (Endianness) endianness
263 {
264 return NativeEndian;
265 }
266
267 - (unsigned int) readDuration
268 {
269 return (ov_time_total (_ov_file, -1));
270 }
271
272 - (void) streamClose
273 {
274 ov_clear (_ov_file);
275 _ov_file = NULL;
276 }
277
278 // Player Protocol
279 + (NSArray *) acceptedFileExtensions
280 {
281 return [NSArray arrayWithObject: @"ogg"];
282 }
283
284 - (BOOL) isSeekable
285 {
286 return (BOOL) ov_seekable (_ov_file);
287 }
288
289 - (void) seek: (unsigned int) aPos
290 {
291 ogg_int64_t pcmPos;
292 pcmPos = aPos * [self readChannels] * [self readRate] / 2;
293 #ifdef __MACOSX__
294 ov_pcm_seek (_ov_file, pcmPos);
295 #else
296 ov_pcm_seek_lap (_ov_file, pcmPos);
297 #endif
298 }
299
300 @end