Debian Codegraph @

Bundles/AO/ao.m

raw
1 /* AO.m - this file is part of Cynthiune
2 *
3 * Copyright (C) 2002-2004 Wolfgang Sourdeau
4 * Copyright (C) 2012 Philippe Roussel
5 *
6 * Author: Philippe Roussel <[email protected]>
7 * Riccardo Mottola <[email protected]>
8 *
9 * This file is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2, or (at your option)
12 * any later version.
13 *
14 * This file is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; see the file COPYING. If not, write to
21 * the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
22 * Boston, MA 02111-1307, USA.
23 */
24
25 #import <AppKit/AppKit.h>
26
27 #import <Cynthiune/CynthiuneBundle.h>
28 #import <Cynthiune/Output.h>
29 #import <ao/ao.h>
30
31
32 @interface AO : NSObject <CynthiuneBundle, Output>
33 {
34 id parentPlayer;
35 BOOL stopRequested;
36 ao_device *dev;
37 int driver;
38 ao_sample_format format;
39 unsigned char buffer[DEFAULT_BUFFER_SIZE];
40 NSLock *devlock;
41 }
42 @end
43
44 @implementation AO : NSObject
45
46 + (NSString *) bundleDescription
47 {
48 return @"Output plug-in based on libao";
49 }
50
51 + (NSArray *) bundleCopyrightStrings
52 {
53 return [NSArray arrayWithObjects: @"Copyright (C) 2012 Philippe Roussel", nil];
54 }
55
56 + (BOOL) isThreaded
57 {
58 return YES;
59 }
60
61 - (void) setParentPlayer: (id) aPlayer;
62 {
63 parentPlayer = aPlayer;
64 }
65
66 - (id) init
67 {
68 if ((self = [super init])) {
69 stopRequested = NO;
70 memset(&format, 0, sizeof(format));
71 devlock = [NSLock new];
72 ao_initialize();
73 driver = ao_default_driver_id();
74 }
75 return self;
76 }
77
78 /* FIXME : this is never called */
79 - (void)dealloc
80 {
81 ao_shutdown();
82 [devlock release];
83 [super dealloc];
84 }
85
86 - (BOOL) prepareDeviceWithChannels: (unsigned int) numberOfChannels
87 andRate: (unsigned long) sampleRate
88 withEndianness: (Endianness) e
89 {
90 BOOL result = YES;
91
92 format.channels = (int)numberOfChannels;
93 format.rate = (int)sampleRate;
94 /* FIXME : this should somehow come from the input bundle */
95 format.bits = 16;
96 if (e == BigEndian)
97 format.byte_format = AO_FMT_BIG;
98 else
99 format.byte_format = AO_FMT_LITTLE;
100 [devlock lock];
101 if (dev) {
102 ao_close(dev);
103 dev = ao_open_live(driver, &format, NULL);
104 result = (dev == NULL) ? NO : YES;
105 }
106 [devlock unlock];
107 return result;
108 }
109
110 - (BOOL) openDevice
111 {
112 BOOL result;
113
114 [devlock lock];
115 dev = ao_open_live(driver, &format, NULL);
116 result = (dev == NULL) ? NO : YES;
117 [devlock unlock];
118 return result;
119 }
120
121 - (void) closeDevice
122 {
123 [devlock lock];
124 ao_close(dev);
125 dev = NULL;
126 [devlock unlock];
127 }
128
129 - (void) threadLoop
130 {
131 int bufferSize;
132 NSAutoreleasePool *pool = [NSAutoreleasePool new];
133
134 while (!stopRequested) {
135 bufferSize = [parentPlayer readNextChunk: buffer
136 withSize: DEFAULT_BUFFER_SIZE];
137 [devlock lock];
138 if (dev && bufferSize > 0)
139 ao_play(dev, (char *)buffer, (uint_32)bufferSize);
140 [devlock unlock];
141 /* FIXME : copied from ALSA.m, I'm not sure this is needed */
142 if ([pool autoreleaseCount] > 50)
143 [pool emptyPool];
144 }
145 stopRequested = NO;
146 [pool release];
147 }
148
149 - (BOOL) startThread
150 {
151 [NSThread detachNewThreadSelector: @selector(threadLoop)
152 toTarget: self
153 withObject: nil];
154 return YES;
155 }
156
157 - (void) stopThread
158 {
159 stopRequested = YES;
160 }
161 @end