Debian Codegraph @

Bundles/aRts/aRts.m

raw
1 /* aRts.m - this file is part of Cynthiune
2 *
3 * Copyright (C) 2005 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 #ifndef _REENTRANT
24 #define _REENTRANT 1
25 #endif
26
27 #import <Foundation/NSData.h>
28 #import <Foundation/NSNotification.h>
29 #import <Foundation/NSTask.h>
30 #import <Foundation/NSThread.h>
31
32 #import <artsc.h>
33
34 #import <Cynthiune/CynthiuneBundle.h>
35 #import <Cynthiune/Output.h>
36 #import <Cynthiune/Preference.h>
37
38 #import "aRts.h"
39
40 #define LOCALIZED(X) _b ([aRts class], X)
41 #define artsStreamName "Cynthiune stream"
42
43 @implementation aRts : NSObject
44
45 + (NSString *) bundleDescription
46 {
47 return @"Output plug-in for the aRts sound daemon";
48 }
49
50 + (NSArray *) bundleCopyrightStrings
51 {
52 return [NSArray arrayWithObjects:
53 @"Copyright (C) 2005 Wolfgang Sourdeau",
54 nil];
55 }
56
57 + (BOOL) isThreaded
58 {
59 return YES;
60 }
61
62 - (id) init
63 {
64 if ((self = [super init]))
65 {
66 outputStream = NULL;
67 channels = 0;
68 rate = 0;
69 stopRequested = NO;
70 }
71
72 return self;
73 }
74
75 - (void) setParentPlayer: (id) aPlayer;
76 {
77 parentPlayer = aPlayer;
78 }
79
80 - (BOOL) prepareDeviceWithChannels: (unsigned int) numberOfChannels
81 andRate: (unsigned long) sampleRate
82 withEndianness: (Endianness) e
83 {
84 BOOL result;
85
86 channels = numberOfChannels;
87 rate = sampleRate;
88
89 if (outputStream)
90 {
91 arts_close_stream (outputStream);
92 outputStream = arts_play_stream (rate, 16, channels, artsStreamName);
93 arts_stream_set (outputStream, ARTS_P_BLOCKING, 1);
94 result = (outputStream != NULL);
95 }
96 else
97 result = YES;
98
99 return result;
100 }
101
102 - (BOOL) _launchServer
103 {
104 NSTask *artsdTask;
105 NSString *artsdPath;
106
107 artsdTask = [NSTask new];
108 [artsdTask autorelease];
109 artsdPath = [NSString stringWithCString: ARTSD];
110 [artsdTask setLaunchPath: artsdPath];
111 [artsdTask launch];
112
113 [NSThread sleepUntilDate:
114 [NSDate dateWithTimeIntervalSinceNow: 0.2]];
115
116 return [artsdTask isRunning];
117 }
118
119 - (BOOL) openDevice
120 {
121 BOOL result;
122 int code;
123
124 if (outputStream)
125 result = YES;
126 else
127 {
128 /* FIXME: should add code to spawn the daemon */
129 code = arts_init ();
130 if (code == ARTS_E_NOSERVER)
131 if ([self _launchServer])
132 code = arts_init ();
133 if (code == 0)
134 {
135 outputStream = arts_play_stream (rate, 16, channels, artsStreamName);
136 arts_stream_set (outputStream, ARTS_P_BLOCKING, 1);
137 result = (outputStream != NULL);
138 }
139 else
140 {
141 NSLog (@"failure opening device:\n%d, %s", code, arts_error_text (code));
142 result = NO;
143 }
144 }
145
146 return result;
147 }
148
149 - (void) closeDevice
150 {
151 while (stopRequested)
152 [NSThread sleepUntilDate: [NSDate dateWithTimeIntervalSinceNow: 0.01]];
153 arts_close_stream (outputStream);
154 outputStream = NULL;
155 arts_free ();
156 }
157
158 - (void) threadLoop
159 {
160 NSAutoreleasePool *pool;
161 int bufferSize;
162
163 pool = [NSAutoreleasePool new];
164
165 while (!stopRequested)
166 {
167 bufferSize = [parentPlayer readNextChunk: buffer
168 withSize: DEFAULT_BUFFER_SIZE];
169 if (bufferSize > 0)
170 arts_write (outputStream, buffer, bufferSize);
171
172 if ([pool autoreleaseCount] > 50)
173 [pool emptyPool];
174 }
175
176 stopRequested = NO;
177 [pool release];
178 }
179
180 - (BOOL) startThread
181 {
182 [NSThread detachNewThreadSelector: @selector (threadLoop)
183 toTarget: self
184 withObject: nil];
185
186 return YES;
187 }
188
189 - (void) stopThread
190 {
191 stopRequested = YES;
192 }
193
194 @end