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; }
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; }
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; }
AbstractVector * Vector_UB32::adjust_vector(INDEX new_capacity, Value initial_element, Value initial_contents) { if (initial_contents != NIL) { // "If INITIAL-CONTENTS is supplied, it is treated as for MAKE-ARRAY. // In this case none of the original contents of array appears in the // resulting array." unsigned int * new_data = (unsigned int *) GC_malloc_atomic(new_capacity * sizeof(unsigned int)); if (listp(initial_contents)) { Value list = initial_contents; for (INDEX i = 0; i < new_capacity; i++) { new_data[i] = check_ub32(car(list)); list = xcdr(list); } } else if (vectorp(initial_contents)) { AbstractVector * v = the_vector(initial_contents); for (INDEX i = 0; i < new_capacity; i++) new_data[i] = check_ub32(v->aref(i)); } else signal_type_error(initial_contents, S_sequence); _data = new_data; } else { if (_data == NULL) { // displaced array _data = (unsigned int *) GC_malloc_atomic(new_capacity * sizeof(unsigned int)); INDEX limit = (_capacity < new_capacity) ? _capacity : new_capacity; for (INDEX i = 0; i < limit; i++) _data[i] = check_ub32(_array->aref(i + _offset)); unsigned int n = check_ub32(initial_element); for (INDEX i = _capacity; i < new_capacity; i++) _data[i] = n; } else if (_capacity != new_capacity) { unsigned int * new_data = (unsigned int *) GC_malloc_atomic(new_capacity * sizeof(unsigned int)); INDEX limit = (_capacity < new_capacity) ? _capacity : new_capacity; for (INDEX i = 0; i < limit; i++) new_data[i] = _data[i]; unsigned int n = check_ub32(initial_element); for (INDEX i = _capacity; i < new_capacity; i++) new_data[i] = n; _data = new_data; } } _capacity = new_capacity; // "The consequences are unspecified if array is adjusted to a size smaller // than its fill pointer without supplying the fill-pointer argument so that // its fill-pointer is properly adjusted in the process." if (_fill_pointer > _capacity) _fill_pointer = _capacity; _array = NULL; _offset = 0; return this; }
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; }