1 /* copyright: Steve Dekorte, 2002
2 * All rights reserved. See _BSDLicense.txt.
4 * user clones MP3Decoder and implements input and output methods
6 * input should return a buffer, output will have a buffer argument.
9 #include "IoMP3Encoder.h"
10 #include "IoObject_actor.h"
11 #include "base/List.h"
18 #define DATA(self) ((IoMP3EncoderData *)self->data)
20 IoTag
*IoMP3Encoder_newTag(void *state
)
22 IoTag
*tag
= IoTag_newWithName_("MP3Encoder");
23 IoTag_state_(tag
, state
);
24 IoTag_cloneFunc_(tag
, (IoTagCloneFunc
*)IoMP3Encoder_rawClone
);
25 IoTag_markFunc_(tag
, (IoTagMarkFunc
*)IoMP3Encoder_mark
);
26 IoTag_freeFunc_(tag
, (IoTagFreeFunc
*)IoMP3Encoder_free
);
30 IoMP3Encoder
*IoMP3Encoder_proto(void *state
)
32 IoObject
*self
= IoObject_new(state
);
33 IoObject_tag_(self
, IoMP3Encoder_newTag(state
));
35 self
->data
= calloc(1, sizeof(IoMP3EncoderData
));
36 DATA(self
)->outBuffer
= IoBuffer_new(state
);
37 DATA(self
)->encoder
= MP3Encoder_new();
38 MP3Encoder_setExternalOutputUArray_(DATA(self
)->encoder
,
39 IoBuffer_rawUArray(DATA(self
)->outBuffer
));
42 IoState_registerProtoWithFunc_(state
, self
, IoMP3Encoder_proto
);
45 IoMethodTable methodTable
[] = {
46 {"encode", IoMP3Encoder_encode
},
47 {"end", IoMP3Encoder_end
},
48 {"outBuffer", IoMP3Encoder_outBuffer
},
49 {"setBitRate", IoMP3Encoder_setBitRate
},
50 {"setSampleRate", IoMP3Encoder_setSampleRate
},
51 {"setQuality", IoMP3Encoder_setQuality
},
52 {"setCompressionRatio", IoMP3Encoder_setCompressionRatio
},
55 IoObject_addMethodTable_(self
, methodTable
);
61 IoMP3Encoder
*IoMP3Encoder_rawClone(IoMP3Encoder
*proto
)
63 IoObject
*self
= IoObject_rawClonePrimitive(proto
);
64 self
->data
= cpalloc(proto
->data
, sizeof(IoMP3EncoderData
));
65 DATA(self
)->outBuffer
= IoBuffer_new(IOSTATE
);
66 DATA(self
)->encoder
= MP3Encoder_new();
67 MP3Encoder_setExternalOutputUArray_(DATA(self
)->encoder
,
68 IoBuffer_rawUArray(DATA(self
)->outBuffer
));
69 IoState_addValue_(IOSTATE
, self
);
73 IoMP3Encoder
*IoMP3Encoder_new(void *state
)
75 IoObject
*proto
= IoState_protoWithInitFunction_(state
, IoMP3Encoder_proto
);
76 return IOCLONE(proto
);
79 /* ----------------------------------------------------------- */
81 void IoMP3Encoder_free(IoMP3Encoder
*self
)
83 MP3Encoder_free(DATA(self
)->encoder
);
87 void IoMP3Encoder_mark(IoMP3Encoder
*self
)
89 IoObject_makeGrayIfWhite(DATA(self
)->outBuffer
);
92 /* --- Output Parameters -------------------------------------------------------- */
94 IoObject
*IoMP3Encoder_setSampleRate(IoMP3Encoder
*self
, IoObject
*locals
, IoMessage
*m
)
96 int n
= IoMessage_locals_intArgAt_(m
, locals
, 0);
97 MP3Encoder_setSampleRate_(DATA(self
)->encoder
, n
);
98 return IoMP3Encoder_checkError(self
, locals
, m
);
101 IoObject
*IoMP3Encoder_setBitRate(IoMP3Encoder
*self
, IoObject
*locals
, IoMessage
*m
)
103 int n
= IoMessage_locals_intArgAt_(m
, locals
, 0);
104 MP3Encoder_setBitRate_(DATA(self
)->encoder
, n
);
105 return IoMP3Encoder_checkError(self
, locals
, m
);
108 IoObject
*IoMP3Encoder_setQuality(IoMP3Encoder
*self
, IoObject
*locals
, IoMessage
*m
)
110 int n
= IoMessage_locals_intArgAt_(m
, locals
, 0);
111 MP3Encoder_setQuality_(DATA(self
)->encoder
, n
);
112 return IoMP3Encoder_checkError(self
, locals
, m
);
115 IoObject
*IoMP3Encoder_setCompressionRatio(IoMP3Encoder
*self
, IoObject
*locals
, IoMessage
*m
)
117 float f
= IoMessage_locals_doubleArgAt_(m
, locals
, 0);
118 MP3Encoder_setCompressionRatio_(DATA(self
)->encoder
, f
);
119 return IoMP3Encoder_checkError(self
, locals
, m
);
122 /* ----------------------------------------------------------- */
125 IoObject
*IoMP3Encoder_checkError(IoMP3Encoder
*self
, IoObject
*locals
, IoMessage
*m
)
127 char *e
= (char *)MP3Encoder_error(DATA(self
)->encoder
);
131 IoState_error_description_(IOSTATE
, m
, "MP3Encoder", "error: %s", e
);
138 /* ----------------------------------------------------------- */
141 IoObject
*IoMP3Encoder_encode(IoMP3Encoder
*self
, IoObject
*locals
, IoMessage
*m
)
143 UArray
*inBa
= IoBuffer_rawUArray(IoMessage_locals_bufferArgAt_(m
, locals
, 0));
144 /*UArray *outBa = IoBuffer_rawUArray(DATA(self)->outBuffer);*/
147 int end
= UArray_length(inBa
);
149 if (IoMessage_argCount(m
) > 1) start
= IoMessage_locals_intArgAt_(m
, locals
, 1);
150 if (IoMessage_argCount(m
) > 2) end
= IoMessage_locals_intArgAt_(m
, locals
, 2);
152 { IoState_error_description_(IOSTATE
, m
, "MP3Encoder", "range error: start > end"); }
153 if (end
> UArray_length(inBa
))
154 { IoState_error_description_(IOSTATE
, m
, "MP3Encoder", "range error: end > length of input buffer"); }
156 MP3Encoder_encode(DATA(self
)->encoder
, UArray_bytes(inBa
) + start
, end
);
158 return IoMP3Encoder_checkError(self
, locals
, m
);
161 IoObject
*IoMP3Encoder_end(IoMP3Encoder
*self
, IoObject
*locals
, IoMessage
*m
)
163 MP3Encoder_end(DATA(self
)->encoder
);
164 return IoMP3Encoder_checkError(self
, locals
, m
);
167 IoObject
*IoMP3Encoder_outBuffer(IoMP3Encoder
*self
, IoObject
*locals
, IoMessage
*m
)
168 { return DATA(self
)->outBuffer
; }