// ---------------------------------------------------------------------------
// 
// ------------
bool bXMapMakePoly::process(int msg, void* prm){
	switch(msg){
		case kExtProcessCallFromIntf:
			return(make());
			break;
		case kExtProcessCallWithParams:{
makepoly_prm* p=(makepoly_prm*)prm;
				return(make(p->tp,&p->o,p->sz,p->nb,p->sui,p->justo));
			}
			break;
		case kExtProcessCallWithXMLTree:{
makepoly_prm			p;
char					val[_values_length_max_];
bGenericXMLBaseElement*	elt;
				p.justo=0;
				elt=getelement(1);
				if(!elt){
					return(false);
				}
				elt->getvalue(val);
				p.tp=_gapp->typesMgr()->get(_gapp->typesMgr()->index(val));
				if(!p.tp){
					return(false);
				}
				elt=getelement(2);
				if(!elt){
					return(false);
				}
				elt->getvalue(val);
				p.o.x=atof(val);
				
				elt=getelement(3);
				if(!elt){
					return(false);
				}
				elt->getvalue(val);
				p.o.y=atof(val);
				
				elt=getelement(4);
				if(!elt){
					return(false);
				}
				elt->getvalue(val);
				p.sz=atof(val);
				
				elt=getelement(5);
				if(!elt){
					return(false);
				}
				elt->getvalue(val);
				p.nb=atoi(val);

				elt=getelement(6);
				if(elt){
					elt->getvalue(val);
					p.sui=atoi(val);
				}
				else{
					p.sui=0;
				}
			
				return(make(p.tp,&p.o,p.sz,p.nb,p.sui,p.justo));
			}	
			break;
		default:
			break;
	}
	return(false);
}
Exemplo n.º 2
0
void ifft__fetch(register ifft_susp_type susp, snd_list_type snd_list)
{
    int cnt = 0; /* how many samples computed */
    int togo;
    int n;
    sample_block_type out;
    register sample_block_values_type out_ptr;

    register sample_block_values_type out_ptr_reg;

    register long index_reg;
    register sample_type * outbuf_reg;
    falloc_sample_block(out, "ifft__fetch");
    out_ptr = out->samples;
    snd_list->block = out;

    while (cnt < max_sample_block_len) { /* outer loop */
	/* first compute how many samples to generate in inner loop: */
	/* don't overflow the output sample block: */
	togo = max_sample_block_len - cnt;


        if (susp->src == NULL) {
out:        togo = 0;   /* indicate termination */
            break;      /* we're done */
        }
        if (susp->index >= susp->stepsize) {
            long i;
            long m, n;
            LVAL elem;
            susp->index = 0;
            susp->array = 
                xleval(cons(s_send, cons(susp->src, consa(s_next))));
            if (susp->array == NULL) {
                susp->src = NULL;
                goto out;
            } else if (!vectorp(susp->array)) {
                xlerror("array expected", susp->array);
            } else if (susp->samples == NULL) {
                /* assume arrays are all the same size as first one;
                   now that we know the size, we just have to do this
                   first allocation.
                 */
                susp->length = getsize(susp->array);
                if (susp->length < 1) 
                    xlerror("array has no elements", susp->array);
                if (susp->window && (susp->window_len != susp->length))
                    xlerror("window size and spectrum size differ", 
                            susp->array);
                /* tricky non-power of 2 detector: only if this is a
                 * power of 2 will the highest 1 bit be cleared when
                 * we subtract 1 ...
                 */
                if (susp->length & (susp->length - 1))
                    xlfail("spectrum size must be a power of 2");
                susp->samples = (sample_type *) calloc(susp->length,
                                                       sizeof(sample_type));
                susp->outbuf = (sample_type *) calloc(susp->length, 
                                                      sizeof(sample_type));
            } else if (getsize(susp->array) != susp->length) {
                xlerror("arrays must all be the same length", susp->array);
            }

            /* at this point, we have a new array to put samples */
            /* the incoming array format is [DC, R1, I1, R2, I2, ... RN]
             * where RN is the real coef at the Nyquist frequency
             * but susp->samples should be organized as [DC, RN, R1, I1, ...]
             */
            n = susp->length;
            /* get the DC (real) coef */
            elem = getelement(susp->array, 0);
            MUST_BE_FLONUM(elem)
            susp->samples[0] = (sample_type) getflonum(elem);

            /* get the Nyquist (real) coef */
            elem = getelement(susp->array, n - 1);
            MUST_BE_FLONUM(elem);
            susp->samples[1] = (sample_type) getflonum(elem);

            /* get the remaining coef */
            for (i = 1; i < n - 1; i++) {
                elem = getelement(susp->array, i);
                MUST_BE_FLONUM(elem)
                susp->samples[i + 1] = (sample_type) getflonum(elem);
            }
            susp->array = NULL; /* free the array */

            /* here is where the IFFT and windowing should take place */
            //fftnf(1, &n, susp->samples, susp->samples + n, -1, 1.0);
            m = round(log(n) / M_LN2);
            if (!fftInit(m)) riffts(susp->samples, m, 1);
            else xlfail("FFT initialization error");
            if (susp->window) {
                n = susp->length;
                for (i = 0; i < n; i++) {
                    susp->samples[i] *= susp->window[i];
                }
            }

            /* shift the outbuf */
            n = susp->length - susp->stepsize;
            for (i = 0; i < n; i++) {
                susp->outbuf[i] = susp->outbuf[i + susp->stepsize];
            }

            /* clear end of outbuf */
            for (i = n; i < susp->length; i++) {
                susp->outbuf[i] = 0;
            }

            /* add in the ifft result */
            n = susp->length;
            for (i = 0; i < n; i++) {
                susp->outbuf[i] += susp->samples[i];
            }
        }
        togo = min(togo, susp->stepsize - susp->index);

	n = togo;
	index_reg = susp->index;
	outbuf_reg = susp->outbuf;
	out_ptr_reg = out_ptr;
	if (n) do { /* the inner sample computation loop */
*out_ptr_reg++ = outbuf_reg[index_reg++];;
	} while (--n); /* inner loop */

	susp->index = index_reg;
	susp->outbuf = outbuf_reg;
	out_ptr += togo;
	cnt += togo;
    } /* outer loop */

    /* test for termination */
    if (togo == 0 && cnt == 0) {
	snd_list_terminate(snd_list);
    } else {
	snd_list->block_len = cnt;
	susp->susp.current += cnt;
    }
} /* ifft__fetch */
// ---------------------------------------------------------------------------
//
// ------------
bool bXMapNetClean::process(int msg, void* prm){
_bTrace_("bXMapNetClean::process",true);
netcleancfg_prm*	cp;
netcleanact_prm*	ap;
bool				b;
    
    switch(msg){
        case kExtProcessCallFromIntf:
            _silent=false;
            process_network(true);
            break;
        case kExtProcessCallWithParams:
            ap=(netcleanact_prm*)prm;
            _silent=false;
            process_network(false);
            break;
        case kExtProcessCallWithXMLTree:{
            if(countelements()==6){
                init_data();

char                    val[_values_length_max_];
bGenericXMLBaseElement*	elt;
                elt=getelement(1);
                elt->getvalue(val);
                _act_prm.create_nodes=atoi(val);

                if(_act_prm.create_nodes){
                    elt=getelement(2);
                    elt->getvalue(val);
                    _act_prm.cut_edges=atoi(val);
                }
                else{
                    _act_prm.cut_edges=false;
                }

                elt=getelement(3);
                elt->getvalue(val);
                _act_prm.join_on_nodes=atoi(val);

                elt=getelement(4);
                elt->getvalue(val);
                _act_prm.join_on_edges=atoi(val);
                if(_act_prm.join_on_nodes&&_act_prm.join_on_edges){
                    _act_prm.join_on_edges=false;
                }
                
                elt=getelement(5);
                elt->getvalue(val);
                _act_prm.check_nodes=atoi(val);

                elt=getelement(6);
                elt->getvalue(val);
                _act_prm.check_edges=atoi(val);

                _silent=true;
                process_network(false);
                
                _nodes.reset();
                _edges.reset();
            }
            else{
                return(false);
            }
            }
            break;
        case kExtProcessCallGetData:
_tm_("kExtProcessCallGetData");
            cp=(netcleancfg_prm*)prm;
            if(cp->nodes){
                cp->nodes->reset();
                b=(*cp->nodes)+_nodes;
            }
            if(cp->edges){
                cp->edges->reset();
                b=(*cp->edges)+_edges;
            }
            cp->tnod=_cfg_prm.tnod;
            cp->stnod=_cfg_prm.stnod;
            strcpy(cp->name,_cfg_prm.name);
            cp->tbdg=_cfg_prm.tbdg;
            cp->stbdg=_cfg_prm.stbdg;
            cp->dnod=_cfg_prm.dnod;
            cp->dbdg=_cfg_prm.dbdg;
            cp->autoclean=_cfg_prm.autoclean;
            break;
        case kExtProcessCallSetData:
_tm_("kExtProcessCallSetData");
            cp=(netcleancfg_prm*)prm;
            if(cp->nodes){
                _nodes.reset();
                b=_nodes+(*cp->nodes);
            }
            if(cp->edges){
                _edges.reset();
                b=_edges+(*cp->edges);
            }
            _cfg_prm.tnod=cp->tnod;
            _cfg_prm.stnod=cp->stnod;
            strcpy(_cfg_prm.name,cp->name);
            _cfg_prm.tbdg=cp->tbdg;
            _cfg_prm.stbdg=cp->stbdg;
            _cfg_prm.dnod=cp->dnod;
            _cfg_prm.dbdg=cp->dbdg;
            _cfg_prm.autoclean=cp->autoclean;
            write_p();
            break;
        default:
            return(false);
    }
    return(true);
}
Exemplo n.º 4
0
double sound_overwrite(
  LVAL snd_expr,
  long n,
  unsigned char *filename,
  double offset_secs,
  long format,
  long mode,
  long bits,
  long swap,
  double *duration)
{
    LVAL result;       // the SOUND to be evaluated
    SF_INFO sf_info;   // info about the sound file
    double max_sample; // return value
    long ntotal;       // how many samples were overwritten
    /*
    long flags;
    */
    // first check if sound file exists, do not create new file
    FILE *file = NULL;
    if (ok_to_open((char *) filename, "rb"))
        file = fopen((char *) filename, "rb");
    // if not then fail
    if (!file) {
        *duration = 0;
        return 0.0;
    } else {
        fclose(file);
    }
    memset(&sf_info, 0, sizeof(sf_info));
    sf_info.format = lookup_format(format, mode, bits, swap);
    result = xleval(snd_expr);
    /* BE CAREFUL - DO NOT ALLOW GC TO RUN WHILE RESULT IS UNPROTECTED */
    if (vectorp(result)) {
        SNDFILE *sndfile;  // opened sound file 
        float *buf; // buffer for samples read in from sound file
        /* make sure all elements are of type a_sound */
        long i = getsize(result);
        long channels = i;
        while (i > 0) {
            i--;
            if (!exttypep(getelement(result, i), a_sound)) {
                xlerror("sound_save: array has non-sound element",
                         result);
            }
        }
        sndfile = open_for_write(filename, SFM_RDWR, format, &sf_info, channels,
                                 ROUND(getsound(getelement(result, 0))->sr),
                                 offset_secs, &buf);

        max_sample = sound_save_array(result, n, &sf_info, sndfile, 
                                      buf, &ntotal, NULL);
        *duration = ntotal / (double) sf_info.samplerate;
        free(buf);
        sf_close(sndfile);
    } else if (exttypep(result, a_sound)) {
        SNDFILE *sndfile;  // opened sound file 
        float *buf; // buffer for samples read in from sound file
        sndfile = open_for_write(filename, SFM_RDWR, format, &sf_info, 1, 
                                 ROUND(getsound(result)->sr), 
                                 offset_secs, &buf);
        max_sample = sound_save_sound(result, n, &sf_info, sndfile, buf, 
                                      &ntotal, NULL);
        *duration = ntotal / (double) sf_info.samplerate;
        free(buf);
        sf_close(sndfile);
    } else {
        xlerror("sound_save: expression did not return a sound",
                 result);
        max_sample = 0.0;
    }
    return max_sample;
}
Exemplo n.º 5
0
sample_type sound_save_array(LVAL sa, long n, SF_INFO *sf_info, 
        SNDFILE *sndfile, float *buf, long *ntotal, PaStream *audio_stream)
{
    long i, chans;
    float *float_bufp;
    sound_state_type state;
    double start_time = HUGE_VAL;
    LVAL sa_copy;
    long debug_unit;    /* print messages at intervals of this many samples */
    long debug_count;   /* next point at which to print a message */
    sample_type max_sample = 0.0F;
    sample_type threshold = 0.0F;
    /*    cvtfn_type cvtfn; jlh */

    *ntotal = 0;

    /* THE ALGORITHM: first merge floating point samples from N channels
     * into consecutive multi-channel frames in buf.  Then, treat buf
     * as just one channel and use one of the cvt_to_* functions to
     * convert the data IN PLACE in the buffer (this is ok because the
     * converted data will never take more space than the original 32-bit
     * floats, so the converted data will not overwrite any floats before
     * the floats are converted
     */

    /* if snd_expr was simply a symbol, then sa now points to
        a shared sound_node.  If we read samples from it, then
        the sounds bound to the symbol will be destroyed, so
        copy it first.  If snd_expr was a real expression that
        computed a new value, then the next garbage collection
        will reclaim the sound array.  See also sound_save_sound()
    */

    chans = getsize(sa);
    if (chans > MAX_SND_CHANNELS) {
        xlerror("sound_save: too many channels", sa);
        free(buf);
        sf_close(sndfile);
    }
    xlprot1(sa);
    sa_copy = newvector(chans);
    xlprot1(sa_copy);

    /* Why do we copy the array into an xlisp array instead of just
     * the state[i] array? Because some of these sounds may reference
     * the lisp heap. We must put the sounds in an xlisp array so that
     * the gc will find and mark them. xlprot1(sa_copy) makes the array
     * visible to gc.
     */
    for (i = 0; i < chans; i++) {
        sound_type s = getsound(getelement(sa, i));
        setelement(sa_copy, i, cvsound(sound_copy(s)));
    }
    sa = sa_copy;	/* destroy original reference to allow GC */

    state = (sound_state_type) malloc(sizeof(sound_state_node) * chans);
    for (i = 0; i < chans; i++) {
        state[i].sound = getsound(getelement(sa, i));
        state[i].scale = state[i].sound->scale;
D       nyquist_printf("save scale factor %ld = %g\n", i, state[i].scale);
        state[i].terminated = false;
        state[i].cnt = 0;   /* force a fetch */
        start_time = min(start_time, state[i].sound->t0);
    }

    for (i = 0; i < chans; i++) {
        if (state[i].sound->t0 > start_time)
            sound_prepend_zeros(state[i].sound, start_time);
    }

    debug_unit = debug_count = (long) max(sf_info->samplerate, 10000.0);

    sound_frames = 0;
    sound_srate = sf_info->samplerate;
    while (n > 0) {
        /* keep the following information for each sound:
            has it terminated?
            pointer to samples
            number of samples remaining in block
           scan to find the minimum remaining samples and
           output that many in an inner loop.  Stop outer
           loop if all sounds have terminated
         */
        int terminated = true;
        int togo = n;
        int j;

        oscheck();

        for (i = 0; i < chans; i++) {
            if (state[i].cnt == 0) {
                if (sndwrite_trace) {
                    nyquist_printf("CALLING SOUND_GET_NEXT ON CHANNEL %ld (%lx)\n",
				   i, (unsigned long) state[i].sound); /* jlh 64 bit issue */
                    sound_print_tree(state[i].sound);
                }
                state[i].ptr = sound_get_next(state[i].sound,
                                   &(state[i].cnt))->samples;
                if (sndwrite_trace) {
                    nyquist_printf("RETURNED FROM CALL TO SOUND_GET_NEXT ON CHANNEL %ld\n", i);
                }
                if (state[i].ptr == zero_block->samples) {
                    state[i].terminated = true;
                }
            }
            if (!state[i].terminated) terminated = false;
            togo = min(togo, state[i].cnt);
        }

        if (terminated) break;

        float_bufp = (float *) buf;
        if (is_pcm(sf_info)) {
            for (j = 0; j < togo; j++) {
                for (i = 0; i < chans; i++) {
                    float s = (float) (*(state[i].ptr++) * (float) state[i].scale);
                    COMPUTE_MAXIMUM_AND_WRAP(s);
                    *float_bufp++ = s;
                }
            }
        } else {
            for (j = 0; j < togo; j++) {
                for (i = 0; i < chans; i++) {
                    float s = (float) (*(state[i].ptr++) * (float) state[i].scale);
                    COMPUTE_MAXIMUM();
                    *float_bufp++ = s;
                }
            }
        }
        /* Here we have interleaved floats. Before converting to the sound
           file format, call PortAudio to play them. */
        if (audio_stream) {
            PaError err = Pa_WriteStream(audio_stream, buf, togo);
            if (err) {
                printf("Pa_WriteStream error %d\n", err);
            }
            sound_frames += togo;
        }
        if (sndfile) sf_writef_float(sndfile, buf, togo);

        n -= togo;
        for (i = 0; i < chans; i++) {
            state[i].cnt -= togo;
        }
        *ntotal += togo;
        if (*ntotal > debug_count) {
            gprintf(TRANS, " %ld ", *ntotal);
            fflush(stdout);
            debug_count += debug_unit;
        }
    }
    gprintf(TRANS, "total samples: %ld x %ld channels\n",
           *ntotal, chans);

    /* references to sounds are shared by sa_copy and state[].
     * here, we dispose of state[], allowing GC to do the
     * sound_unref call that frees the sounds. (Freeing them now
     * would be a bug.)
     */
    free(state);
    xlpop();
    return max_sample;
}
Exemplo n.º 6
0
sample_type sound_save_array(LVAL sa, long n, snd_type snd, 
                             char *buf, long *ntotal, snd_type player)
{
    long i, chans;
    long buflen;
    sound_state_type state;
    double start_time = HUGE_VAL;
    float *float_bufp;
    LVAL sa_copy;
    long debug_unit;    /* print messages at intervals of this many samples */
    long debug_count;   /* next point at which to print a message */
    sample_type max_sample = 0.0F;
    cvtfn_type cvtfn;

    *ntotal = 0;

    /* THE ALGORITHM: first merge floating point samples from N channels
     * into consecutive multi-channel frames in buf.  Then, treat buf
     * as just one channel and use one of the cvt_to_* functions to
     * convert the data IN PLACE in the buffer (this is ok because the
     * converted data will never take more space than the original 32-bit
     * floats, so the converted data will not overwrite any floats before
     * the floats are converted
     */

    /* if snd_expr was simply a symbol, then sa now points to
        a shared sound_node.  If we read samples from it, then
        the sounds bound to the symbol will be destroyed, so
        copy it first.  If snd_expr was a real expression that
        computed a new value, then the next garbage collection
        will reclaim the sound array.  See also sound_save_sound()
    */
    chans = getsize(sa);
    if (chans > MAX_SND_CHANNELS) {
        xlerror("sound_save: too many channels", sa);
        free(buf);
        snd_close(snd);
    }
    xlprot1(sa);
    sa_copy = newvector(chans);
    xlprot1(sa_copy);

    /* Why do we copy the array into an xlisp array instead of just
     * the state[i] array? Because some of these sounds may reference
     * the lisp heap. We must put the sounds in an xlisp array so that
     * the gc will find and mark them. xlprot1(sa_copy) makes the array
     * visible to gc.
     */
    for (i = 0; i < chans; i++) {
        sound_type s = getsound(getelement(sa, i));
        setelement(sa_copy, i, cvsound(sound_copy(s)));
    }
    sa = sa_copy;	/* destroy original reference to allow GC */

    state = (sound_state_type) malloc(sizeof(sound_state_node) * chans);
    for (i = 0; i < chans; i++) {
        state[i].sound = getsound(getelement(sa, i));
        state[i].scale = state[i].sound->scale;
D       nyquist_printf("save scale factor %d = %g\n", (int)i, state[i].scale);
        state[i].terminated = false;
        state[i].cnt = 0;   /* force a fetch */
        start_time = min(start_time, state[i].sound->t0);
    }

    for (i = 0; i < chans; i++) {
        if (state[i].sound->t0 > start_time)
            sound_prepend_zeros(state[i].sound, start_time);
    }

    /* for debugging */
/*    printing_this_sound = s;*/

    cvtfn = find_cvt_to_fn(snd, buf);

#ifdef MACINTOSH
    if (player) {
        gprintf(TRANS, "Playing audio: Click and hold mouse button to stop playback.\n");
    }
#endif

    debug_unit = debug_count = (long) max(snd->format.srate, 10000.0);

    while (n > 0) {
        /* keep the following information for each sound:
            has it terminated?
            pointer to samples
            number of samples remaining in block
           scan to find the minimum remaining samples and
           output that many in an inner loop.  Stop outer
           loop if all sounds have terminated
         */
        int terminated = true;
        int togo = n;
        int j;
        float peak;

        oscheck();

        for (i = 0; i < chans; i++) {
            if (state[i].cnt == 0) {
                if (sndwrite_trace) {
                    nyquist_printf("CALLING SOUND_GET_NEXT "
                                   "ON CHANNEL %d (%p)\n",
                                   (int)i, state[i].sound);
                    sound_print_tree(state[i].sound);
                }
                state[i].ptr = sound_get_next(state[i].sound,
                                   &(state[i].cnt))->samples;
                if (sndwrite_trace) {
                    nyquist_printf("RETURNED FROM CALL TO SOUND_GET_NEXT "
                                   "ON CHANNEL %d\n", (int)i);
                }
                if (state[i].ptr == zero_block->samples) {
                    state[i].terminated = true;
                }
            }
            if (!state[i].terminated) terminated = false;
            togo = min(togo, state[i].cnt);
        }

        if (terminated) break;

        float_bufp = (float *) buf;
        for (j = 0; j < togo; j++) {
            for (i = 0; i < chans; i++) {
                double s = *(state[i].ptr++) * state[i].scale; 
                *float_bufp++ = (float) s;
            }
        }
        // we're treating sound as mono for the conversion, so multiply
        // togo by chans to get proper number of samples, and divide by
        // chans to convert back to frame count required by snd_write
        buflen = (*cvtfn)((void *) buf, (void *) buf, togo * chans, 1.0F, 
                          &peak) / chans;
        if (peak > max_sample) max_sample = peak;
#ifdef MACINTOSH
        if (Button()) {
            if (player) {
                snd_reset(player);
            }
            gprintf(TRANS, "\n\nStopping playback...\n\n\n");
            break;
        }
#endif

        if (snd->u.file.file != -1) snd_write(snd, (void *) buf, buflen);
        if (player) write_to_audio(player, (void *) buf, buflen);

        n -= togo;
        for (i = 0; i < chans; i++) {
            state[i].cnt -= togo;
        }
        *ntotal += togo;
        if (*ntotal > debug_count) {
            gprintf(TRANS, " %d ", *ntotal);
            fflush(stdout);
            debug_count += debug_unit;
        }
    }
    gprintf(TRANS, "total samples: %d x %d channels\n",
           *ntotal, chans);

    /* references to sounds are shared by sa_copy and state[].
     * here, we dispose of state[], allowing GC to do the
     * sound_unref call that frees the sounds. (Freeing them now
     * would be a bug.)
     */
    free(state);
    xlpop();
    return max_sample;
}
Exemplo n.º 7
0
double sound_save(
  LVAL snd_expr,
  long n,
  unsigned char *filename,
  long format,
  long mode,
  long bits,
  long swap,
  double *sr,
  long *nchans,
  double *duration,
  LVAL play)
{
    LVAL result;
    float *buf;
    long ntotal;
    double max_sample;
    SNDFILE *sndfile = NULL;
    SF_INFO sf_info;
    PaStream *audio_stream = NULL;
    if (SAFE_NYQUIST) play = FALSE;
    
    gc();
    
    memset(&sf_info, 0, sizeof(sf_info));
    sf_info.format = lookup_format(format, mode, bits, swap);

    result = xleval(snd_expr);
    /* BE CAREFUL - DO NOT ALLOW GC TO RUN WHILE RESULT IS UNPROTECTED */
    if (vectorp(result)) {
        /* make sure all elements are of type a_sound */
        long i = getsize(result);
        *nchans = sf_info.channels = i;
        while (i > 0) {
            i--;
            if (!exttypep(getelement(result, i), a_sound)) {
                xlerror("sound_save: array has non-sound element",
                         result);
            }
        }
        /* assume all are the same: */
        *sr = sf_info.samplerate = ROUND(getsound(getelement(result, 0))->sr); 

        /* note: if filename is "", then don't write file; therefore,
         * write the file if (filename[0])
         */ 
        if (filename[0]) {
            sndfile = NULL;
            if (ok_to_open((char *) filename, "wb"))
                sndfile = sf_open((char *) filename, SFM_WRITE, &sf_info);
            if (sndfile) {
                /* use proper scale factor: 8000 vs 7FFF */
                sf_command(sndfile, SFC_SET_CLIPPING, NULL, SF_TRUE);
            }
        }
        
        if (play) 
            play = prepare_audio(play, &sf_info, &audio_stream);

        if ((buf = (float *) malloc(max_sample_block_len * sf_info.channels *
                                    sizeof(float))) == NULL) {
            xlabort("snd_save -- couldn't allocate memory");
        }

        max_sample = sound_save_array(result, n, &sf_info, sndfile, 
                                      buf, &ntotal, audio_stream);
        *duration = ntotal / *sr;
        if (sndfile) sf_close(sndfile);
        if (play != NIL) finish_audio(audio_stream);
    } else if (exttypep(result, a_sound)) {
        *nchans = sf_info.channels = 1;
        sf_info.samplerate = ROUND((getsound(result))->sr);
        *sr = sf_info.samplerate;
        if (filename[0]) {
            sndfile = NULL;
            if (ok_to_open((char *) filename, "wb")) {
                sndfile = sf_open((char *) filename, SFM_WRITE, &sf_info);
                if (sndfile) {
                    /* use proper scale factor: 8000 vs 7FFF */
                    sf_command(sndfile, SFC_SET_CLIPPING, NULL, SF_TRUE);
                } else {
                    char error[240];
                    sprintf(error, "snd_save -- %s", sf_error_number(sf_error(sndfile)));
                    xlabort(error);
                }
            } else {
                xlabort("snd_save -- write not permitted by -W option");
            }
        }
        if (play)
            play = prepare_audio(play, &sf_info, &audio_stream);

        if ((buf = (float *) malloc(max_sample_block_len * 
                                    sizeof(float))) == NULL) {
            xlabort("snd_save -- couldn't allocate memory");
        }

        max_sample = sound_save_sound(result, n, &sf_info, sndfile,
                                      buf, &ntotal, audio_stream);
        *duration = ntotal / *sr;
        if (sndfile) sf_close(sndfile);
        if (play != NIL) finish_audio(audio_stream);
    } else {
        xlerror("sound_save: expression did not return a sound",
                 result);
        max_sample = 0.0;
    }
    free(buf);
    return max_sample;
}
Exemplo n.º 8
0
double sound_overwrite(
  LVAL snd_expr,
  long n,
  unsigned char *filename,
  long byte_offset,
  long header,
  long mode,
  long bits,
  long swap,
  double sr,
  long nchans,
  double *duration)
{
    LVAL result;
    char *buf;
    char error[140];
    long ntotal;
    double max_sample;
    snd_node snd;
    long flags;

    snd.device = SND_DEVICE_FILE;
    snd.write_flag = SND_OVERWRITE;
    strcpy(snd.u.file.filename, (char *) filename);
    snd.u.file.header = header;
    snd.u.file.byte_offset = byte_offset;
    snd.format.channels = nchans;
    snd.format.mode = mode;
    snd.format.bits = bits;
    snd.u.file.swap = swap;
    snd.format.srate = sr;

    if ((buf = (char *) malloc(max_sample_block_len * MAX_SND_CHANNELS *
                                  sizeof(float))) == NULL) {
        xlabort("snd_overwrite: couldn't allocate memory");
    }

    if (snd_open(&snd, &flags) != SND_SUCCESS) {
        sprintf(error,
                "snd_overwrite: cannot open file %s and seek to %d", 
                filename, (int)byte_offset);
        free(buf);
        xlabort(error);
    }

    result = xleval(snd_expr);
    /* BE CAREFUL - DO NOT ALLOW GC TO RUN WHILE RESULT IS UNPROTECTED */
    if (vectorp(result)) {
        /* make sure all elements are of type a_sound */
        long i = getsize(result);
        if (nchans != i) {
            sprintf(error, "%s%d%s%d%s", 
                    "snd_overwrite: number of channels in sound (",
                    (int)i,
                    ") does not match\n    number of channels in file (",
                    (int)nchans,
                    ")");
            free(buf);
            snd_close(&snd);
            xlabort(error);
        }
        while (i > 0) {
            i--;
            if (!exttypep(getelement(result, i), a_sound)) {
                free(buf);
                snd_close(&snd);
                xlerror("sound_save: array has non-sound element",
                         result);
            }
        }
        /* assume all are the same: */
        if (sr != getsound(getelement(result, 0))->sr) {
            sprintf(error, "%s%g%s%g%s",
                    "snd_overwrite: sample rate in sound (",
                    getsound(getelement(result, 0))->sr,
                    ") does not match\n    sample rate in file (",
                    sr,
                    ")"); 
            free(buf);
            snd_close(&snd);
            xlabort(error);
        }
        
        max_sample = sound_save_array(result, n, &snd, buf, &ntotal, NULL);
        *duration = ntotal / sr;
    } else if (exttypep(result, a_sound)) {
        if (nchans != 1) {
            sprintf(error, "%s%s%d%s", 
                    "snd_overwrite: number of channels in sound (1",
                    ") does not match\n    number of channels in file (",
                    (int)nchans,
                    ")");
            free(buf);
            snd_close(&snd);
            xlabort(error);
        }
            
        if (sr != getsound(result)->sr) {
            sprintf(error, "%s%g%s%g%s",
                    "snd_overwrite: sample rate in sound (",
                    getsound(result)->sr,
                    ") does not match\n    sample rate in file (",
                    sr,
                    ")"); 
            free(buf);
            snd_close(&snd);
            xlabort(error);
        }
        
        max_sample = sound_save_sound(result, n, &snd, buf, &ntotal, NULL);
        *duration = ntotal / sr;
    } else {
        free(buf);
        snd_close(&snd);
        xlerror("sound_save: expression did not return a sound",
                 result);
        max_sample = 0.0;
    }
    free(buf);
    snd_close(&snd);
    return max_sample;
}
Exemplo n.º 9
0
double sound_save(
  LVAL snd_expr,
  long n,
  unsigned char *filename,
  long format,
  long mode,
  long bits,
  long swap,
  double *sr,
  long *nchans,
  double *duration,
  LVAL play)
{
    LVAL result;
    char *buf;
    long ntotal;
    double max_sample;
    snd_node snd;
    snd_node player;
    long flags;

    snd.device = SND_DEVICE_FILE;
    snd.write_flag = SND_WRITE;
    strcpy(snd.u.file.filename, (char *) filename);
    snd.u.file.file = -1;	/* this is a marker that snd is unopened */
    snd.u.file.header = format;
    snd.format.mode = mode;
    snd.format.bits = bits;
    snd.u.file.swap = swap;

    player.device = SND_DEVICE_AUDIO;
    player.write_flag = SND_WRITE;
    player.u.audio.devicename[0] = '\0';
    player.u.audio.descriptor = NULL;
    player.u.audio.protocol = SND_COMPUTEAHEAD;
    player.u.audio.latency = 1.0;
    player.u.audio.granularity = 0.0;

    if ((buf = (char *) malloc(max_sample_block_len * MAX_SND_CHANNELS *
                                  sizeof(float))) == NULL) {
        xlabort("snd_save -- couldn't allocate memory");
    }

    result = xleval(snd_expr);
    /* BE CAREFUL - DO NOT ALLOW GC TO RUN WHILE RESULT IS UNPROTECTED */
    if (vectorp(result)) {
        /* make sure all elements are of type a_sound */
        long i = getsize(result);
        *nchans = snd.format.channels = i;
        while (i > 0) {
            i--;
            if (!exttypep(getelement(result, i), a_sound)) {
                xlerror("sound_save: array has non-sound element",
                         result);
            }
        }
        /* assume all are the same: */
        *sr = snd.format.srate = getsound(getelement(result, 0))->sr; 

        /* note: if filename is "", then don't write file; therefore,
         * write the file if (filename[0])
         */ 
        if (filename[0] && snd_open(&snd, &flags) != SND_SUCCESS) {
            xlabort("snd_save -- could not open sound file");
        }
        
        play = prepare_audio(play, &snd, &player);

        max_sample = sound_save_array(result, n, &snd,
                         buf, &ntotal, (play == NIL ? NULL : &player));
        *duration = ntotal / *sr;
        if (filename[0]) snd_close(&snd);
        if (play != NIL) finish_audio(&player);
    } else if (exttypep(result, a_sound)) {
        *nchans = snd.format.channels = 1;
        *sr = snd.format.srate = (getsound(result))->sr;
        if (filename[0] && snd_open(&snd, &flags) != SND_SUCCESS) {
            xlabort("snd_save -- could not open sound file");
        }

        play = prepare_audio(play, &snd, &player);

        max_sample = sound_save_sound(result, n, &snd,
                        buf, &ntotal, (play == NIL ? NULL : &player));
        *duration = ntotal / *sr;
        if (filename[0]) snd_close(&snd);
        if (play != NIL) finish_audio(&player);
    } else {
        xlerror("sound_save: expression did not return a sound",
                 result);
        max_sample = 0.0;
    }
    free(buf);
    return max_sample;
}
// ---------------------------------------------------------------------------
//
// -----------
bool bXMapStringProcessing::process_str(bool dummy){
_bTrace_("bXMapStringProcessing::process_str",true);
strprocess_prm			p;
bGenericXMLBaseElement*	elt;
bGenericXMLBaseElement*	cnt;
bGenericType*			tp;
int						idx;
char					value[_values_length_max_];
bArray					arr(sizeof(int));
    
    p.tp=SelectionIsMonoType(_gapp);
    if(p.tp==0){
_te_("bad selection");
        return(false);
    }
    
    tp=_gapp->typesMgr()->get(p.tp);
    p.arr=&arr;
    
// Fields
    cnt=getelement(1);
    if(!cnt){
_te_("no field container");
        return(false);
    }
    for(long i=1;i<=cnt->countelements();i++){
        elt=cnt->getelement(i);
        elt->getvalue(value);
        idx=tp->fields()->get_index(value);
        if(idx==0){
_te_("bad field "+value);
            return(false);
        }
        arr.add(&idx);
    }
    if(arr.count()==0){
_te_("no field");
        return(false);
    }
// Separator
    elt=getelement(2);
    if(!elt){
_te_("no separator");
        return(false);
    }
    elt->getvalue(p.sep);
// Case
    elt=getelement(3);
    if(!elt){
_te_("no case");
        return(false);
    }
    elt->getvalue(value);
    p.cas=atoi(value);
    if((p.cas<kStrProcessCaseDontChange)||(p.cas>kStrProcessCaseFWUpper)){
_te_("bad case "+p.cas);
        return(false);
    }
// Search
    elt=getelement(4);
    if(!elt){
_te_("no search");
        return(false);
    }
    elt->getvalue(p.srch);
char pat[4]={'<','>','&',0};
    
//    if(strcmp(pat,p.srch)==0){
//        strcpy(p.srch," ");
//    }
_tm_("tst :"+pat[0]+"="+(pat[0]/16)+(pat[0]%16));
_tm_("tst :"+pat[1]+"="+(pat[1]/16)+(pat[1]%16));
_tm_("tst :"+pat[2]+"="+(pat[2]/16)+(pat[2]%16));
    
_tm_("search string :"+p.srch);
// Replace
    elt=getelement(5);
    if(!elt){
_te_("no replacement");
        return(false);
    }
    elt->getvalue(p.repl);
//    if(strcmp(pat,p.repl)==0){
//        strcpy(p.repl," ");
//    }
_tm_("replacement string :"+p.repl);
// Format
    elt=getelement(6);
    if(!elt){
_te_("no format");
        return(false);
    }
    elt->getvalue(p.fmt);
// Fill field
    elt=getelement(7);
    if(!elt){
_te_("no target field");
        return(false);
    }
    elt->getvalue(value);
    p.target=tp->fields()->get_index(value);
    if(p.target==0){
_te_("bad target field "+value);
        return(false);
    }
    
    return(process_str(&p,true));
}
Exemplo n.º 11
0
/* mark - mark all accessible nodes */
void mark(LVAL ptr)
{
    register LVAL this,prev,tmp;
    int type,i,n;

    /*
    if (ptr == test_mark) {
       printf("\n\nFound test_mark\n\n");
       }
    */

    /* initialize */
    prev = NIL;
    this = ptr;

    /* mark this list */
    for (;;) {

        /* descend as far as we can */
        while (!(this->n_flags & MARK))

            /* check cons and symbol nodes */
            if ((type = ntype(this)) == CONS || type == USTREAM) {
                if (tmp = car(this)) {
                    this->n_flags |= MARK|LEFT;
                    rplaca(this,prev);
                }
                else if (tmp = cdr(this)) {
                    this->n_flags |= MARK;
                    rplacd(this,prev);
                }
                else {				/* both sides nil */
                    this->n_flags |= MARK;
                    break;
                }
                prev = this;			/* step down the branch */
                this = tmp;
            }

            /* mark other node types */
            else {
                this->n_flags |= MARK;
                switch (type) {
                case SYMBOL:
                case OBJECT:
                case VECTOR:
                case CLOSURE:
                    for (i = 0, n = getsize(this); --n >= 0; ++i)
                        if (tmp = getelement(this,i))
                            mark(tmp);
                    break;
                case EXTERN:
                    if (getdesc(this)->mark_meth) { (*(getdesc(this)->mark_meth))(getinst(this));
                    }
                }
                break;
            }

        /* backup to a point where we can continue descending */
        for (;;)

            /* make sure there is a previous node */
            if (prev) {
                if (prev->n_flags & LEFT) {	/* came from left side */
                    prev->n_flags &= ~LEFT;
                    tmp = car(prev);
                    rplaca(prev,this);
                    if (this = cdr(prev)) {
                        rplacd(prev,tmp);			
                        break;
                    }
                }
                else {				/* came from right side */
                    tmp = cdr(prev);
                    rplacd(prev,this);
                }
                this = prev;			/* step back up the branch */
                prev = tmp;
            }

            /* no previous node, must be done */
            else
                return;
    }
}
Exemplo n.º 12
0
Arquivo: xldmem.c Projeto: 8l/csolve
/* vmark - mark a vector */
void vmark(NODE *n)
{
    int i;
    for (i = 0; i < getsize(n); ++i)
	mark(getelement(n,i));
}