-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathspcplayerosx.c
216 lines (167 loc) · 6.05 KB
/
spcplayerosx.c
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
/*
Stephen B Melvin Jr, <stephenbmelvin@gmail.com>
Version 0.3 OSX Core Audio Version
Usage: ./soap filename.spc
To exit use Control-C.
*/
#include <CoreAudio/CoreAudio.h>
#include <CoreServices/CoreServices.h>
#include <stdio.h>
#include <unistd.h>
#include <AudioUnit/AudioUnit.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/mman.h>
#include <fcntl.h>
#include "openspc.h"
#include <unistd.h>
#include <sys/stat.h>
#include <ctype.h>
#include <string.h>
#include <errno.h>
#include <strings.h>
UInt32 theFormatFlags = kAudioFormatFlagIsSignedInteger | kAudioFormatFlagsNativeEndian | kLinearPCMFormatFlagIsPacked | kAudioFormatFlagIsNonInterleaved;
UInt32 theBytesPerFrame = 2,theBytesInAPacket = 2;
UInt32 theBitsPerChannel = 16;
AudioUnit gOutputUnit;
UInt32 initAudio(void);
void playAudio(void);
OSStatus MyRenderer(void *inRefCon, AudioUnitRenderActionFlags *ioActionFlags, const AudioTimeStamp *inTimeStamp, UInt32 inBusNumber, UInt32 inNumberFrames, AudioBufferList *ioData);
void *buf;
int main(int argc, char *argv[]){
if(argc!=2){
printf("Usage: ./spcplayer filename.spc\n");
return 1;
}
int fd;
char c;
void *ptr;
off_t size;
buf=malloc(32000);
fd=open(argv[1],O_RDONLY);
if(fd==-1){
perror(argv[1]);
return 1;
}
size=lseek(fd,0,SEEK_END);
lseek(fd,0,SEEK_SET);
ptr=malloc(size);
read(fd,ptr,size);
close(fd);
fd=OSPC_Init(ptr,size);
free(ptr);
initAudio();
playAudio();
return 0;
}
UInt32 initAudio(void)
{
OSStatus err = noErr;
ComponentDescription desc;
Component comp;
//There are several Different types of AudioUnits.
//Some audio units serve as Outputs, Mixers, or DSP
//units. See AUComponent.h for listing
desc.componentType = kAudioUnitType_Output;
//Every Component has a subType, which will give a clearer picture
//of what this components function will be.
desc.componentSubType = kAudioUnitSubType_DefaultOutput;
//All AudioUnits in AUComponent.h must use
//"kAudioUnitManufacturer_Apple" as the Manufacturer
desc.componentManufacturer = kAudioUnitManufacturer_Apple;
desc.componentFlags = 0;
desc.componentFlagsMask = 0;
//Finds a component that meets the desc spec's
comp = FindNextComponent(NULL, &desc);
if (comp == NULL) exit (-1);
//gains access to the services provided by the component
err = OpenAComponent(comp, &gOutputUnit);
if (err) { printf ("AudioUnitSetProperty-CB=%ld\n", (long int)err); return; }
// Set up a callback function to generate output to the output unit
AURenderCallbackStruct input;
input.inputProc = MyRenderer;
input.inputProcRefCon = NULL;
err = AudioUnitSetProperty (gOutputUnit,
kAudioUnitProperty_SetRenderCallback,
kAudioUnitScope_Input,
0,
&input,
sizeof(input));
if (err) {
printf ("AudioUnitSetProperty-CB=%ld\n", (long int)err);
return;
}
}
OSStatus MyRenderer(void *inRefCon,
AudioUnitRenderActionFlags *ioActionFlags,
const AudioTimeStamp *inTimeStamp,
UInt32 inBusNumber,
UInt32 inNumberFrames,
AudioBufferList *ioData)
{
fflush(stdout);
int frame=0;
int size,channel=0;
size=OSPC_Run(-1,buf,inNumberFrames*4);
for(frame; frame<inNumberFrames; ++frame)
{
memcpy(ioData->mBuffers[0].mData+(frame*2), buf+(frame*4), 2);
memcpy(ioData->mBuffers[1].mData+(frame*2), buf+(frame*4)+2, 2);
}
return noErr;
}
void playAudio(){
OSStatus err = noErr;
// We tell the Output Unit what format we're going to supply data to it
// this is necessary if you're providing data through an input callback
// AND you want the DefaultOutputUnit to do any format conversions
// necessary from your format to the device's format.
AudioStreamBasicDescription streamFormat;
streamFormat.mSampleRate = 32000.0; // the sample rate of the audio stream
streamFormat.mFormatID = kAudioFormatLinearPCM;; // the specific encoding type of audio stream
streamFormat.mFormatFlags = theFormatFlags; // flags specific to each format
streamFormat.mBytesPerPacket = theBytesInAPacket;
streamFormat.mFramesPerPacket = 1;
streamFormat.mBytesPerFrame = theBytesPerFrame;
streamFormat.mChannelsPerFrame = 2;
streamFormat.mBitsPerChannel = theBitsPerChannel;
err = AudioUnitSetProperty (gOutputUnit,
kAudioUnitProperty_StreamFormat,
kAudioUnitScope_Input,
0,
&streamFormat,
sizeof(AudioStreamBasicDescription));
if (err) { printf ("AudioUnitSetProperty-SF=%4.4s, %ld\n", (char*)&err, (long int)err); return; }
// Initialize unit
err = AudioUnitInitialize(gOutputUnit);
if (err) { printf ("AudioUnitInitialize=%ld\n", (long int)err); return; }
Float64 outSampleRate;
UInt32 size = sizeof(Float64);
err = AudioUnitGetProperty (gOutputUnit,
kAudioUnitProperty_SampleRate,
kAudioUnitScope_Output,
0,
&outSampleRate,
&size);
if (err) { printf ("AudioUnitSetProperty-GF=%4.4s, %ld\n", (char*)&err, (long int)err); return; }
// Start the rendering
// The DefaultOutputUnit will do any format conversions to the format of the default device
err = AudioOutputUnitStart (gOutputUnit);
if (err) { printf ("AudioOutputUnitStart=%ld\n", (long int)err); return; }
// we call the CFRunLoopRunInMode to service any notifications that the audio
// system has to deal with
printf("\nPress Control-C to stop playback.\n");
CFRunLoopRun();
//CFRunLoopRunInMode(kCFRunLoopDefaultMode, 20, false);
// REALLY after you're finished playing STOP THE AUDIO OUTPUT UNIT!!!!!!
// but we never get here because we're running until the process is nuked...
verify_noerr (AudioOutputUnitStop (gOutputUnit));
err = AudioUnitUninitialize (gOutputUnit);
if (err) { printf ("AudioUnitUninitialize=%ld\n", (long int)err); return; }
}
void CloseDefaultAU()
{
CloseComponent (gOutputUnit);
}