void AudioFileSndfile::write(const void* buffer, unsigned frames) { if (!_handle || (_mode != ModeWrite && _mode != ModeReadWrite)) { RUNTIME_ERROR("Attempt to write file not opened for writing."); } flushChunks(); sf_count_t count; switch (_info.sampleType) { case Sound::Type::Float32: case Sound::Type::Int24E: count = sf_writef_float(_handle.get(), static_cast<const Sound::Float32*>(buffer), frames); break; case Sound::Type::Float64: count = sf_writef_double(_handle.get(), static_cast<const Sound::Float64*>(buffer), frames); break; case Sound::Type::Int8: count = sf_write_raw(_handle.get(), buffer, frames * _info.channels); break; case Sound::Type::Int16: count = sf_writef_short(_handle.get(), static_cast<const Sound::Int16*>(buffer), frames); break; case Sound::Type::Int32: count = sf_writef_int(_handle.get(), static_cast<const Sound::Int32*>(buffer), frames); break; case Sound::Type::Int64: case Sound::Type::Precise: case Sound::Type::Count: RUNTIME_ERROR("Writing for this sample format is not supported"); } }
/* insert 'sample_count' samples at sample 'insert_pos' * to the soundfile. * 'insert_pos' == -1 means append to end of file. */ int soundfile_insert_samples(long linsert_pos, long lsample_count, int *sample_data, int status_info) { sf_count_t insert_pos = linsert_pos; sf_count_t sample_count = lsample_count; sf_count_t end_pos = sf_seek(sndfile, 0, SEEK_END|SFM_READ); if (insert_pos < 0 || insert_pos > end_pos) insert_pos = end_pos; if (soundfile_shift_samples_right(insert_pos, sample_count, status_info) < 0) { return -1; } /* go to insert position 'insert_pos'... */ if (sf_seek(sndfile, insert_pos, SEEK_SET|SFM_WRITE) < 0) { perr("soundfile_insert_samples: sf_seek write pointer"); warning("Libsndfile reports write pointer seek error in audio file"); return -1; } /* ...and insert new data */ if (sf_writef_int(sndfile, sample_data, sample_count) != sample_count) { perr("soundfile_insert_samples: append with sf_writef_int"); warning("Libsndfile reports write error in audio file"); return -1; } return 0; }
void pcap_callback(u_char* args, const struct pcap_pkthdr* packet_header, const u_char* packet) { unsigned char* test_stream_id; struct ethernet_header* eth_header; struct six1883_sample* sample; uint32_t buf; uint32_t *mybuf; uint32_t frame[2] = { 0 , 0 }; #ifdef DEBUG fprintf(stdout,"Got packet.\n"); #endif eth_header = (struct ethernet_header*)(packet); #ifdef DEBUG fprintf(stdout,"Ether Type: 0x%02x%02x\n", eth_header->type[0], eth_header->type[1]); #endif if (0 == memcmp(ETHER_TYPE,eth_header->type,sizeof(eth_header->type))) { test_stream_id = (unsigned char*)(packet + ETHERNET_HEADER_SIZE + SEVENTEEN22_HEADER_PART1_SIZE); #ifdef DEBUG fprintf(stderr, "Received stream id: %02x%02x%02x%02x%02x%02x%02x%02x\n ", test_stream_id[0], test_stream_id[1], test_stream_id[2], test_stream_id[3], test_stream_id[4], test_stream_id[5], test_stream_id[6], test_stream_id[7]); #endif if (0 == memcmp(test_stream_id, stream_id, sizeof(STREAM_ID_SIZE))) { #ifdef DEBUG fprintf(stdout,"Stream ids matched.\n"); #endif //sample = (struct six1883_sample*) (packet + HEADER_SIZE); mybuf = (uint32_t*) (packet + HEADER_SIZE); for(int i = 0; i < SAMPLES_PER_FRAME * CHANNELS; i += 2) { memcpy(&frame[0], &mybuf[i], sizeof(frame)); frame[0] = ntohl(frame[0]); /* convert to host-byte order */ frame[1] = ntohl(frame[1]); frame[0] &= 0x00ffffff; /* ignore leading label */ frame[1] &= 0x00ffffff; frame[0] <<= 8; /* left-align remaining PCM-24 sample */ frame[1] <<= 8; sf_writef_int(snd_file, frame, 1); } } } }
int main(int argc, char* argv[]) { /* Initialize args*/ float sr, pitch_factor; int *buf, num, f, c, buffer_size, semitones; char inFileName[NAME_SIZE]; // file name without file extension char fileExtension[NAME_SIZE]; // file extension of both in/output char outPath[NAME_SIZE]; // format: "inputName-out.extension" char srcPath[NAME_SIZE]; // assuming input is in sound_files directory SNDFILE *input, *output; SF_INFO info; handleInput(argc, argv, &semitones, inFileName, fileExtension); sprintf(outPath, "sound_files/%s-out_(%d).%s", inFileName, semitones, fileExtension); sprintf(srcPath, "sound_files/%s.%s", inFileName, fileExtension); pitch_factor = pow(2., semitones / 12.); /* Open the WAV file. */ info.format = 0; input = sf_open(srcPath, SFM_READ, &info); if (input == NULL) { printf("Failed to open the file.\n"); exit(-1); } /* Print some of the info */ f = info.frames; sr = info.samplerate; c = info.channels; buffer_size = f * c; printf("Input: samplerate: %0.0lf, channels: %d, buffer_size: %d\n", sr, c, buffer_size); printf("Shifted by a factor of %f\n", pitch_factor); /* Allocate space for the data to be read, then read it. */ buf = (int*)malloc(buffer_size * sizeof(int)); num = sf_readf_int(input, buf, buffer_size); sf_close(input); printf("Read %d samples from %s\n", num, srcPath); // printBuffer(buf, buffer_size); /* Call PitchShift() */ PitchShiftFile(pitch_factor, buffer_size, buf, buf); /* Open sound file for writing */ output = sf_open(outPath, SFM_WRITE, &info); num = sf_writef_int(output, buf, buffer_size); sf_close(output); printf("Wrote %d samples to %s\n", num, outPath); return 0; }
static switch_status_t sndfile_file_write(switch_file_handle_t *handle, void *data, size_t *len) { size_t inlen = *len; sndfile_context *context = handle->private_info; if (switch_test_flag(handle, SWITCH_FILE_DATA_RAW)) { *len = (size_t) sf_write_raw(context->handle, data, inlen); } else if (switch_test_flag(handle, SWITCH_FILE_DATA_INT)) { *len = (size_t) sf_writef_int(context->handle, (int *) data, inlen); } else if (switch_test_flag(handle, SWITCH_FILE_DATA_SHORT)) { *len = (size_t) sf_writef_short(context->handle, (short *) data, inlen); } else if (switch_test_flag(handle, SWITCH_FILE_DATA_FLOAT)) { *len = (size_t) sf_writef_float(context->handle, (float *) data, inlen); } else if (switch_test_flag(handle, SWITCH_FILE_DATA_DOUBLE)) { *len = (size_t) sf_writef_double(context->handle, (double *) data, inlen); } else { *len = (size_t) sf_writef_int(context->handle, (int *) data, inlen); } handle->sample_count += *len; return sf_error(context->handle) == SF_ERR_NO_ERROR ? SWITCH_STATUS_SUCCESS : SWITCH_STATUS_FALSE; }
static void copy_data_int (SNDFILE *outfile, SNDFILE *infile, int channels) { static int data [BUFFER_LEN] ; int frames, readcount ; frames = BUFFER_LEN / channels ; readcount = frames ; while (readcount > 0) { readcount = sf_readf_int (infile, data, frames) ; sf_writef_int (outfile, data, readcount) ; } ; return ; } /* copy_data_int */
int soundfile_load_file(char *filename, long lpos, long lsample_count, int status_info) { sf_count_t pos = lpos; sf_count_t sample_count = lsample_count; int rc = -1; if (sf_seek(sndfile, pos, SEEK_SET|SFM_WRITE) == pos) { int channels = sfinfo.channels; sf_count_t buffer_size = SBW*1000; int *buffer = calloc(channels*buffer_size, sizeof(int)); if (buffer != NULL) { SNDFILE *sndfile_new; SF_INFO sfinfo_new = sfinfo; sfinfo_new.format = 0; sndfile_new = sf_open(filename, SFM_READ, &sfinfo_new); if (sndfile_new != NULL) { sf_count_t processed = 0; sf_count_t read_size; if (sample_count > sfinfo_new.frames) sample_count = sfinfo_new.frames; while (processed < sample_count) { if (buffer_size > sample_count - processed) buffer_size = sample_count - processed; read_size = sf_readf_int(sndfile_new, buffer, buffer_size); if (read_size > 0) sf_writef_int(sndfile, buffer, read_size); processed += buffer_size; if (status_info) update_progress_bar((gfloat)processed/sample_count, PROGRESS_UPDATE_INTERVAL, FALSE); } sf_close(sndfile_new); rc = 0; /* remove(filename); */ } free(buffer); } } return rc; }
int main( int argc, char **argv ) { Analysis *scmanal = new Analysis( 1, 1, 1, "AIFF_OUTPUT", argc, argv ); char *output_aiff_filename = argv[ 2 ]; assert( scmanal->get_type( 0 ) == TIME_DOMAIN ); /* Open AIFF input */ SF_INFO aiff_info; aiff_info.format = SF_FORMAT_AIFF | SF_FORMAT_PCM_24; aiff_info.frames = scmanal->get_length( 0 ); aiff_info.samplerate = 2 * (scmanal->get_compl_end( 0 ) - scmanal->get_compl_begin( 0 )); aiff_info.channels = 2; aiff_info.sections = 0; aiff_info.seekable = 0; SNDFILE *aiff = sf_open( output_aiff_filename, SFM_WRITE, &aiff_info ); if ( aiff == NULL ) { fprintf( stderr, "sf_open: %s\n", sf_strerror( NULL ) ); exit( 1 ); } double *file_samples = scmanal->get_file( 0 ); uint length = scmanal->get_length( 0 ); uint overflow_count = 0; for ( uint i = 0; i < length; i++ ) { int samples[ 2 ]; if ( fabs( file_samples[ i ] ) >= 1<<23 ) { // fprintf( stderr, "Warning: Possible overflow at sample %d\n", i ); overflow_count++; } samples[ 0 ] = samples[ 1 ] = 256.0 * file_samples[ i ]; assert( sf_writef_int( aiff, samples, 1 ) == 1 ); } if ( overflow_count ) { fprintf( stderr, "Warning: %d possible overflows\n", overflow_count ); } delete scmanal; assert( sf_close( aiff ) == 0 ); }
void test_writef_int_or_die (SNDFILE *file, int pass, int *test, sf_count_t frames, int line_num) { sf_count_t count ; if ((count = sf_writef_int (file, test, frames)) != frames) { printf ("\n\nLine %d", line_num) ; if (pass > 0) printf (" (pass %d)", pass) ; printf (" : sf_writef_int failed with short writef (%ld => %ld).\n", SF_COUNT_TO_LONG (frames), SF_COUNT_TO_LONG (count)) ; fflush (stdout) ; puts (sf_strerror (file)) ; exit (1) ; } ; return ; } /* test_writef_int */
static void concat_data_int (SNDFILE *wfile, SNDFILE *rofile, int channels) { static int data [BUFFER_LEN] ; int frames, readcount ; frames = BUFFER_LEN / channels ; readcount = frames ; sf_seek (wfile, 0, SEEK_END) ; while (readcount > 0) { readcount = sf_readf_int (rofile, data, frames) ; sf_writef_int (wfile, data, readcount) ; } ; return ; } /* concat_data_int */
/* simply write 'sample_count' silence-samples at 'pos' * (overwrite existing samples) */ static int write_silence(sf_count_t pos, sf_count_t sample_count) { int rc = 0; int channels = sfinfo.channels; sf_count_t buffer_size = SBW*100; int *buffer = calloc(channels*buffer_size, sizeof(int)); /* printf("write_silence: pos=%lld, sample_count=%lld\n", */ /* pos, sample_count); */ if (buffer == NULL) { warning("Out of Memory"); return -1; } /* go to position ... */ if (sf_seek(sndfile, pos, SEEK_SET|SFM_WRITE) < 0) { perr("write_silence: sf_seek write pointer"); warning("Libsndfile reports write pointer seek error in audio file"); rc = -1; } else { /* ... and write sample_count silence */ while (sample_count > 0) { if (sample_count - buffer_size < 0) buffer_size = sample_count; if (sf_writef_int(sndfile, buffer, buffer_size) != buffer_size) { perr("write_silence: sf_writef_int"); warning("Libsndfile reports write error in audio file"); rc = -1; break; } sample_count -= buffer_size; } } free(buffer); /* printf("write_silence: rc=%d\n", rc); */ return rc; }
/* * call-seq: * snd.write(buf) => integer * * Writes the entire contents of the given buffer to the sound and returns the * number of frames written. */ static VALUE ra_sound_write(VALUE self, VALUE buf) { RA_SOUND *snd; Data_Get_Struct(self, RA_SOUND, snd); if(snd->closed) rb_raise(eRubyAudioError, "closed sound"); // Get buffer struct RA_BUFFER *b; Data_Get_Struct(buf, RA_BUFFER, b); // Get info struct SF_INFO *info; Data_Get_Struct(snd->info, SF_INFO, info); // Check buffer channels matches actual channels if(b->channels != info->channels) { rb_raise(eRubyAudioError, "channel count mismatch: %d vs %d", b->channels, info->channels); } // Write data sf_count_t written; switch(b->type) { case RA_BUFFER_TYPE_SHORT: written = sf_writef_short(snd->snd, b->data, b->real_size); break; case RA_BUFFER_TYPE_INT: written = sf_writef_int(snd->snd, b->data, b->real_size); break; case RA_BUFFER_TYPE_FLOAT: written = sf_writef_float(snd->snd, b->data, b->real_size); break; case RA_BUFFER_TYPE_DOUBLE: written = sf_writef_double(snd->snd, b->data, b->real_size); break; } return OFFT2NUM(written); }
static void interleave_int (STATE * state) { int max_read_len, read_len ; int ch, k ; do { max_read_len = 0 ; for (ch = 0 ; ch < state->channels ; ch ++) { read_len = sf_read_int (state->infile [ch], state->din.i, BUFFER_LEN) ; if (read_len < BUFFER_LEN) memset (state->din.i + read_len, 0, sizeof (state->din.i [0]) * (BUFFER_LEN - read_len)) ; for (k = 0 ; k < BUFFER_LEN ; k++) state->dout.i [k * state->channels + ch] = state->din.i [k] ; max_read_len = MAX (max_read_len, read_len) ; } ; sf_writef_int (state->outfile, state->dout.i, max_read_len) ; } while (max_read_len > 0) ; } /* interleave_int */
static sf_count_t silence_detector_locate_silence_offset(SilenceDetector *ptr, sf_count_t soff, sf_count_t *offset, FILE *owave) { int i, wave_length, avg = 0; double chunk_duration, rms = 0.0; sf_count_t read_frames; memset(ptr->buffer, 0, ptr->buffer_size * sizeof(int)); read_frames = sf_readf_int(ptr->sndfile, ptr->buffer, ptr->buffer_size); for ( i = 0; i < read_frames; ++i ) { wave_length = ptr->buffer[i] / (ptr->sndfile_info.samplerate); avg += abs(wave_length); rms += (wave_length * wave_length); //printf("%.2d ", wave_length); } //printf("r: %llu, avg: %d\n", read_frames, avg); if (read_frames > 0 && avg > 0) { rms /= read_frames; rms = sqrt(rms); avg /= read_frames; //printf("\t%d\n", (int)avg); // fflush(stdout); if (owave) { // write out the wave amps fprintf(owave, "\t%.2f\n", rms); } } if (avg > ptr->threshold) { //printf("\tnoisy here: %llu until %llu\n", *offset, ((*offset) + read_frames)); *offset += read_frames; // end of the sequence is possibly silence // open the sound file if necessary if (!ptr->out_sndfile) { silence_detector_create_audio_chunk(ptr); } // write these bytes to our file sf_writef_int(ptr->out_sndfile, ptr->buffer, read_frames); } else { //printf("starting silence at %llu\n", *offset); if (ptr->out_sndfile) { sf_close(ptr->out_sndfile); ptr->out_sndfile = NULL; // get the duration of the chunk and compare to the filter duration chunk_duration = read_sound_file_duration(ptr->last_chunk_file_name); printf("chunk: %s %.2f sec\n", ptr->last_chunk_file_name, chunk_duration); if (chunk_duration < ptr->min_duration) { printf("chunk %s:%.2f is smaller than min duration: %.2f\n", ptr->last_chunk_file_name, chunk_duration, ptr->min_duration); unlink(ptr->last_chunk_file_name); } // the chunk is too long based on the max provided, it will need to be reprocessed with a higher threshold and a smaller window if (chunk_duration > ptr->max_duration) { printf("chunk %s:%.2f is larger than max duration: %.2f\n", ptr->last_chunk_file_name, chunk_duration, ptr->max_duration); // reprocess this chunk on it's own using a higher threshold and a smaller buffer silence_detector_verify_last_chunk_duration(ptr); } } } return read_frames; }
int64_t _ambix_writef_int32 (ambix_t*ambix, const int32_t*data, int64_t frames) { return (int64_t)sf_writef_int(PRIVATE(ambix)->sf_file, (int*)data, frames) ; }
int rec_raw (int margin_dB, int chans, int fs, char *nameFile, bool*flagParar, int *clipping, int device) { int i = 0, j = 0; int buffer_size; int dummy; int clipping_threshold = (int)((double)INT_MAX/pow(10, ((double)margin_dB/(double)20)) ); MY_TYPE *buffer; RtAudio *audio = 0; char nomFichEGG[MAXPATH]; strcpy(nomFichEGG,nameFile); flhPonerExtension(nomFichEGG,".egg.wav"); char nomFichMicro[MAXPATH]; strcpy(nomFichMicro,nameFile); flhPonerExtension(nomFichMicro,".ch1.wav"); /*Implemetación con libnsnd*/ SNDFILE *fdTOT; SNDFILE *fdEGG; SNDFILE *fdMicro; SF_INFO SfInfoTot, SfInfoEGG, SfInfoMicro; SfInfoTot.samplerate = fs; SfInfoTot.channels = chans; SfInfoTot.format = SF_FORMAT_WAV | SF_FORMAT_PCM_24 | SF_ENDIAN_LITTLE; SfInfoEGG.samplerate = fs; SfInfoEGG.channels = 1; SfInfoEGG.format = SF_FORMAT_WAV | SF_FORMAT_PCM_16 | SF_ENDIAN_LITTLE; SfInfoMicro.samplerate = fs; SfInfoMicro.channels = 1; SfInfoMicro.format = SF_FORMAT_WAV | SF_FORMAT_PCM_16 | SF_ENDIAN_LITTLE; fdTOT = sf_open (nameFile, SFM_WRITE, &SfInfoTot); fdEGG = sf_open (nomFichEGG, SFM_WRITE, &SfInfoEGG); fdMicro = sf_open (nomFichMicro, SFM_WRITE, &SfInfoMicro); /*Implemetación con libnsnd*/ (*clipping) = 0; buffer_size = TAM_BUFF_AUDIO; try { audio = new RtAudio(0, 0, device, chans, FORMAT, fs, &buffer_size, MAX_NUM_CHANNELS, #ifdef __WINDOWS_DS__ RtAudio::WINDOWS_DS #elif defined(__WINDOWS_ASIO__) RtAudio::WINDOWS_ASIO #endif ); } catch (RtError &error) { error.printMessage(); exit(EXIT_FAILURE); } try { buffer = (MY_TYPE *) audio->getStreamBuffer(); audio->startStream(); } catch (RtError &error) { error.printMessage(); goto cleanup; } /********* GRABAMOS ************/ /*******************************/ while (1) { if (*flagParar==false) { try { audio->tickStream(); } catch (RtError &error) { error.printMessage(); goto cleanup; } //Grabamos los ocho canales sf_count_t counts = buffer_size; counts = sf_writef_int (fdTOT, buffer, counts); for (int i=0; i < buffer_size; i++) { dummy = INT_MAX; if ((dummy=abs(buffer[i*chans+canalMicro])) > clipping_threshold) { (*clipping)++; //dummy = CLIPPING_THRESHOLD; } counts = sf_writef_int (fdEGG, &buffer[i*chans+canalEGG], 1); counts = sf_writef_int (fdMicro, &buffer[i*chans+canalMicro], 1); } /* EnterCriticalSection(&CriticalSection); for (i = 0, j = 0; i < buffer_size; i += chans, j++) { muestraOSC_GLOTO[j] = buffer[i*chans + canalEGG]; muestraOSC_CHANNEL[j] = buffer[i*chans + canalMicro]; } LeaveCriticalSection (&CriticalSection); */ } else { try { audio->tickStream(); } catch (RtError &error) { error.printMessage(); goto cleanup; } break; } }//end while try { audio->stopStream(); } catch (RtError &error) { error.printMessage(); } cleanup: audio->closeStream(); delete audio; sf_close (fdTOT); sf_close (fdEGG); sf_close (fdMicro); return 0; }
int main(int argc, char** argv) { SF_INFO info = {}, dst_info = {}; SNDFILE *in = NULL, *out = NULL; int* buffer = NULL; size_t num_low_samples = 0; sf_count_t ndata = 0; if (argc != 3) { printf("usage: %s in.wav out.wav\n", argv[0]); return EXIT_FAILURE; } in = sf_open(argv[1], SFM_READ, &info); if (sf_error(0)) { fprintf(stderr, "%s\n", sf_strerror(0)); goto cleanup; } printf("rate %d, frames %d channels %d\n", info.samplerate, (int)info.frames, info.channels); buffer = (int*)malloc(info.channels * info.frames * sizeof(int)); if (!buffer) { perror(""); goto cleanup; } ndata = sf_readf_int(in, buffer, info.frames * info.channels); if (sf_error(0)) { fprintf(stderr, "failed to read input %s\n", sf_strerror(0)); } printf("read %d items\n", (int)ndata); dst_info = info; dst_info.samplerate = 8000; num_low_samples = downsample(buffer, ndata, info.samplerate, dst_info.samplerate); out = sf_open(argv[2], SFM_WRITE, &dst_info); if (sf_error(0)) { fprintf(stderr, "%s\n", sf_strerror(0)); goto cleanup; } ndata = sf_writef_int(out, buffer, num_low_samples); if (sf_error(0)) { fprintf(stderr, "failed to write output %s\n", sf_strerror(0)); goto cleanup; } printf("written %d items\n", (int)ndata); cleanup: if (buffer) { free(buffer); } if (in) { sf_close(in); } if (out) { sf_close(out); } return EXIT_SUCCESS; }
static void lcomp_test_int (const char *str, const char *filename, int filetype, double margin) { SNDFILE *file ; SF_INFO sfinfo ; int k, m, *orig, *data, sum_abs ; long datalen, seekpos ; double scale ; printf ("\nThis is program is not part of the libsndfile test suite.\n\n") ; printf (" lcomp_test_int : %s ... ", str) ; fflush (stdout) ; datalen = BUFFER_SIZE ; scale = 1.0 * 0x10000 ; data = data_buffer.i ; orig = orig_buffer.i ; gen_signal_double (orig_buffer.d, 32000.0 * scale, datalen) ; for (k = 0 ; k < datalen ; k++) orig [k] = orig_buffer.d [k] ; sfinfo.samplerate = SAMPLE_RATE ; sfinfo.frames = 123456789 ; /* Ridiculous value. */ sfinfo.channels = 1 ; sfinfo.format = filetype ; if (! (file = sf_open (filename, SFM_WRITE, &sfinfo))) { printf ("sf_open_write failed with error : ") ; puts (sf_strerror (NULL)) ; exit (1) ; } ; if ((k = sf_writef_int (file, orig, datalen)) != datalen) { printf ("sf_writef_int failed with short write (%ld => %d).\n", datalen, k) ; exit (1) ; } ; sf_close (file) ; memset (data, 0, datalen * sizeof (int)) ; if ((filetype & SF_FORMAT_TYPEMASK) != SF_FORMAT_RAW) memset (&sfinfo, 0, sizeof (sfinfo)) ; if (! (file = sf_open (filename, SFM_READ, &sfinfo))) { printf ("sf_open_read failed with error : ") ; puts (sf_strerror (NULL)) ; exit (1) ; } ; if ((sfinfo.format & (SF_FORMAT_TYPEMASK | SF_FORMAT_SUBMASK)) != (filetype & (SF_FORMAT_TYPEMASK | SF_FORMAT_SUBMASK))) { printf ("Line %d: Returned format incorrect (0x%08X => 0x%08X).\n", __LINE__, filetype, sfinfo.format) ; exit (1) ; } ; if (sfinfo.frames < datalen) { printf ("Too few.frames in file. (%ld should be a little more than %ld)\n", datalen, SF_COUNT_TO_LONG (sfinfo.frames)) ; exit (1) ; } ; if (sfinfo.frames > (datalen + datalen / 2)) { printf ("Too many.frames in file. (%ld should be a little more than %ld)\n", datalen, SF_COUNT_TO_LONG (sfinfo.frames)) ; exit (1) ; } ; if (sfinfo.channels != 1) { printf ("Incorrect number of channels in file.\n") ; exit (1) ; } ; check_log_buffer_or_die (file, __LINE__) ; if ((k = sf_readf_int (file, data, datalen)) != datalen) { printf ("Line %d: short read (%d should be %ld).\n", __LINE__, k, datalen) ; exit (1) ; } ; sum_abs = 0 ; for (k = 0 ; k < datalen ; k++) { if (error_function (data [k] / scale, orig [k] / scale, margin)) { printf ("Line %d: Incorrect sample (#%d : %f should be %f).\n", __LINE__, k, data [k] / scale, orig [k] / scale) ; oct_save_int (orig, data, datalen) ; exit (1) ; } ; sum_abs = abs (sum_abs + abs (data [k])) ; } ; if (sum_abs < 1.0) { printf ("Line %d: Signal is all zeros.\n", __LINE__) ; exit (1) ; } ; if ((k = sf_readf_int (file, data, datalen)) != sfinfo.frames - datalen) { printf ("Line %d: Incorrect read length (%ld should be %d).\n", __LINE__, SF_COUNT_TO_LONG (sfinfo.frames - datalen), k) ; exit (1) ; } ; /* This check is only for block based encoders which must append silence ** to the end of a file so as to fill out a block. */ if ((sfinfo.format & SF_FORMAT_SUBMASK) != SF_FORMAT_MS_ADPCM) for (k = 0 ; k < sfinfo.frames - datalen ; k++) if (abs (data [k] / scale) > decay_response (k)) { printf ("Line %d : Incorrect sample B (#%d : abs (%d) should be < %d).\n", __LINE__, k, data [k], decay_response (k)) ; exit (1) ; } ; if (! sfinfo.seekable) { printf ("ok\n") ; return ; } ; /* Now test sf_seek function. */ if ((k = sf_seek (file, 0, SEEK_SET)) != 0) { printf ("Line %d: Seek to start of file failed (%d).\n", __LINE__, k) ; exit (1) ; } ; for (m = 0 ; m < 3 ; m++) { int n ; if ((k = sf_readf_int (file, data, 11)) != 11) { printf ("Line %d: Incorrect read length (11 => %d).\n", __LINE__, k) ; exit (1) ; } ; for (k = 0 ; k < 11 ; k++) if (error_function (data [k] / scale, orig [k + m * 11] / scale, margin)) { printf ("Line %d: Incorrect sample (m = %d) (#%d : %d => %d).\n", __LINE__, m, k + m * 11, orig [k + m * 11], data [k]) ; for (n = 0 ; n < 1 ; n++) printf ("%d ", data [n]) ; printf ("\n") ; exit (1) ; } ; } ; seekpos = BUFFER_SIZE / 10 ; /* Check seek from start of file. */ if ((k = sf_seek (file, seekpos, SEEK_SET)) != seekpos) { printf ("Seek to start of file + %ld failed (%d).\n", seekpos, k) ; exit (1) ; } ; if ((k = sf_readf_int (file, data, 1)) != 1) { printf ("Line %d: sf_readf_int (file, data, 1) returned %d.\n", __LINE__, k) ; exit (1) ; } ; if (error_function ((double) data [0], (double) orig [seekpos], margin)) { printf ("Line %d: sf_seek (SEEK_SET) followed by sf_readf_int failed (%d, %d).\n", __LINE__, orig [1], data [0]) ; exit (1) ; } ; if ((k = sf_seek (file, 0, SEEK_CUR)) != seekpos + 1) { printf ("Line %d: sf_seek (SEEK_CUR) with 0 offset failed (%d should be %ld)\n", __LINE__, k, seekpos + 1) ; exit (1) ; } ; seekpos = sf_seek (file, 0, SEEK_CUR) + BUFFER_SIZE / 5 ; k = sf_seek (file, BUFFER_SIZE / 5, SEEK_CUR) ; sf_readf_int (file, data, 1) ; if (error_function ((double) data [0], (double) orig [seekpos], margin) || k != seekpos) { printf ("Line %d: sf_seek (forwards, SEEK_CUR) followed by sf_readf_int failed (%d, %d) (%d, %ld).\n", __LINE__, data [0], orig [seekpos], k, seekpos + 1) ; exit (1) ; } ; seekpos = sf_seek (file, 0, SEEK_CUR) - 20 ; /* Check seek backward from current position. */ k = sf_seek (file, -20, SEEK_CUR) ; sf_readf_int (file, data, 1) ; if (error_function ((double) data [0], (double) orig [seekpos], margin) || k != seekpos) { printf ("sf_seek (backwards, SEEK_CUR) followed by sf_readf_int failed (%d, %d) (%d, %ld).\n", data [0], orig [seekpos], k, seekpos) ; exit (1) ; } ; /* Check that read past end of file returns number of items. */ sf_seek (file, (int) sfinfo.frames, SEEK_SET) ; if ((k = sf_readf_int (file, data, datalen)) != 0) { printf ("Line %d: Return value from sf_readf_int past end of file incorrect (%d).\n", __LINE__, k) ; exit (1) ; } ; /* Check seek backward from end. */ if ((k = sf_seek (file, 5 - (int) sfinfo.frames, SEEK_END)) != 5) { printf ("sf_seek (SEEK_END) returned %d instead of %d.\n", k, 5) ; exit (1) ; } ; sf_readf_int (file, data, 1) ; if (error_function (data [0] / scale, orig [5] / scale, margin)) { printf ("Line %d: sf_seek (SEEK_END) followed by sf_readf_short failed (%d should be %d).\n", __LINE__, data [0], orig [5]) ; exit (1) ; } ; sf_close (file) ; printf ("ok\n") ; } /* lcomp_test_int */
/* append 'sample_count' samples and shift all samples from 'first_pos' * to end of file (this creates 'sample_count' samples at 'first_pos') */ int soundfile_shift_samples_right(long lfirst_pos, long lsample_count, int status_info) { sf_count_t first_pos = lfirst_pos; sf_count_t sample_count = lsample_count; sf_count_t end_pos = sf_seek(sndfile, 0, SEEK_END|SFM_READ); if (sample_count <= 0) { puts("soundfile_shift_samples_right: no samples to move"); return 0; } /* printf("soundfile_shift_samples_right: " */ /* "first_pos=%lld, shift=%lld, samples in file=%lld\n", */ /* first_pos, sample_count, end_pos); */ if (first_pos < end_pos) { sf_count_t processed = 0; sf_count_t read_pos, write_pos; int channels = sfinfo.channels; #define TMPBUFSIZE (4*SBW*1000) int buffer[TMPBUFSIZE]; sf_count_t buffer_size = TMPBUFSIZE/channels; /* go to end position and append enough space for the new data */ if (write_silence(end_pos, sample_count) < 0) { return -1; } /* loop reading-writing from end */ write_pos = end_pos + sample_count; read_pos = end_pos; while (read_pos > first_pos) { if (buffer_size > read_pos - first_pos) buffer_size = read_pos - first_pos; write_pos -= buffer_size; read_pos -= buffer_size; /* printf("soundfile_shift_samples_right: " */ /* "read_pos=%lld, write_pos=%lld, diff=%lld, buffer_size=%lld\n", */ /* read_pos, write_pos, write_pos-read_pos, buffer_size); */ /* start position for reading */ if (sf_seek(sndfile, read_pos, SEEK_SET|SFM_READ) < 0) { perr("soundfile_shift_samples_right: sf_seek read pointer"); warning("Libsndfile reports read pointer seek error in audio file"); return -1; } if (sf_readf_int(sndfile, buffer, buffer_size) != buffer_size) { perr("soundfile_shift_samples_right: sf_readf_int"); warning("Libsndfile reports read error in audio file"); return -1; } /* start position for writing */ if (sf_seek(sndfile, write_pos, SEEK_SET|SFM_WRITE) < 0) { perr("soundfile_shift_samples_right: sf_seek write pointer"); warning("Libsndfile reports write pointer seek error in audio file"); return -1; } if (sf_writef_int(sndfile, buffer, buffer_size) != buffer_size) { perr("soundfile_shift_samples_right: sf_writef_int"); warning("Libsndfile reports write error in audio file"); return -1; } processed += buffer_size; if (status_info) update_progress_bar((gfloat)processed/(end_pos-first_pos), PROGRESS_UPDATE_INTERVAL, FALSE); /* printf("soundfile_shift_samples_right: processed=%lld (%f.2)\n", */ /* processed, (gfloat)processed/(end_pos-first_pos)); */ } } /* printf("soundfile_shift_samples_right: samples in file=%lld\n", */ /* sf_seek(sndfile, 0, SEEK_END|SFM_READ)); */ adjust_all_marker_positions(first_pos, sample_count); return 0; }
/* shift all samples from 'first_pos' to begin of file * (this removes 'sample_count' samples at end of file) */ int soundfile_shift_samples_left(long lfirst_pos, long lsample_count, int status_info) { /* if 'last_pos' is equal 'end_pos' simply truncate the end * else move sample i from 'last_pos' to the 'first_pos' position * (for all i between 'last_pos' and end of file) * and then truncate 'last_pos - first_pos' samples at the end. */ sf_count_t first_pos = lfirst_pos; sf_count_t sample_count = lsample_count; sf_count_t end_pos = sf_seek(sndfile, 0, SEEK_END|SFM_READ); sf_count_t last_pos = first_pos + sample_count; if (sample_count <= 0) { puts("soundfile_shift_samples_left: no samples to move"); return 0; } /* printf("soundfile_shift_samples_left: " */ /* "first_pos=%lld, shift=%lld, samples in file=%lld\n", */ /* first_pos, sample_count, end_pos); */ if (last_pos < end_pos) { sf_count_t processed = 0; sf_count_t buffer_size; int channels = sfinfo.channels; #define TMPBUFSIZE (4*SBW*1000) int buffer[TMPBUFSIZE]; /* start position for writing */ if (sf_seek(sndfile, first_pos, SEEK_SET|SFM_WRITE) < 0) { perr("soundfile_shift_samples_left: sf_seek write pointer"); warning("Libsndfile reports write pointer seek error in audio file"); return -1; } /* start position for reading */ if (sf_seek(sndfile, last_pos, SEEK_SET|SFM_READ) < 0) { perr("soundfile_shift_samples_left: sf_seek read pointer"); warning("Libsndfile reports read pointer seek error in audio file"); return -1; } /* loop to end of file */ buffer_size = TMPBUFSIZE/channels; while ((buffer_size = sf_readf_int(sndfile, buffer, buffer_size)) > 0) { if (sf_writef_int(sndfile, buffer, buffer_size) != buffer_size) { perr("soundfile_shift_samples_left: sf_writef_int"); warning("Libsndfile reports write error in audio file"); return -1; } processed += buffer_size; if (status_info) update_progress_bar((gfloat)processed/(end_pos-first_pos-sample_count), PROGRESS_UPDATE_INTERVAL, FALSE); /* printf("soundfile_shift_samples_left: processed=%lld (%f.2)\n", */ /* processed, (gfloat)processed/(end_pos-first_pos-sample_count)); */ } } /* truncate file size for '-sample_count' samples */ end_pos -= sample_count; if (sf_command(sndfile, SFC_FILE_TRUNCATE, &end_pos, sizeof(end_pos))) { perr("soundfile_shift_samples_left: sf_command"); warning("Libsndfile reports truncation of audio file failed"); return -1; } /* printf("soundfile_shift_samples_left: samples in file=%lld\n", */ /* sf_seek(sndfile, 0, SEEK_END|SFM_READ)); */ adjust_all_marker_positions(first_pos, -sample_count); return 0; }