void oedge_alloc_atom_array(t_oedge *x, int n) { if(n + 4 != x->ac){ x->ac = n + 4; x->av = (t_atom *)sysmem_resizeptr(x->av, (n + 4) * sizeof(t_atom)); } }
// this is the Max 6 version of the dsp method -- it registers a function for the signal chain in Max 6, // which operates on 64-bit audio signals. void env_tilde_dsp64(t_sigenv *x, t_object *dsp64, short *count, double samplerate, long maxvectorsize, long flags) { //post("my sample rate is: %f", samplerate); // instead of calling dsp_add(), we send the "dsp_add64" message to the object representing the dsp chain // the dsp_add64 arguments are: // 1: the dsp64 object passed-in by the calling function // 2: a pointer to your object // 3: a pointer to your 64-bit perform method // 4: flags to alter how the signal chain handles your object -- just pass 0 // 5: a generic pointer that you can use to pass any additional data to your perform method object_method(dsp64, gensym("dsp_add64"), x, env_tilde_perform64, 0, NULL); if (x->x_period % maxvectorsize) x->x_realperiod = x->x_period + maxvectorsize - (x->x_period % maxvectorsize); else x->x_realperiod = x->x_period; if (maxvectorsize > x->x_allocforvs) { //void *xx = t_resizebytes_(x->x_buf, // (x->x_npoints + x->x_allocforvs) * sizeof(t_sample), // (x->x_npoints + sp[0]->s_n) * sizeof(t_sample)); if (!(x->x_buf = sysmem_resizeptr(x->x_buf, (x->x_npoints + maxvectorsize) * sizeof(t_sample)))) { error("env~: out of memory"); return; } //x->x_buf = (t_sample *)xx; x->x_allocforvs = maxvectorsize; } }
// this function is called when the DAC is enabled, and "registers" a function for the signal chain in Max 5 and earlier. // In this case we register the 32-bit, "bfcc_perform" method. void env_tilde_dsp(t_sigenv *x, t_signal **sp, short *count) { //post("my sample rate is: %f", sp[0]->s_sr); // dsp_add // 1: (t_perfroutine p) perform method // 2: (long argc) number of args to your perform method // 3...: argc additional arguments, all must be sizeof(pointer) or long // these can be whatever, so you might want to include your object pointer in there // so that you have access to the info, if you need it. dsp_add(env_tilde_perform, 4, x, sp[0]->s_vec, sp[1]->s_vec, sp[0]->s_n); if (x->x_period % sp[0]->s_n) x->x_realperiod = x->x_period + sp[0]->s_n - (x->x_period % sp[0]->s_n); else x->x_realperiod = x->x_period; if (sp[0]->s_n > x->x_allocforvs) { //void *xx = t_resizebytes_(x->x_buf, // (x->x_npoints + x->x_allocforvs) * sizeof(t_sample), // (x->x_npoints + sp[0]->s_n) * sizeof(t_sample)); if (!(x->x_buf = sysmem_resizeptr(x->x_buf, (x->x_npoints + sp[0]->s_n) * sizeof(t_sample)))) { error("env~: out of memory"); return; } //x->x_buf = (t_sample *)xx; x->x_allocforvs = sp[0]->s_n; } }
int memalloc(t_myObj *x, int n) { if(x->infilt) x->infilt = (double*)sysmem_resizeptr(x->infilt, (n+2)*sizeof(double)); else x->infilt = (double *)sysmem_newptrclear((n+2)*sizeof(double)); if(!x->infilt) { object_error((t_object *)x, "memory allocation failed!"); return 1; } if(x->outfilt) x->outfilt = (double *)sysmem_resizeptr(x->outfilt, (n+2)*sizeof(double)); else x->outfilt = (double *)sysmem_newptrclear((n+2)*sizeof(double)); if(!x->outfilt) { object_error((t_object *)x, "memory allocation failed!"); return 1; } x->blocksize = n; return 0; }
void bed_fadeout(t_bed *x, double fadetime) { if (!bed_attach_buffer(x)) { return; } t_buffer *b; b = x->buffer; ATOMIC_INCREMENT(&b->b_inuse); if (!b->b_valid) { ATOMIC_DECREMENT(&b->b_inuse); post("bed • Not a valid buffer!"); return; } long fadeframes = fadetime * 0.001 * b->b_sr; if (fadetime <= 0 || fadeframes > b->b_frames) { post("bed • %.0fms is not a valid fade-out time", fadetime); ATOMIC_DECREMENT(&b->b_inuse); return; } long chunksize = fadeframes * b->b_nchans * sizeof(float); if (x->undo_samples == NULL) { x->undo_samples = (float *)sysmem_newptr(chunksize); } else { x->undo_samples = (float *)sysmem_resizeptr(x->undo_samples, chunksize); } if (x->undo_samples == NULL) { error("bed • Cannot allocate memory for undo"); x->can_undo = 0; ATOMIC_DECREMENT(&b->b_inuse); return; } else { x->can_undo = 1; x->undo_start = b->b_frames - fadeframes; x->undo_frames = fadeframes; x->undo_resize = 0; x->undo_cut = 0; sysmem_copyptr(b->b_samples + x->undo_start, x->undo_samples, chunksize); } for (int ii = (int)x->undo_start; ii < x->undo_start + fadeframes; ii++) { for (int jj = 0; jj < b->b_nchans; jj++) { b->b_samples[(ii * b->b_nchans) + jj] *= 1 - (float)(ii - x->undo_start) / (float)fadeframes; } } object_method(&b->b_obj, gensym("dirty")); ATOMIC_DECREMENT(&b->b_inuse); }
void bed_reverse(t_bed *x) { if (!bed_attach_buffer(x)) { return; } t_buffer *b; b = x->buffer; ATOMIC_INCREMENT(&b->b_inuse); if (!b->b_valid) { ATOMIC_DECREMENT(&b->b_inuse); post("bed • Not a valid buffer!"); return; } long chunksize = b->b_frames * b->b_nchans * sizeof(float); if (x->undo_samples == NULL) { x->undo_samples = (float *)sysmem_newptr(chunksize); } else { x->undo_samples = (float *)sysmem_resizeptr(x->undo_samples, chunksize); } if (x->undo_samples == NULL) { error("bed • Cannot allocate memory for undo"); x->can_undo = 0; ATOMIC_DECREMENT(&b->b_inuse); return; } else { x->can_undo = 1; x->undo_start = 0; x->undo_frames = b->b_frames; x->undo_resize = 0; x->undo_cut = 0; sysmem_copyptr(b->b_samples, x->undo_samples, chunksize); } float temp; for (int ii = 0; ii < ceil(b->b_frames / 2); ii++) { for (int jj = 0; jj < b->b_nchans; jj++) { temp = b->b_samples[(ii * b->b_nchans) + jj]; b->b_samples[(ii * b->b_nchans) + jj] = b->b_samples[(b->b_frames - 1 - ii * b->b_nchans) + jj]; b->b_samples[(b->b_frames - 1 - ii * b->b_nchans) + jj] = temp; } } object_method(&b->b_obj, gensym("dirty")); ATOMIC_DECREMENT(&b->b_inuse); }
void bed_ring_modulation(t_bed *x, double frequency) { if (!bed_attach_buffer(x)) { return; } t_buffer *b; b = x->buffer; ATOMIC_INCREMENT(&b->b_inuse); if (!b->b_valid) { ATOMIC_DECREMENT(&b->b_inuse); post("bed • Not a valid buffer!"); return; } long chunksize = b->b_frames * b->b_nchans * sizeof(float); if (x->undo_samples == NULL) { x->undo_samples = (float *)sysmem_newptr(chunksize); } else { x->undo_samples = (float *)sysmem_resizeptr(x->undo_samples, chunksize); } if (x->undo_samples == NULL) { error("bed • Cannot allocate memory for undo"); x->can_undo = 0; ATOMIC_DECREMENT(&b->b_inuse); return; } else { x->can_undo = 1; x->undo_start = 0; x->undo_frames = b->b_frames; x->undo_resize = 0; x->undo_cut = 0; sysmem_copyptr(b->b_samples, x->undo_samples, chunksize); } float twopi = 8.0 * atan(1.0); float oneoversr = 1.0 / b->b_sr; for (int ii = 0; ii < b->b_frames; ii++) { for (int jj = 0; jj < b->b_nchans; jj++) { b->b_samples[(ii * b->b_nchans) + jj] *= sin(twopi * frequency * ii * oneoversr); } } object_method(&b->b_obj, gensym("dirty")); ATOMIC_DECREMENT(&b->b_inuse); }
int mem_alloc(t_myObj *x, long size) { long i, n; if(size != x->N) { n = size; x->wgauss = (double *)sysmem_resizeptr(x->wgauss, n * sizeof(double)); if(x->wgauss != NULL) { for(i=0; i<n; i++) { // gaussian double sigma = 0.4; x->wgauss[i] = exp( -0.5 * pow( (i-(n-1)*0.5) / (sigma*(n-1)*0.5), 2) ); } return 1; } else object_error((t_object*)x, "out of memory!"); } return 0; }
void omax_class_addToSchemaList(t_class *c, const char *address) { t_symbol *classname = class_nameget(c); if(!classname){ return; } t_hashtab *ht = omax_class_getHashtab(classname->s_name); if(!ht){ return; } t_schemalist *sl = NULL; hashtab_lookup(ht, gensym("cnmat_internal_osc_schema_list"), (t_object **)(&sl)); if(!sl){ sl = (t_schemalist *)sysmem_newptr(sizeof(t_schemalist)); sl->address_buf_len = 64; sl->addresses = (t_charbuffer *)sysmem_newptr(sl->address_buf_len * sizeof(t_charbuffer)); sl->address_buf_pos = 0; sl->nbytes = 0; hashtab_store(ht, gensym("cnmat_internal_osc_schema_list"), (t_object *)sl); } int len = strlen(address); len++; while(len % 4){ len++; } if(sl->address_buf_pos >= sl->address_buf_len){ sl->addresses = (t_charbuffer *)sysmem_resizeptr(sl->addresses, sizeof(t_charbuffer) * (sl->address_buf_len + 16)); if(!sl->addresses){ return; } sl->address_buf_len += 16; } t_charbuffer *cb = sl->addresses + sl->address_buf_pos; cb->buf = (char *)sysmem_newptr(len); memset(cb->buf, '\0', len); strcpy(cb->buf, address); cb->bufpos = len; cb->buflen = len; sl->address_buf_pos++; sl->nbytes += len; }
void bed_shuffle_n_segments(t_bed *x, long segments) { if (!bed_attach_buffer(x)) { return; } t_buffer *b; b = x->buffer; ATOMIC_INCREMENT(&b->b_inuse); if (!b->b_valid) { ATOMIC_DECREMENT(&b->b_inuse); post("bed • Not a valid buffer!"); return; } long chunksize = b->b_frames * b->b_nchans * sizeof(float); if (x->undo_samples == NULL) { x->undo_samples = (float *)sysmem_newptr(chunksize); } else { x->undo_samples = (float *)sysmem_resizeptr(x->undo_samples, chunksize); } if (x->undo_samples == NULL) { error("bed • Cannot allocate memory for undo"); x->can_undo = 0; ATOMIC_DECREMENT(&b->b_inuse); return; } else { x->can_undo = 1; x->undo_start = 0; x->undo_frames = b->b_frames; x->undo_resize = 0; x->undo_cut = 0; sysmem_copyptr(b->b_samples, x->undo_samples, chunksize); } long totallength = b->b_frames; long basesegmentlength = ceil(totallength / segments); long randomsegment; long start; long end; long bufferlength; long buffersize; float *local_buffer = NULL; while (segments > 0) { randomsegment = rand() % segments; start = randomsegment * basesegmentlength; end = start + basesegmentlength; if (end > totallength) { end = totallength; } bufferlength = (end - start); buffersize = bufferlength * b->b_nchans * sizeof(float); if (local_buffer == NULL) { local_buffer = (float *)sysmem_newptr(buffersize); } else { local_buffer = (float *)sysmem_resizeptr(local_buffer, buffersize); } sysmem_copyptr(b->b_samples + (start * b->b_nchans), local_buffer, buffersize); for (long ii = end; ii < totallength; ii++) { for (int jj = 0; jj < b->b_nchans; jj++) { b->b_samples[((ii - bufferlength) * b->b_nchans) + jj] = b->b_samples[(ii * b->b_nchans) + jj]; } } sysmem_copyptr(local_buffer, b->b_samples + (totallength - bufferlength) * b->b_nchans, buffersize); totallength -= bufferlength; segments--; } sysmem_freeptr(local_buffer); object_method(&b->b_obj, gensym("dirty")); ATOMIC_DECREMENT(&b->b_inuse); }
void bed_cut(t_bed *x, double start, double end) { if (!bed_attach_buffer(x)) { return; } t_buffer *b; b = x->buffer; ATOMIC_INCREMENT(&b->b_inuse); if (!b->b_valid) { ATOMIC_DECREMENT(&b->b_inuse); post("bed • Not a valid buffer!"); return; } long startframe = start * 0.001 * b->b_sr; long endframe = end * 0.001 * b->b_sr; long cutframes = endframe - startframe; if (startframe < 0 || endframe > b->b_frames || startframe > endframe) { post("bed • %.0fms and %.0fms are not valid cut times", start, end); ATOMIC_DECREMENT(&b->b_inuse); return; } long chunksize = cutframes * b->b_nchans * sizeof(float); if (x->undo_samples == NULL) { x->undo_samples = (float *)sysmem_newptr(chunksize); } else { x->undo_samples = (float *)sysmem_resizeptr(x->undo_samples, chunksize); } if (x->undo_samples == NULL) { error("bed • Cannot allocate memory for undo"); x->can_undo = 0; ATOMIC_DECREMENT(&b->b_inuse); return; } else { x->can_undo = 1; x->undo_start = startframe; x->undo_frames = cutframes; x->undo_resize = 1; x->undo_cut = 1; sysmem_copyptr(b->b_samples + (startframe * b->b_nchans), x->undo_samples, chunksize); } long bufferframes = b->b_frames; long buffersize = bufferframes * b->b_nchans * sizeof(float); float *local_buffer = (float *)sysmem_newptr(buffersize); if (local_buffer == NULL) { error("bed • Cannot allocate memory for undo"); x->can_undo = 0; ATOMIC_DECREMENT(&b->b_inuse); return; } else { sysmem_copyptr(b->b_samples, local_buffer, buffersize); } ATOMIC_DECREMENT(&b->b_inuse); t_atom rv; object_method_long(&b->b_obj, gensym("sizeinsamps"), (b->b_frames - cutframes), &rv); ATOMIC_INCREMENT(&b->b_inuse); chunksize = startframe * b->b_nchans * sizeof(float); sysmem_copyptr(local_buffer, b->b_samples, chunksize); chunksize = (bufferframes - endframe) * b->b_nchans * sizeof(float); sysmem_copyptr(local_buffer + (endframe * b->b_nchans), b->b_samples + (startframe * b->b_nchans), chunksize); sysmem_freeptr(local_buffer); object_method(&b->b_obj, gensym("dirty")); ATOMIC_DECREMENT(&b->b_inuse); }
void bed_normalize(t_bed *x, t_symbol *msg, short argc, t_atom *argv) { if (argc > 1) { error("bed • The message must have at most two members"); return; } float newmax = 1.0; if (argc == 1) { newmax = atom_getfloat(argv); } if (!bed_attach_buffer(x)) { return; } t_buffer *b; b = x->buffer; ATOMIC_INCREMENT(&b->b_inuse); if (!b->b_valid) { ATOMIC_DECREMENT(&b->b_inuse); post("bed • Not a valid buffer!"); return; } long chunksize = b->b_frames * b->b_nchans * sizeof(float); if (x->undo_samples == NULL) { x->undo_samples = (float *)sysmem_newptr(chunksize); } else { x->undo_samples = (float *)sysmem_resizeptr(x->undo_samples, chunksize); } if (x->undo_samples == NULL) { error("bed • Cannot allocate memory for undo"); x->can_undo = 0; ATOMIC_DECREMENT(&b->b_inuse); return; } else { x->can_undo = 1; x->undo_start = 0; x->undo_frames = b->b_frames; x->undo_resize = 0; x->undo_cut = 0; sysmem_copyptr(b->b_samples, x->undo_samples, chunksize); } float maxamp = 0.0; for (int ii = 0; ii < b->b_frames * b->b_nchans; ii++) { if (maxamp < fabs(b->b_samples[ii])) { maxamp = fabs(b->b_samples[ii]); } } float rescale; if (maxamp > 1e-6) { rescale = newmax / maxamp; } else { post("bed • Amplitude is too low to rescale: %.2f", maxamp); ATOMIC_DECREMENT(&b->b_inuse); return; } for (int ii = 0; ii < b->b_frames * b->b_nchans; ii++) { b->b_samples[ii] *= rescale; } object_method(&b->b_obj, gensym("dirty")); ATOMIC_DECREMENT(&b->b_inuse); }