void breakgen_writefile(t_breakgen *x, t_symbol *s, long argc, t_atom *argv) { t_atom *ap; long num_frames; // t_max_err buferror; float frame; int i; FILE *break_out; float amp = 1.0; short path; char ps[MAX_FILENAME_CHARS] = ""; char finalpath[MAX_PATH_CHARS]; ap = argv; // if (atom_gettype(ap) == A_SYM) { // if (atom_getsym(ap) != gensym("")) { // *ps = atom_getsym(ap); // } // } // strcpy(ps, ""); if (saveas_dialog(ps,&path,NULL)) { return; } path_toabsolutesystempath(path, ps, finalpath); post(finalpath); if (atom_gettype(ap) == A_LONG) { if ((break_out = fopen(finalpath, "w"))) { post("file opened"); num_frames = atom_getlong(ap); post("%i", argc); if (argc > 1) { ap++; if ( (atom_gettype(ap) == A_FLOAT) && (atom_getfloat(ap) < 1.0) ) { amp = atom_getfloat(ap); } } for (i = 0; i < num_frames; i++) { frame = ((output_val(x) - 1) * amp) + 1; fprintf(break_out, "%f", frame); if (i + 1 != num_frames) { fprintf(break_out, "\n"); } } fclose(break_out); post("file closed"); } else { post("couldn't create file"); } } else { post("wrong atom type"); } }
static PyObject * encode_binary(PyObject *self, PyObject *args) { PyObject* enc_obj; PyObject* type_args; PyObject* buf; PyObject* ret = NULL; if (!PyArg_ParseTuple(args, "OO", &enc_obj, &type_args)) { return NULL; } buf = PycStringIO->NewOutput(INIT_OUTBUF_SIZE); if (output_val(buf, enc_obj, T_STRUCT, type_args)) { ret = PycStringIO->cgetvalue(buf); } Py_DECREF(buf); return ret; }
void breakgen_writebuf(t_breakgen *x, t_symbol *s, long argc, t_atom *argv) { t_atom *ap; t_atom_long framecount; long num_frames; t_buffer_obj *buf; // need to free this float *bufframe; t_max_err buferror; int i; ap = argv; if (atom_gettype(ap) == A_SYM) { x->buffy = buffer_ref_new((t_object *)x, atom_getsym(ap)); if (buffer_ref_exists(x->buffy) != 0) { buf = buffer_ref_getobject(x->buffy); framecount = buffer_getframecount(buf); ap++; if (atom_gettype(ap) == A_LONG) { num_frames = atom_getlong(ap); if (num_frames < framecount) framecount = num_frames; bufframe = buffer_locksamples(buf); if (bufframe) { for (i = 0; i < framecount; i++) { *bufframe = output_val(x) - 1.0; bufframe++; } } buferror = buffer_setdirty(buf); buffer_unlocksamples(buf); } } else { post("buffer doesn't exist"); }; } }
static bool output_val(PyObject* output, PyObject* value, TType type, PyObject* typeargs) { /* * Refcounting Strategy: * * We assume that elements of the thrift_spec tuple are not going to be * mutated, so we don't ref count those at all. Other than that, we try to * keep a reference to all the user-created objects while we work with them. * output_val assumes that a reference is already held. The *caller* is * responsible for handling references */ switch (type) { case T_BOOL: { int v = PyObject_IsTrue(value); if (v == -1) { return false; } writeByte(output, (int8_t) v); break; } case T_I08: { int32_t val; if (!parse_pyint(value, &val, INT8_MIN, INT8_MAX)) { return false; } writeByte(output, (int8_t) val); break; } case T_I16: { int32_t val; if (!parse_pyint(value, &val, INT16_MIN, INT16_MAX)) { return false; } writeI16(output, (int16_t) val); break; } case T_I32: { int32_t val; if (!parse_pyint(value, &val, INT32_MIN, INT32_MAX)) { return false; } writeI32(output, val); break; } case T_I64: { int64_t nval = PyLong_AsLongLong(value); if (INT_CONV_ERROR_OCCURRED(nval)) { return false; } if (!CHECK_RANGE(nval, INT64_MIN, INT64_MAX)) { PyErr_SetString(PyExc_OverflowError, "int out of range"); return false; } writeI64(output, nval); break; } case T_DOUBLE: { double nval = PyFloat_AsDouble(value); if (nval == -1.0 && PyErr_Occurred()) { return false; } writeDouble(output, nval); break; } case T_STRING: { Py_ssize_t len = 0; if (is_utf8(typeargs) && PyUnicode_Check(value)) value = PyUnicode_AsUTF8String(value); len = PyString_Size(value); if (!check_ssize_t_32(len)) { return false; } writeI32(output, (int32_t) len); PycStringIO->cwrite(output, PyString_AsString(value), (int32_t) len); break; } case T_LIST: case T_SET: { Py_ssize_t len; SetListTypeArgs parsedargs; PyObject *item; PyObject *iterator; if (!parse_set_list_args(&parsedargs, typeargs)) { return false; } len = PyObject_Length(value); if (!check_ssize_t_32(len)) { return false; } writeByte(output, parsedargs.element_type); writeI32(output, (int32_t) len); iterator = PyObject_GetIter(value); if (iterator == NULL) { return false; } while ((item = PyIter_Next(iterator))) { if (!output_val(output, item, parsedargs.element_type, parsedargs.typeargs)) { Py_DECREF(item); Py_DECREF(iterator); return false; } Py_DECREF(item); } Py_DECREF(iterator); if (PyErr_Occurred()) { return false; } break; } case T_MAP: { PyObject *k, *v; Py_ssize_t pos = 0; Py_ssize_t len; MapTypeArgs parsedargs; len = PyDict_Size(value); if (!check_ssize_t_32(len)) { return false; } if (!parse_map_args(&parsedargs, typeargs)) { return false; } writeByte(output, parsedargs.ktag); writeByte(output, parsedargs.vtag); writeI32(output, len); // TODO(bmaurer): should support any mapping, not just dicts while (PyDict_Next(value, &pos, &k, &v)) { // TODO(dreiss): Think hard about whether these INCREFs actually // turn any unsafe scenarios into safe scenarios. Py_INCREF(k); Py_INCREF(v); if (!output_val(output, k, parsedargs.ktag, parsedargs.ktypeargs) || !output_val(output, v, parsedargs.vtag, parsedargs.vtypeargs)) { Py_DECREF(k); Py_DECREF(v); return false; } Py_DECREF(k); Py_DECREF(v); } break; } // TODO(dreiss): Consider breaking this out as a function // the way we did for decode_struct. case T_STRUCT: { StructTypeArgs parsedargs; Py_ssize_t nspec; Py_ssize_t i; if (!parse_struct_args(&parsedargs, typeargs)) { return false; } nspec = PyTuple_Size(parsedargs.spec); if (nspec == -1) { return false; } for (i = 0; i < nspec; i++) { StructItemSpec parsedspec; PyObject* spec_tuple; PyObject* instval = NULL; spec_tuple = PyTuple_GET_ITEM(parsedargs.spec, i); if (spec_tuple == Py_None) { continue; } if (!parse_struct_item_spec (&parsedspec, spec_tuple)) { return false; } instval = PyObject_GetAttr(value, parsedspec.attrname); if (!instval) { return false; } if (instval == Py_None) { Py_DECREF(instval); continue; } writeByte(output, (int8_t) parsedspec.type); writeI16(output, parsedspec.tag); if (!output_val(output, instval, parsedspec.type, parsedspec.typeargs)) { Py_DECREF(instval); return false; } Py_DECREF(instval); } writeByte(output, (int8_t)T_STOP); break; } case T_STOP: case T_VOID: case T_UTF16: case T_UTF8: case T_U64: default: PyErr_SetString(PyExc_TypeError, "Unexpected TType"); return false; } return true; }