Updating built in Io code to use += instead of x = x + y
[io/quag.git] / addons / PortAudio / source / new / IoAudioTrack.c
blob0cc01c3ea1145bdd0f1b51c10e84615b28fb431c
1 /* Copyright (c) 2002, Steve Dekorte
2 * All rights reserved. See _BSDLicense.txt.
3 */
5 #include "IoAudioTrack.h"
6 #include "IoObject_actor.h"
7 #include "base/List.h"
8 #include "IoState.h"
9 #include "IoNumber.h"
10 #include "IoBuffer.h"
11 #include "IoNil.h"
14 IoObject *IoMessage_locals_audioTrackArgAt_(IoMessage *self, IoObject *locals, int n)
16 IoObject *v = IoMessage_locals_valueArgAt_(self, locals, n);
17 if (!ISTRACK(v)) IoMessage_locals_numberArgAt_errorForType_(self, locals, n, "Track");
18 return v;
21 #define DATA(self) ((IoAudioTrackData *)self->data)
23 IoTag *IoAudioTrack_newTag(void *state)
25 IoTag *tag = IoTag_newWithName_("Sound");
26 IoTag_state_(tag, state);
27 IoTag_cloneFunc_(tag, (IoTagCloneFunc *)IoAudioTrack_rawClone);
28 IoTag_markFunc_(tag, (IoTagMarkFunc *)IoAudioTrack_mark);
29 IoTag_freeFunc_(tag, (IoTagFreeFunc *)IoAudioTrack_free);
30 return tag;
33 IoAudioTrack *IoAudioTrack_proto(void *state)
35 IoObject *self = IoObject_new(state);
36 IoObject_tag_(self, IoAudioTrack_newTag(state));
38 self->data = calloc(1, sizeof(IoAudioTrackData));
40 DATA(self)->buffer = IoBuffer_newWithCString_(IOSTATE, "");
41 DATA(self)->sound = Sound_new();
42 Sound_setExternalData_(DATA(self)->sound, IoBuffer_rawUArray(DATA(self)->buffer));
44 IoState_registerProtoWithFunc_(state, self, IoAudioTrack_proto);
46 IoObject_addMethod_(self, IOSTRING("setPath"), IoAudioTrack_setPath);
47 IoObject_addMethod_(self, IOSTRING("path"), IoAudioTrack_path);
48 IoObject_addMethod_(self, IOSTRING("open"), IoAudioTrack_open);
49 IoObject_addMethod_(self, IOSTRING("read"), IoAudioTrack_read);
50 IoObject_addMethod_(self, IOSTRING("write"), IoAudioTrack_write);
52 IoObject_addMethod_(self, IOSTRING("setSampleRate"), IoAudioTrack_setSampleRate);
53 IoObject_addMethod_(self, IOSTRING("sampleRate"), IoAudioTrack_sampleRate);
55 IoObject_addMethod_(self, IOSTRING("setChannels"), IoAudioTrack_setChannels);
56 IoObject_addMethod_(self, IOSTRING("channels"), IoAudioTrack_channels);
58 IoObject_addMethod_(self, IOSTRING("setBuffer"), IoAudioTrack_setBuffer);
59 IoObject_addMethod_(self, IOSTRING("buffer"), IoAudioTrack_buffer);
61 IoObject_addMethod_(self, IOSTRING("sampleCount"), IoAudioTrack_sampleCount);
62 IoObject_addMethod_(self, IOSTRING("seconds"), IoAudioTrack_seconds);
64 IoObject_addMethod_(self, IOSTRING("setVolume"), IoAudioTrack_setVolume);
65 IoObject_addMethod_(self, IOSTRING("volume"), IoAudioTrack_volume);
67 IoObject_addMethod_(self, IOSTRING("setChannelVolume"), IoAudioTrack_setChannelVolume);
68 IoObject_addMethod_(self, IOSTRING("channelVolume"), IoAudioTrack_channelVolume);
70 IoObject_addMethod_(self, IOSTRING("aveOutputCountLength"), IoAudioTrack_aveOutputCountLength);
71 IoObject_addMethod_(self, IOSTRING("setAveOutputCountLength"), IoAudioTrack_setAveOutputCountLength);
73 IoObject_addMethod_(self, IOSTRING("setPosition"), IoAudioTrack_setPosition);
74 IoObject_addMethod_(self, IOSTRING("position"), IoAudioTrack_position);
76 IoObject_addMethod_(self, IOSTRING("setPlayPosition"), IoAudioTrack_setPlayPosition);
77 IoObject_addMethod_(self, IOSTRING("playPosition"), IoAudioTrack_playPosition);
79 IoObject_addMethod_(self, IOSTRING("isLooping"), IoAudioTrack_isLooping);
80 IoObject_addMethod_(self, IOSTRING("loopingOn"), IoAudioTrack_loopingOn);
81 IoObject_addMethod_(self, IOSTRING("loopingOff"), IoAudioTrack_loopingOff);
82 IoObject_addMethod_(self, IOSTRING("buffer"), IoAudioTrack_buffer);
83 IoObject_addMethod_(self, IOSTRING("resampleToSampleCount"), IoAudioTrack_resampleToSampleCount);
85 IoObject_addMethod_(self, IOSTRING("setPlayLength"), IoAudioTrack_setPlayLength);
86 IoObject_addMethod_(self, IOSTRING("playLength"), IoAudioTrack_playLength);
88 IoObject_addMethod_(self, IOSTRING("pixelsPerSecond"), IoAudioTrack_pixelsPerSecond);
90 IoObject_addMethod_(self, IOSTRING("setPixelsPerVolume"), IoAudioTrack_setPixelsPerVolume);
91 IoObject_addMethod_(self, IOSTRING("pixelsPerVolume"), IoAudioTrack_pixelsPerVolume);
93 /* --- volumes --- */
94 IoObject_addMethod_(self, IOSTRING("setVolumeFromTo"), IoAudioTrack_setVolumeFromTo);
95 IoObject_addMethod_(self, IOSTRING("volumeAt"), IoAudioTrack_volumeAt);
96 IoObject_addMethod_(self, IOSTRING("setSamplesPerVolume"), IoAudioTrack_setSamplesPerVolume);
97 IoObject_addMethod_(self, IOSTRING("samplesPerVolume"), IoAudioTrack_samplesPerVolume);
99 IoObject_addMethod_(self, IOSTRING("waveFormDisplayList"), IoAudioTrack_waveFormDisplayList);
100 IoObject_addMethod_(self, IOSTRING("updateWaveFormDisplayList"), IoAudioTrack_updateWaveFormDisplayList);
101 IoObject_addMethod_(self, IOSTRING("volumeMaskDisplayList"), IoAudioTrack_volumeMaskDisplayList);
102 IoObject_addMethod_(self, IOSTRING("updateVolumeMaskDisplayList"), IoAudioTrack_updateVolumeMaskDisplayList);
104 return self;
107 IoAudioTrack *IoAudioTrack_rawClone(IoAudioTrack *self)
109 IoObject *child = IoObject_rawClonePrimitive(self);
110 child->data = cpalloc(self->data, sizeof(IoAudioTrackData));
111 DATA(child)->buffer = IoBuffer_rawClone(DATA(self)->buffer);
112 DATA(child)->sound = Sound_new();
113 Sound_setExternalData_(DATA(child)->sound, IoBuffer_rawUArray(DATA(child)->buffer));
114 return child;
117 IoAudioTrack *IoAudioTrack_new(void *state)
119 IoObject *proto = IoState_protoWithInitFunction_(state, IoAudioTrack_proto);
120 return IoAudioTrack_rawClone(proto);
123 /* ----------------------------------------------------------- */
125 void IoAudioTrack_free(IoAudioTrack *self)
127 Sound_free(DATA(self)->sound);
128 free(self->data);
131 void IoAudioTrack_mark(IoAudioTrack *self)
133 IoObject_makeGrayIfWhite(DATA(self)->buffer);
136 Sound *IoAudioTrack_rawSound(IoAudioTrack *self) { return DATA(self)->sound; }
138 /* ----------------------------------------------------------- */
140 IoObject *IoAudioTrack_setPath(IoAudioTrack *self, IoObject *locals, IoMessage *m)
142 Sound_setPath_(DATA(self)->sound, IoMessage_locals_cStringArgAt_(m, locals, 0));
143 return self;
146 IoObject *IoAudioTrack_path(IoAudioTrack *self, IoObject *locals, IoMessage *m)
147 { return IOSTRING(Sound_path(DATA(self)->sound)); }
149 IoObject *IoAudioTrack_open(IoAudioTrack *self, IoObject *locals, IoMessage *m)
151 IoAudioTrack_setPath(self, locals, m);
152 IoAudioTrack_read(self, locals, m);
153 return self;
156 IoObject *IoAudioTrack_read(IoAudioTrack *self, IoObject *locals, IoMessage *m)
157 { return Sound_read(DATA(self)->sound) ? self : IONIL(self); }
159 IoObject *IoAudioTrack_write(IoAudioTrack *self, IoObject *locals, IoMessage *m)
160 { return Sound_write(DATA(self)->sound) ? self : IONIL(self); }
162 IoObject *IoAudioTrack_setSampleRate(IoAudioTrack *self, IoObject *locals, IoMessage *m)
164 Sound_setSampleRate_(DATA(self)->sound,
165 IoMessage_locals_intArgAt_(m, locals, 0));
166 return self;
169 IoObject *IoAudioTrack_sampleRate(IoAudioTrack *self, IoObject *locals, IoMessage *m)
170 { return IONUMBER(Sound_sampleRate(DATA(self)->sound)); }
172 IoObject *IoAudioTrack_setChannels(IoAudioTrack *self, IoObject *locals, IoMessage *m)
174 Sound_setChannels_(DATA(self)->sound,
175 IoMessage_locals_intArgAt_(m, locals, 0));
176 return self;
179 IoObject *IoAudioTrack_channels(IoAudioTrack *self, IoObject *locals, IoMessage *m)
180 { return IONUMBER(Sound_channels(DATA(self)->sound)); }
182 IoObject *IoAudioTrack_setBuffer(IoAudioTrack *self, IoObject *locals, IoMessage *m)
184 IoBuffer *b = IoMessage_locals_bufferArgAt_(m, locals, 0);
185 Sound_setExternalData_(DATA(self)->sound, IoBuffer_rawUArray(b));
186 DATA(self)->buffer = IOREF(b);
187 return self;
190 IoObject *IoAudioTrack_buffer(IoAudioTrack *self, IoObject *locals, IoMessage *m)
191 { return DATA(self)->buffer; }
193 IoObject *IoAudioTrack_sampleCount(IoAudioTrack *self, IoObject *locals, IoMessage *m)
194 { return IONUMBER(Sound_sampleCount(DATA(self)->sound)); }
196 IoObject *IoAudioTrack_seconds(IoAudioTrack *self, IoObject *locals, IoMessage *m)
198 double d = Sound_seconds(DATA(self)->sound);
199 return IONUMBER(d);
202 IoObject *IoAudioTrack_setVolume(IoAudioTrack *self, IoObject *locals, IoMessage *m)
204 Sound_setVolume_(DATA(self)->sound,
205 IoMessage_locals_doubleArgAt_(m, locals, 0));
206 return self;
209 IoObject *IoAudioTrack_volume(IoAudioTrack *self, IoObject *locals, IoMessage *m)
210 { return IONUMBER(Sound_volume(DATA(self)->sound)); }
212 IoObject *IoAudioTrack_setChannelVolume(IoAudioTrack *self, IoObject *locals, IoMessage *m)
214 int c = IoMessage_locals_intArgAt_(m, locals, 0);
215 float f = (float)IoMessage_locals_doubleArgAt_(m, locals, 1);
216 if (c == 0) { Sound_setLeftVolume_(DATA(self)->sound, f); } else
217 if (c == 1) { Sound_setRightVolume_(DATA(self)->sound, f); }
218 return self;
221 IoObject *IoAudioTrack_channelVolume(IoAudioTrack *self, IoObject *locals, IoMessage *m)
223 int c = IoMessage_locals_intArgAt_(m, locals, 0);
224 if (c == 0) { return IONUMBER(Sound_leftVolume(DATA(self)->sound)); } else
225 if (c == 1) { return IONUMBER(Sound_rightVolume(DATA(self)->sound)); }
226 return IONIL(self);
229 IoObject *IoAudioTrack_aveOutput(IoAudioTrack *self, IoObject *locals, IoMessage *m)
230 { return IONUMBER(Sound_aveOutput(DATA(self)->sound)); }
232 IoObject *IoAudioTrack_aveOutputCountLength(IoAudioTrack *self, IoObject *locals, IoMessage *m)
233 { return IONUMBER(Sound_aveOutputCountLength(DATA(self)->sound)); }
235 IoObject *IoAudioTrack_setAveOutputCountLength(IoAudioTrack *self, IoObject *locals, IoMessage *m)
237 Sound_setAveOutputCountLength_(DATA(self)->sound,
238 IoMessage_locals_intArgAt_(m, locals, 0));
239 return self;
242 IoObject *IoAudioTrack_setPosition(IoAudioTrack *self, IoObject *locals, IoMessage *m)
244 Sound_setPosition_(DATA(self)->sound,
245 IoMessage_locals_intArgAt_(m, locals, 0));
246 return self;
249 IoObject *IoAudioTrack_position(IoAudioTrack *self, IoObject *locals, IoMessage *m)
250 { return IONUMBER(Sound_position(DATA(self)->sound)); }
252 IoObject *IoAudioTrack_setPlayPosition(IoAudioTrack *self, IoObject *locals, IoMessage *m)
254 Sound_setPlayPosition_(DATA(self)->sound,
255 IoMessage_locals_intArgAt_(m, locals, 0));
256 return self;
259 IoObject *IoAudioTrack_playPosition(IoAudioTrack *self, IoObject *locals, IoMessage *m)
260 { return IONUMBER(Sound_playPosition(DATA(self)->sound)); }
262 IoObject *IoAudioTrack_isLooping(IoAudioTrack *self, IoObject *locals, IoMessage *m)
263 { return IONUMBER(Sound_isLooping(DATA(self)->sound)); }
265 IoObject *IoAudioTrack_loopingOn(IoAudioTrack *self, IoObject *locals, IoMessage *m)
266 { Sound_loopingOn(DATA(self)->sound); return self;}
268 IoObject *IoAudioTrack_loopingOff(IoAudioTrack *self, IoObject *locals, IoMessage *m)
269 { Sound_loopingOff(DATA(self)->sound); return self; }
271 IoObject *IoAudioTrack_resampleToSampleCount(IoAudioTrack *self, IoObject *locals, IoMessage *m)
273 int sc = IoMessage_locals_intArgAt_(m, locals, 0);
274 IOASSERT(sc > 0, "sample count must be greater than 0");
275 Sound_resizeToSampleCount_(DATA(self)->sound, sc);
276 return self;
279 /* --- playLength --- */
281 IoObject *IoAudioTrack_setPlayLength(IoAudioTrack *self, IoObject *locals, IoMessage *m)
283 Sound_playLength_(DATA(self)->sound, IoMessage_locals_longArgAt_(m, locals, 0));
284 return self;
287 IoObject *IoAudioTrack_playLength(IoAudioTrack *self, IoObject *locals, IoMessage *m)
288 { return IONUMBER(Sound_playLength(DATA(self)->sound)); }
290 /* --- pixelsPerSecond --- */
292 IoObject *IoAudioTrack_setPixelsPerVolume(IoAudioTrack *self, IoObject *locals, IoMessage *m)
294 Sound_pixelsPerVolume_(DATA(self)->sound, IoMessage_locals_intArgAt_(m, locals, 0));
295 return self;
298 IoObject *IoAudioTrack_pixelsPerVolume(IoAudioTrack *self, IoObject *locals, IoMessage *m)
299 { return IONUMBER(Sound_pixelsPerVolume(DATA(self)->sound)); }
301 IoObject *IoAudioTrack_pixelsPerSecond(IoAudioTrack *self, IoObject *locals, IoMessage *m)
302 { return IONUMBER(Sound_pixelsPerSecond(DATA(self)->sound)); }
304 /* --- volumes --- */
306 IoObject *IoAudioTrack_setVolumeFromTo(IoAudioTrack *self, IoObject *locals, IoMessage *m)
308 int v = IoMessage_locals_intArgAt_(m, locals, 0);
309 int from = IoMessage_locals_intArgAt_(m, locals, 1);
310 int to = IoMessage_locals_intArgAt_(m, locals, 2);
311 UArray *ba = Sound_volumes(DATA(self)->sound);
312 UArray_setByteWithValue_from_to_(ba, v, from, to);
313 return self;
316 IoObject *IoAudioTrack_volumeAt(IoAudioTrack *self, IoObject *locals, IoMessage *m)
318 int i = IoMessage_locals_intArgAt_(m, locals, 0);
319 UArray *ba = Sound_volumes(DATA(self)->sound);
320 return IONUMBER(UArray_at_(ba, i));
323 IoObject *IoAudioTrack_setSamplesPerVolume(IoAudioTrack *self, IoObject *locals, IoMessage *m)
325 int s = IoMessage_locals_intArgAt_(m, locals, 0);
326 Sound_samplesPerVolume_(DATA(self)->sound, s);
327 return self;
330 IoObject *IoAudioTrack_samplesPerVolume(IoAudioTrack *self, IoObject *locals, IoMessage *m)
332 int s = Sound_samplesPerVolume(DATA(self)->sound);
333 return IONUMBER(s);
336 /* --- pixels per sample --- */
339 /* --- wave form ------------------------------- */
341 IoObject *IoAudioTrack_waveFormDisplayList(IoAudioTrack *self, IoObject *locals, IoMessage *m)
343 unsigned int d = Sound_waveFormDisplayList(DATA(self)->sound);
344 return IONUMBER(d);
347 IoObject *IoAudioTrack_updateWaveFormDisplayList(IoAudioTrack *self, IoObject *locals, IoMessage *m)
349 Sound_updateWaveFormDisplayList(DATA(self)->sound);
350 return self;
353 /* --- volume mask ------------------------------- */
355 IoObject *IoAudioTrack_volumeMaskDisplayList(IoAudioTrack *self, IoObject *locals, IoMessage *m)
357 unsigned int d = Sound_volumeMaskDisplayList(DATA(self)->sound);
358 return IONUMBER(d);
361 IoObject *IoAudioTrack_updateVolumeMaskDisplayList(IoAudioTrack *self, IoObject *locals, IoMessage *m)
363 Sound_updateVolumeMaskDisplayList(DATA(self)->sound);
364 return self;