| 1 |
/* Esound.m - this file is part of Cynthiune |
| 2 |
* |
| 3 |
* Copyright (C) 2003, 2004 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 <unistd.h> |
| 28 |
#import <esd.h> |
| 29 |
|
| 30 |
#import <AppKit/NSApplication.h> |
| 31 |
#import <Foundation/NSArray.h> |
| 32 |
#import <Foundation/NSFileHandle.h> |
| 33 |
#import <Foundation/NSNotification.h> |
| 34 |
#import <Foundation/NSRunLoop.h> |
| 35 |
#import <Foundation/NSThread.h> |
| 36 |
|
| 37 |
#import <Cynthiune/CynthiuneBundle.h> |
| 38 |
#import <Cynthiune/Output.h> |
| 39 |
#import <Cynthiune/Preference.h> |
| 40 |
#import <Cynthiune/utils.h> |
| 41 |
|
| 42 |
#import "Esound.h" |
| 43 |
#import "EsoundPreference.h" |
| 44 |
|
| 45 |
#define LOCALIZED(X) _b ([Esound class], X) |
| 46 |
|
| 47 |
static NSNotificationCenter *nc = nil; |
| 48 |
static NSArray *loopModes; |
| 49 |
|
| 50 |
@implementation Esound : NSObject |
| 51 |
|
| 52 |
+ (void) initialize |
| 53 |
{ |
| 54 |
nc = [NSNotificationCenter defaultCenter]; |
| 55 |
loopModes = [NSArray arrayWithObjects: NSDefaultRunLoopMode, |
| 56 |
NSEventTrackingRunLoopMode, nil]; |
| 57 |
[loopModes retain]; |
| 58 |
} |
| 59 |
|
| 60 |
+ (void) unload |
| 61 |
{ |
| 62 |
[loopModes release]; |
| 63 |
} |
| 64 |
|
| 65 |
+ (NSString *) bundleDescription |
| 66 |
{ |
| 67 |
return @"Output plug-in for the Esound sound daemon"; |
| 68 |
} |
| 69 |
|
| 70 |
+ (NSArray *) bundleCopyrightStrings |
| 71 |
{ |
| 72 |
return [NSArray arrayWithObjects: |
| 73 |
@"Copyright (C) 2003, 2004 Wolfgang Sourdeau", |
| 74 |
nil]; |
| 75 |
} |
| 76 |
|
| 77 |
+ (BOOL) isThreaded |
| 78 |
{ |
| 79 |
return NO; |
| 80 |
} |
| 81 |
|
| 82 |
- (void) setParentPlayer: (id) aPlayer; |
| 83 |
{ |
| 84 |
parentPlayer = aPlayer; |
| 85 |
} |
| 86 |
|
| 87 |
- (id) init |
| 88 |
{ |
| 89 |
if ((self = [super init])) |
| 90 |
{ |
| 91 |
esd = nil; |
| 92 |
} |
| 93 |
|
| 94 |
return self; |
| 95 |
} |
| 96 |
|
| 97 |
- (int) _openSocket |
| 98 |
{ |
| 99 |
int esdSocket; |
| 100 |
NSString *hostString; |
| 101 |
EsoundPreference *esoundPreference; |
| 102 |
|
| 103 |
esoundPreference = [EsoundPreference instance]; |
| 104 |
|
| 105 |
if ([esoundPreference socketIsTCP]) |
| 106 |
{ |
| 107 |
hostString = [esoundPreference tcpHostConnectString]; |
| 108 |
esdSocket = esd_play_stream ((ESD_BITS16 | ESD_STREAM |
| 109 |
| ESD_PLAY | channels), |
| 110 |
rate, [hostString cString], |
| 111 |
"Cynthiune stream"); |
| 112 |
} |
| 113 |
else |
| 114 |
esdSocket = esd_play_stream_fallback ((ESD_BITS16 | ESD_STREAM |
| 115 |
| ESD_PLAY | channels), |
| 116 |
rate, NULL, |
| 117 |
"Cynthiune stream"); |
| 118 |
|
| 119 |
return esdSocket; |
| 120 |
} |
| 121 |
|
| 122 |
- (BOOL) openDevice |
| 123 |
{ |
| 124 |
int esdSocket, try; |
| 125 |
|
| 126 |
try = 0; |
| 127 |
esdSocket = [self _openSocket]; |
| 128 |
while (esdSocket < 1 && try < 5) |
| 129 |
{ |
| 130 |
try++; |
| 131 |
[NSThread sleepUntilDate: |
| 132 |
[NSDate dateWithTimeIntervalSinceNow: 0.2]]; |
| 133 |
esdSocket = [self _openSocket]; |
| 134 |
} |
| 135 |
|
| 136 |
if (esdSocket > 0) |
| 137 |
{ |
| 138 |
esd = [[NSFileHandle alloc] initWithFileDescriptor: esdSocket]; |
| 139 |
if (esd) |
| 140 |
[nc addObserver: self |
| 141 |
selector: @selector (_writeCompleteNotification:) |
| 142 |
name: GSFileHandleWriteCompletionNotification |
| 143 |
object: esd]; |
| 144 |
else |
| 145 |
close (esdSocket); |
| 146 |
} |
| 147 |
|
| 148 |
return (esd != nil); |
| 149 |
} |
| 150 |
|
| 151 |
- (void) closeDevice |
| 152 |
{ |
| 153 |
[esd closeFile]; |
| 154 |
[esd release]; |
| 155 |
esd = nil; |
| 156 |
} |
| 157 |
|
| 158 |
- (BOOL) prepareDeviceWithChannels: (unsigned int) numberOfChannels |
| 159 |
andRate: (unsigned long) sampleRate |
| 160 |
withEndianness: (Endianness) e |
| 161 |
{ |
| 162 |
BOOL result; |
| 163 |
|
| 164 |
result = YES; |
| 165 |
rate = sampleRate; |
| 166 |
|
| 167 |
switch (numberOfChannels) |
| 168 |
{ |
| 169 |
case 1: |
| 170 |
channels = ESD_MONO; |
| 171 |
break; |
| 172 |
case 2: |
| 173 |
channels = ESD_STEREO; |
| 174 |
break; |
| 175 |
default: |
| 176 |
result = NO; |
| 177 |
} |
| 178 |
|
| 179 |
if (result && esd) |
| 180 |
{ |
| 181 |
[self closeDevice]; |
| 182 |
result = [self openDevice]; |
| 183 |
} |
| 184 |
|
| 185 |
return result; |
| 186 |
} |
| 187 |
|
| 188 |
- (void) _writeCompleteNotification: (NSNotification *) aNotification |
| 189 |
{ |
| 190 |
[parentPlayer chunkFinishedPlaying]; |
| 191 |
} |
| 192 |
|
| 193 |
- (void) playChunk: (NSData *) chunk |
| 194 |
{ |
| 195 |
[esd writeInBackgroundAndNotify: chunk forModes: loopModes]; |
| 196 |
} |
| 197 |
|
| 198 |
@end |