size_t LibmediaPlayback::FillBuffer(void* data, size_t size) { CHECK(data); if (in_file_) { int frames_read = 0; int frame_size = audio_bytes_per_sample(audio_format_) * num_channels_; int sample_size = audio_bytes_per_sample(audio_format_); if (sample_size == 1) { void* temp = malloc(size * sizeof(short)); frames_read = sf_readf_short(in_file_, reinterpret_cast<short*>(temp), size / frame_size); int num_samples = frames_read * frame_size / sample_size; memcpy_to_u8_from_i16(reinterpret_cast<uint8_t*>(data), reinterpret_cast<int16_t*>(temp), num_samples); free(temp); } else if (sample_size == 2) { frames_read = sf_readf_short(in_file_, reinterpret_cast<short*>(data), size / frame_size); } else if (sample_size == 4) { frames_read = sf_readf_int(in_file_, reinterpret_cast<int*>(data), size / frame_size); } else { LOG(ERROR) << "Could not handle file with sample size = " << sample_size; return 0; } size = frame_size * frames_read; } else { size = (size < sine_data_buffer_->size()) ? size : sine_data_buffer_->size(); memcpy(data, sine_data_buffer_->data(), size); } return size; }
static void ra_sound_read_short(RA_SOUND *snd, RA_BUFFER *buf, sf_count_t frames) { static short temp[1024]; int temp_len = 1024; short *data = (short*)buf->data; short mix_sum; // Get info struct SF_INFO *info; Data_Get_Struct(snd->info, SF_INFO, info); // Up/Downmix based on channel matching sf_count_t read = 0, r, amount; int i, k; if(buf->channels == info->channels) { // Simply read data without mix read = sf_readf_short(snd->snd, data, frames); } else if(buf->channels == 1) { // Downmix to mono sf_count_t max = temp_len / info->channels; int channels; while(read < frames) { // Calculate # of frames to read amount = frames - read; if(amount > max) amount = max; r = sf_readf_short(snd->snd, temp, amount); if(r == 0) break; // Mix channels together by averaging all channels and store to buffer for(i = 0; i < r; i++) { mix_sum = 0; for(k = 0; k < info->channels; k++) mix_sum += temp[i * info->channels + k]; data[read] = mix_sum/info->channels; read++; } } } else if(info->channels == 1) { // Upmix from mono by copying channel while(read < frames) { // Calculate # of frames to read amount = frames - read; if(amount > temp_len) amount = temp_len; r = sf_readf_short(snd->snd, temp, amount); if(r == 0) break; // Write every frame channel times to the buffer for(i = 0; i < r; i++) { for(k = 0; k < buf->channels; k++) { data[read * buf->channels + k] = temp[i]; } read++; } } } else { rb_raise(eRubyAudioError, "unsupported mix from %d to %d", buf->channels, info->channels); } buf->real_size = read; }
static int file_decode_tests(super_tone_rx_state_t *super, const char *file_name) { int16_t amp[8000]; int sample; int frames; int x; awgn_state_t noise_source; printf("File decode tests\n"); super_tone_rx_tone_callback(super, wakeup, (void *) "test"); awgn_init_dbm0(&noise_source, 1234567, -30.0f); printf("Processing file\n"); if ((inhandle = sf_open_telephony_read(file_name, 1)) == NULL) { fprintf(stderr, " Cannot open audio file '%s'\n", file_name); exit(2); } while ((frames = sf_readf_short(inhandle, amp, 8000))) { /* Add some noise to the signal for a more meaningful test. */ //for (sample = 0; sample < frames; sample++) // amp[sample] += saturate(amp[sample] + awgn (&noise_source)); for (sample = 0; sample < frames; ) { x = super_tone_rx(super, amp + sample, frames - sample); sample += x; } } if (sf_close_telephony(inhandle)) { fprintf(stderr, " Cannot close audio file '%s'\n", file_name); exit(2); } return 0; }
static int talk_off_tests(super_tone_rx_state_t *super) { int16_t amp[8000]; int sample; int frames; int j; int x; /* Test for voice immunity */ printf("Talk off tests\n"); for (j = 0; bellcore_files[j][0]; j++) { if ((inhandle = sf_open_telephony_read(bellcore_files[j], 1)) == NULL) { printf(" Cannot open audio file '%s'\n", bellcore_files[j]); exit(2); } while ((frames = sf_readf_short(inhandle, amp, 8000))) { for (sample = 0; sample < frames; ) { x = super_tone_rx(super, amp + sample, frames - sample); sample += x; } } if (sf_close_telephony(inhandle)) { printf(" Cannot close speech file '%s'\n", bellcore_files[j]); exit(2); } } return 0; }
void SoundFileStream::load( SNDFILE *sf, const SF_INFO &info, sf_count_t beg, sf_count_t dur ) { delete[] _data; _dataOffset = beg; _dataSize = dur; _data = new short [_dataSize * info.channels]; sf_seek( sf, _dataOffset, SEEK_SET); if (info.format & SF_FORMAT_FLOAT || info.format & SF_FORMAT_DOUBLE) { // libsndfile reading float into short is broken for non-power-of-two channel counts int sampleCount = _dataSize * info.channels; float *tmp = new float [sampleCount]; _dataSize = sf_readf_float( sf, tmp, _dataSize ); for (int i = 0; i < sampleCount; ++i) _data[i] = std::max( -1.f, std::min( 1.f, tmp[i] ) ) * std::numeric_limits<short>::max(); delete[] tmp; } else { _dataSize = sf_readf_short( sf, _data, _dataSize ); } _ch = info.channels; _beg = _dataOffset; _dur = _dataSize; }
size_t load_buffer(ALuint buf) { // seek to beginning if in a end if (is_end()) { if (info_.seekable) { if (sf_seek(file_.get(), 0, SEEK_SET) == -1) { Output::Error("libsndfile seek error: %s", sf_strerror(file_.get())); return 0; } } else { file_.reset(sf_open(filename_.c_str(), SFM_READ, &info_), sf_close); if (!file_) { Output::Error("libsndfile open error: %s", sf_strerror(NULL)); return 0; } } loop_count_++; seek_pos_ = 0; } data_.resize(info_.channels * info_.samplerate * SECOND_PER_BUFFER); sf_count_t const read_size = sf_readf_short(file_.get(), &data_.front(), data_.size() / info_.channels); alBufferData(buf, format_, &data_.front(), sizeof(int16_t) * data_.size(), info_.samplerate); seek_pos_ += read_size; return read_size; }
static ssize_t sa_sndfile_read( simpleaudio *sa, void *buf, size_t nframes ) { SNDFILE *s = (SNDFILE *)sa->backend_handle; int n; switch ( sa->format ) { case SA_SAMPLE_FORMAT_FLOAT: n = sf_readf_float(s, buf, nframes); break; case SA_SAMPLE_FORMAT_S16: n = sf_readf_short(s, buf, nframes); break; default: assert(0); break; } if ( n < 0 ) { fprintf(stderr, "sf_read: "); sf_perror(s); return -1; } if ( sa->rxnoise != 0.0 ) { int i; float *fbuf = buf; float f = sa->rxnoise * 2; for ( i=0; i<nframes; i++ ) fbuf[i] += (drand48() - 0.5) * f; } // fprintf(stderr, "sf_read: nframes=%ld n=%d\n", nframes, n); return n; }
static void __pulseaudio_stream_write_cb(pa_stream *stream, size_t length, void *user_data) { sf_count_t read_length; short *data; SOUND_INFO *info = NULL; mmf_return_if_fail(user_data); info = (SOUND_INFO *)user_data; _mmcam_dbg_log("START"); data = pa_xmalloc(length); read_length = (sf_count_t)(length/pa_frame_size(&(info->sample_spec))); if ((sf_readf_short(info->infile, data, read_length)) != read_length) { pa_xfree(data); return; } pa_stream_write(stream, data, length, pa_xfree, 0, PA_SEEK_RELATIVE); info->sample_length -= length; if (info->sample_length <= 0) { pa_stream_set_write_callback(info->sample_stream, NULL, NULL); pa_stream_finish_upload(info->sample_stream); pa_threaded_mainloop_signal(info->pulse_mainloop, 0); _mmcam_dbg_log("send signal DONE"); } _mmcam_dbg_log("DONE read_length %d", read_length); }
//------------------------------------------------------------ bool ofOpenALSoundPlayer::sfStream(string path,vector<short> & buffer,vector<float> & fftAuxBuffer){ if(!streamf){ SF_INFO sfInfo; streamf = sf_open(path.c_str(),SFM_READ,&sfInfo); if(!streamf){ ofLogError("ofOpenALSoundPlayer") << "sfStream(): couldn't read \"" << path << "\""; return false; } stream_subformat = sfInfo.format & SF_FORMAT_SUBMASK ; if (stream_subformat == SF_FORMAT_FLOAT || stream_subformat == SF_FORMAT_DOUBLE){ sf_command (streamf, SFC_CALC_SIGNAL_MAX, &stream_scale, sizeof (stream_scale)) ; if (stream_scale < 1e-10) stream_scale = 1.0 ; else stream_scale = 32700.0 / stream_scale ; } channels = sfInfo.channels; duration = float(sfInfo.frames) / float(sfInfo.samplerate); samplerate = sfInfo.samplerate; stream_samples_read = 0; } int curr_buffer_size = BUFFER_STREAM_SIZE*channels; if(speed>1) curr_buffer_size *= (int)round(speed); buffer.resize(curr_buffer_size); fftAuxBuffer.resize(buffer.size()); if (stream_subformat == SF_FORMAT_FLOAT || stream_subformat == SF_FORMAT_DOUBLE){ sf_count_t samples_read = sf_read_float (streamf, &fftAuxBuffer[0], fftAuxBuffer.size()); stream_samples_read += samples_read; if(samples_read<(int)fftAuxBuffer.size()){ fftAuxBuffer.resize(samples_read); buffer.resize(samples_read); setPosition(0); if(!bLoop) stopThread(); stream_samples_read = 0; stream_end = true; } for (int i = 0 ; i < int(fftAuxBuffer.size()) ; i++){ fftAuxBuffer[i] *= stream_scale ; buffer[i] = 32565.0 * fftAuxBuffer[i]; } }else{ sf_count_t frames_read = sf_readf_short(streamf,&buffer[0],curr_buffer_size/channels); stream_samples_read += frames_read*channels; if(frames_read<curr_buffer_size/channels){ fftAuxBuffer.resize(frames_read*channels); buffer.resize(frames_read*channels); setPosition(0); if(!bLoop) stopThread(); stream_samples_read = 0; stream_end = true; } for(int i=0;i<(int)buffer.size();i++){ fftAuxBuffer[i]=float(buffer[i])/32565.0f; } } return true; }
void SndFileSource::process() { //open first file if (mSoundFile || openNextFile()) { // read audio data do { sf_count_t res = sf_readf_short(mSoundFile, mAlsaFrame.getSoundBuffer(), mAlsaFrame.getDimensionT(AlsaFrame::Time).mResolution); if (res > 0) { // keep constant frame size, populate with zeros if (res < mAlsaFrame.getDimensionT(AlsaFrame::Time).mResolution) memset(mAlsaFrame.getSoundBuffer() + res*mAlsaFrame.getDimensionT(AlsaFrame::Channels).mResolution, 0, (mAlsaFrame.getDimensionT(AlsaFrame::Time).mResolution - res)*sizeof(qint16)*mAlsaFrame.getDimensionT(AlsaFrame::Channels).mResolution); mAlsaFrame.incrementTimeStamp(); emit framesReady(); return; } } //open next file while (openNextFile()); } //error openning file emit processingCompleted(); }
static void callback(SLBufferQueueItf bufq, void *param) { assert(NULL == param); if (!eof) { short *buffer = &buffers[framesPerBuffer * sfinfo.channels * which]; sf_count_t count; count = sf_readf_short(sndfile, buffer, (sf_count_t) framesPerBuffer); if (0 >= count) { eof = SL_BOOLEAN_TRUE; } else { SLuint32 nbytes = count * sfinfo.channels * sizeof(short); if (byteOrder != nativeByteOrder) { swab(buffer, buffer, nbytes); } if (bitsPerSample == 8) { squeeze(buffer, (unsigned char *) buffer, nbytes); nbytes /= 2; } SLresult result = (*bufq)->Enqueue(bufq, buffer, nbytes); assert(SL_RESULT_SUCCESS == result); if (++which >= numBuffers) which = 0; } } }
/// Read the data portion of the block file using libsndfile. Convert it /// to the given format if it is not already. /// /// @param data The buffer where the data will be stored /// @param format The format the data will be stored in /// @param start The offset in this block file /// @param len The number of samples to read int LegacyBlockFile::ReadData(samplePtr data, sampleFormat format, sampleCount start, sampleCount len) { SF_INFO info; memset(&info, 0, sizeof(info)); switch(mFormat) { case int16Sample: info.format = SF_FORMAT_RAW | SF_FORMAT_PCM_16 | SF_ENDIAN_CPU; break; default: case floatSample: info.format = SF_FORMAT_RAW | SF_FORMAT_FLOAT | SF_ENDIAN_CPU; break; case int24Sample: info.format = SF_FORMAT_RAW | SF_FORMAT_PCM_32 | SF_ENDIAN_CPU; break; } info.samplerate = 44100; // Doesn't matter info.channels = 1; info.frames = mLen + (mSummaryInfo.totalSummaryBytes / SAMPLE_SIZE(mFormat)); SNDFILE *sf = sf_open(mFileName.GetFullPath(), SFM_READ, &info); if (!sf) return 0; sf_count_t seekstart = start + (mSummaryInfo.totalSummaryBytes / SAMPLE_SIZE(mFormat)); sf_seek(sf, seekstart , SEEK_SET); samplePtr buffer = NewSamples(len, floatSample); int framesRead = 0; // If both the src and dest formats are integer formats, // read integers from the file (otherwise we would be // converting to float and back, which is unneccesary) if (format == int16Sample && sf_subtype_is_integer(info.format)) { framesRead = sf_readf_short(sf, (short *)data, len); } else if (format == int24Sample && sf_subtype_is_integer(info.format)) { framesRead = sf_readf_int(sf, (int *)data, len); // libsndfile gave us the 3 byte sample in the 3 most // significant bytes -- we want it in the 3 least // significant bytes. int *intPtr = (int *)data; for( int i = 0; i < framesRead; i++ ) intPtr[i] = intPtr[i] >> 8; }
int Mp3Decoder::getMonoFrames(int numFrames, void *buffer) { void *tmpBuffer = malloc(2*numFrames*2); int readFrames = sf_readf_short(sfFile, (short*)tmpBuffer, numFrames); for(int i=0; i<readFrames;i++) { memcpy(buffer+2*i, tmpBuffer+4*i, 2); } return readFrames; }
bool DRMReceiver::fRead(CVector<_SAMPLE>& psData) { msleep(75); int len = BufferSize / 2; sf_count_t c = sf_readf_short(sfin, &psData[0], len); if(c != len) { if(m_loop) { /* rewind */ sf_seek(sfin, 0, SEEK_SET); c = sf_readf_short(sfin, &psData[0], len); } else return true; } return false; }
ALuint SndFileDecoder::read(ALvoid *ptr, ALuint count) { sf_count_t got = 0; if(mSampleType == SampleType::Int16) got = sf_readf_short(mSndFile, static_cast<short*>(ptr), count); else if(mSampleType == SampleType::Float32) got = sf_readf_float(mSndFile, static_cast<float*>(ptr), count); return (ALuint)std::max<sf_count_t>(got, 0); }
static int power_surge_detector_file_test(const char *file) { SNDFILE *inhandle; SNDFILE *outhandle; int inframes; power_surge_detector_state_t *sig; int i; int16_t amp[8000]; int16_t amp_out[2*8000]; int sample; float signal_power; int32_t signal_level; int signal_present; int prev_signal_present; if ((inhandle = sf_open_telephony_read(file, 1)) == NULL) { printf(" Cannot open speech file '%s'\n", file); exit(2); } if ((outhandle = sf_open_telephony_write(OUT_FILE_NAME, 1)) == NULL) { fprintf(stderr, " Cannot create audio file '%s'\n", OUT_FILE_NAME); exit(2); } sig = power_surge_detector_init(NULL, -50.0f, 6.0f); prev_signal_present = FALSE; sample = 0; while ((inframes = sf_readf_short(inhandle, amp, 8000))) { for (i = 0; i < inframes; i++) { signal_level = power_surge_detector(sig, amp[i]); signal_present = (signal_level != 0); if (prev_signal_present != signal_present) { signal_power = power_surge_detector_current_dbm0(sig); if (signal_present) printf("On at %f (%fdBm0)\n", (sample + i)/8000.0, signal_power); else printf("Off at %f (%fdBm0)\n", (sample + i)/8000.0, signal_power); prev_signal_present = signal_present; } amp_out[2*i] = amp[i]; amp_out[2*i + 1] = signal_present*5000; } sf_writef_short(outhandle, amp_out, inframes); sample += inframes; } sf_close(inhandle); sf_close(outhandle); return 0; }
static size_t media_sndfile_read(media_substream *mss, void *outbuf, size_t length) { /* read at most `length' frames into `outbuf' */ /* libsndfile stuff */ media_sndfile_data *sfd; SNDFILE *sf; /* media stream stuff */ Lisp_Media_Stream *ms = mss->up; mtype_audio_properties *mtap; media_sample_format_t *fmt; sf_count_t _read = 0; /* check the integrity of the media stream */ if (media_stream_driver(ms) != MYSELF) return 0; if (media_substream_type(mss) != MTYPE_AUDIO) return 0; /* fetch the SNDFILE context and our audio props */ if ((sfd = media_stream_data(ms)) == NULL || (sf = sfd->sf) == NULL) return 0; if (!(mtap = media_substream_type_properties(mss).aprops)) return 0; fmt = mtap->msf; switch (sfd->sfinfo->format & 0xFF) { case SF_FORMAT_ULAW: case SF_FORMAT_ALAW: case SF_FORMAT_PCM_U8: case SF_FORMAT_PCM_S8: case SF_FORMAT_PCM_16: _read = sf_readf_short(sf, outbuf, length); break; case SF_FORMAT_PCM_24: case SF_FORMAT_PCM_32: _read = sf_readf_int(sf, outbuf, length); break; case SF_FORMAT_FLOAT: _read = sf_readf_float(sf, outbuf, length); break; default: /* be mean */ abort(); } /* always convert to internal format */ MEDIA_SAMPLE_FORMAT_UPSAMPLE(fmt)(outbuf, outbuf, _read*mtap->channels); SNDFILE_DEBUG_S("read %d frames\n", _read); return _read; }
static void signal_load(signal_source_t *sig, const char *name) { sig->handle = sf_open_telephony_read(name, 1); sig->name = name; sig->max = sf_readf_short(sig->handle, sig->signal, SAMPLE_RATE); if (sig->max < 0) { fprintf(stderr, " Error reading sound file '%s'\n", sig->name); exit(2); } }
uint32_t* load_full_audiodata(SNDFILE* sndfile, const SF_INFO* sfinfo) { uint32_t* data = (uint32_t*)malloc(get_full_audiodata_size(sfinfo)); if(data == NULL) return NULL; if(sf_readf_short(sndfile, (short*)data, sfinfo->frames) != sfinfo->frames) { free(data); return NULL; } return data; }
// ---------------------------------------------------------------------------- bool ofOpenALSoundPlayer::sfReadFile(string path, vector<short> & buffer, vector<float> & fftAuxBuffer){ SF_INFO sfInfo; SNDFILE* f = sf_open(path.c_str(),SFM_READ,&sfInfo); if(!f){ ofLogError("ofOpenALSoundPlayer") << "sfReadFile(): couldn't read \"" << path << "\""; return false; } buffer.resize(sfInfo.frames*sfInfo.channels); fftAuxBuffer.resize(sfInfo.frames*sfInfo.channels); int subformat = sfInfo.format & SF_FORMAT_SUBMASK ; if (subformat == SF_FORMAT_FLOAT || subformat == SF_FORMAT_DOUBLE){ double scale ; sf_command (f, SFC_CALC_SIGNAL_MAX, &scale, sizeof (scale)) ; if (scale < 1e-10) scale = 1.0 ; else scale = 32700.0 / scale ; sf_count_t samples_read = sf_read_float (f, &fftAuxBuffer[0], fftAuxBuffer.size()); if(samples_read<(int)fftAuxBuffer.size()){ ofLogWarning("ofOpenALSoundPlayer") << "sfReadFile(): read " << samples_read << " float samples, expected " << fftAuxBuffer.size() << " for \"" << path << "\""; } for (int i = 0 ; i < int(fftAuxBuffer.size()) ; i++){ fftAuxBuffer[i] *= scale ; buffer[i] = 32565.0 * fftAuxBuffer[i]; } }else{ sf_count_t frames_read = sf_readf_short(f,&buffer[0],sfInfo.frames); if(frames_read<sfInfo.frames){ ofLogError("ofOpenALSoundPlayer") << "sfReadFile(): read " << frames_read << " frames from buffer, expected " << sfInfo.frames << " for \"" << path << "\""; return false; } sf_seek(f,0,SEEK_SET); frames_read = sf_readf_float(f,&fftAuxBuffer[0],sfInfo.frames); if(frames_read<sfInfo.frames){ ofLogError("ofOpenALSoundPlayer") << "sfReadFile(): read " << frames_read << " frames from fft buffer, expected " << sfInfo.frames << " for \"" << path << "\""; return false; } } sf_close(f); channels = sfInfo.channels; duration = float(sfInfo.frames) / float(sfInfo.samplerate); samplerate = sfInfo.samplerate; return true; }
ssize_t ad_read_sndfile_short(WfDecoder* d, WfBuf16* buf) { SndfileDecoder* sf = (SndfileDecoder*)d->d; switch(d->info.bit_depth){ case 8: case 16: { if(d->info.channels == 1){ return sf_readf_short(sf->sffile, buf->buf[0], buf->size); }else{ short* data = g_malloc0(d->info.channels * buf->size * sizeof(short)); ssize_t r = sf_read_short(sf->sffile, data, d->info.channels * buf->size); int i, f; for(i=0,f=0;i<r;i+=d->info.channels,f++){ int c; for(c=0;c<d->info.channels;c++){ buf->buf[c][f] = data[i]; } } g_free(data); return f; } } case 24: { int* data = g_malloc0(d->info.channels * buf->size * sizeof(int)); ssize_t r = sf_read_int(sf->sffile, data, d->info.channels * buf->size); int i, f; for(i=0,f=0;i<r;i+=d->info.channels,f++){ int c; for(c=0;c<d->info.channels;c++){ buf->buf[c][f] = data[i] >> 16; } } g_free(data); return f; } case 32: { float* data = g_malloc0(d->info.channels * buf->size * sizeof(float)); ssize_t r = sf_read_float(sf->sffile, data, d->info.channels * buf->size); int i, f; for(i=0,f=0;i<r;i+=d->info.channels,f++){ int c; for(c=0;c<d->info.channels;c++){ buf->buf[c][f] = AD_FLOAT_TO_SHORT(data[i]); } } g_free(data); return r; } #ifdef DEBUG default: dbg(0, "!!! unhandled bit depth: %i", d->info.bit_depth); #endif } return -1; }
int BlockFile::Read(void *data, int len) { wxASSERT(mMode & BLOCK_MODE_READING_MODE); /* make sure the read doesn't start in the header area and end in the data area */ wxASSERT(!(mPos < WaveTrack::GetHeaderLen() && mPos + len > WaveTrack::GetHeaderLen())); /* if you're in data mode, make sure the pointer is in the data area */ wxASSERT(mMode == BLOCK_MODE_READ_HEADER || mPos >= WaveTrack::GetHeaderLen()); /* if you're in header mode, make sure the pointer is in the header area */ wxASSERT(mMode == BLOCK_MODE_READ_DATA || mPos < WaveTrack::GetHeaderLen()); if (mType == BLOCK_TYPE_ALIAS && mMode == BLOCK_MODE_READ_DATA) { int channels = ((SF_INFO *)mInfo)->channels; int frames = len / sizeof(sampleType); int bufferSize = frames * channels; sampleType *buffer = new short[bufferSize]; int framesRead = sf_readf_short((SNDFILE *)mSoundFile, buffer, frames); for (int i = 0; i < framesRead; i++) ((sampleType *) data)[i] = buffer[(channels * i) + mChannel]; delete[]buffer; return (framesRead * sizeof(sampleType)); } else { wxASSERT(mFile); int rval = (int) mFile->Read(data, (size_t) len); if (rval != len) { printf("Expected %d bytes, got %d\n", len, rval); wxASSERT(0); } mPos += rval; return rval; } }
int main(void){ Lowpass filters[2]; int i; SNDFILE *infile = NULL; SNDFILE *outfile = NULL; SF_INFO sfinfo; sf_count_t readcount; int filetype = SF_FORMAT_WAV | SF_FORMAT_PCM_16; short buffer[BLOCK_SIZE * 2]; // Open a wav file to filter if((infile = sf_open ("music.wav", SFM_READ, &sfinfo)) == NULL) { printf ("Not able to open input file\n"); puts (sf_strerror (NULL)) ; return 1; } // Say info about the input file printf("Frames: %d\n", (int)sfinfo.frames); printf("Samplerate: %d\n", sfinfo.samplerate); printf("Channels: %d\n", sfinfo.channels); sfinfo.format = filetype; // Open output file if((outfile = sf_open ("output.wav", SFM_WRITE, &sfinfo)) == NULL) { printf ("Not able to open input file\n"); puts (sf_strerror (NULL)) ; return 1; } // Read and filter the file while((readcount = sf_readf_short(infile, buffer, BLOCK_SIZE)) > 0){ for(i = 0; i < readcount * 2; i += 2){ buffer[i] = filters[0].value(buffer[i]); buffer[i+1] = filters[1].value(buffer[i+1]); //printf("%d ", buffer[i]); //printf("%d ", buffer[i+1]); } sf_writef_short(outfile, buffer, BLOCK_SIZE); } // Close the files sf_close(infile); sf_close(outfile); return 0; }
static int mitel_cm7291_side_2_and_bellcore_tests(void) { int j; SNDFILE *inhandle; int frames; ademco_contactid_sender_state_t *sender; logging_state_t *logging; if ((sender = ademco_contactid_sender_init(NULL, talkoff_tx_callback, NULL)) == NULL) return -1; logging = ademco_contactid_sender_get_logging_state(sender); span_log_set_level(logging, SPAN_LOG_SHOW_SEVERITY | SPAN_LOG_SHOW_PROTOCOL | SPAN_LOG_FLOW); span_log_set_tag(logging, "Ademco-tx"); tx_callback_reported = FALSE; /* The remainder of the Mitel tape is the talk-off test */ /* Here we use the Bellcore test tapes (much tougher), in six files - 1 from each side of the original 3 cassette tapes */ /* Bellcore say you should get no more than 470 false detections with a good receiver. Dialogic claim 20. Of course, we can do better than that, eh? */ printf("Talk-off test\n"); for (j = 0; bellcore_files[j][0]; j++) { if ((inhandle = sf_open_telephony_read(bellcore_files[j], 1)) == NULL) { printf(" Cannot open speech file '%s'\n", bellcore_files[j]); return -1; } while ((frames = sf_readf_short(inhandle, amp, SAMPLE_RATE))) { ademco_contactid_sender_rx(sender, amp, frames); } if (sf_close_telephony(inhandle)) { printf(" Cannot close speech file '%s'\n", bellcore_files[j]); return -1; } printf(" File %d gave %d false hits.\n", j + 1, 0); } if (tx_callback_reported) { printf(" Failed\n"); return -1; } printf(" Passed\n"); return 0; }
void SoundFileStream::load( SNDFILE *sf, const SF_INFO &info, sf_count_t beg, sf_count_t dur ) { delete[] _data; _dataOffset = beg; _dataSize = dur; _data = new short [_dataSize * info.channels]; sf_seek( sf, _dataOffset, SEEK_SET); _dataSize = sf_readf_short( sf, _data, _dataSize ); _ch = info.channels; _beg = _dataOffset; _dur = _dataSize; }
bool SndFileAudioFileReader::run(AudioProcessor& processor) { if (input_file_ == nullptr) { return false; } const int BUFFER_SIZE = 16384; short input_buffer[BUFFER_SIZE]; sf_count_t frames_to_read = BUFFER_SIZE / info_.channels; sf_count_t frames_read = frames_to_read; sf_count_t total_frames_read = 0; bool success = true; success = processor.init(info_.samplerate, info_.channels, BUFFER_SIZE); if (success) { showProgress(0, info_.frames); while (success && frames_read == frames_to_read) { frames_read = sf_readf_short( input_file_, input_buffer, frames_to_read ); success = processor.process( input_buffer, static_cast<int>(frames_read) ); total_frames_read += frames_read; showProgress(total_frames_read, info_.frames); } output_stream << "\nRead " << total_frames_read << " frames\n"; processor.done(); } close(); return success; }
UInt32 SFB::Audio::LibsndfileDecoder::_ReadAudio(AudioBufferList *bufferList, UInt32 frameCount) { sf_count_t framesRead = 0; switch(mReadMethod) { case ReadMethod::Unknown: /* Do nothing */ break; case ReadMethod::Short: framesRead = sf_readf_short(mFile.get(), (short *)bufferList->mBuffers[0].mData, frameCount); break; case ReadMethod::Int: framesRead = sf_readf_int(mFile.get(), (int *)bufferList->mBuffers[0].mData, frameCount); break; case ReadMethod::Float: framesRead = sf_readf_float(mFile.get(), (float *)bufferList->mBuffers[0].mData, frameCount); break; case ReadMethod::Double: framesRead = sf_readf_double(mFile.get(), (double *)bufferList->mBuffers[0].mData, frameCount); break; } bufferList->mBuffers[0].mDataByteSize = (UInt32)(framesRead * mFormat.mBytesPerFrame); bufferList->mBuffers[0].mNumberChannels = mFormat.mChannelsPerFrame; return (UInt32)framesRead; }
Sample* ZInstrument::readSample(const QString& s, QZipReader* uz) { if (uz) { QList<QZipReader::FileInfo> fi = uz->fileInfoList(); buf = uz->fileData(s); if (buf.isEmpty()) { printf("Sample::read: cannot read sample data <%s>\n", qPrintable(s)); return 0; } } else { QFile f(s); if (!f.open(QIODevice::ReadOnly)) { printf("Sample::read: open <%s> failed\n", qPrintable(s)); return 0; } buf = f.readAll(); } SF_INFO info; memset(&info, 0, sizeof(info)); idx = 0; SNDFILE* sf = sf_open_virtual(&sfio, SFM_READ, &info, this); if (sf == 0) { printf("open <%s> failed: %s\n", qPrintable(s), sf_strerror(0)); return 0; } short* data = new short[(info.frames + 3) * info.channels]; int channel = info.channels; int frames = info.frames; int sr = info.samplerate; Sample* sa = new Sample(channel, data, frames, sr); if (info.frames != sf_readf_short(sf, data + channel, frames)) { printf("Sample read failed: %s\n", sf_strerror(sf)); delete[] data; delete sa; sa = 0; } for (int i = 0; i < channel; ++i) { data[i] = data[channel + i]; data[(frames-1) * channel + i] = data[(frames-3) * channel + i]; data[(frames-2) * channel + i] = data[(frames-3) * channel + i]; } sf_close(sf); return sa; }
// ---------------------------------------------------------------------------- bool ofOpenALSoundPlayer_TimelineAdditions::sfReadFile(string path, vector<short> & buffer, vector<float> & fftAuxBuffer){ SF_INFO sfInfo; SNDFILE* f = sf_open(path.c_str(),SFM_READ,&sfInfo); if(!f){ ofLog(OF_LOG_ERROR,"ofOpenALSoundPlayer_TimelineAdditions: couldnt read " + path); return false; } buffer.resize(sfInfo.frames*sfInfo.channels); fftAuxBuffer.resize(sfInfo.frames*sfInfo.channels); int subformat = sfInfo.format & SF_FORMAT_SUBMASK ; if (subformat == SF_FORMAT_FLOAT || subformat == SF_FORMAT_DOUBLE){ double scale ; sf_command (f, SFC_CALC_SIGNAL_MAX, &scale, sizeof (scale)) ; if (scale < 1e-10) scale = 1.0 ; else scale = 32700.0 / scale ; sf_count_t samples_read = sf_read_float (f, &fftAuxBuffer[0], fftAuxBuffer.size()); if(samples_read<(int)fftAuxBuffer.size()) ofLog(OF_LOG_ERROR,"ofOpenALSoundPlayer_TimelineAdditions: couldnt read " + path); for (int i = 0 ; i < int(fftAuxBuffer.size()) ; i++){ fftAuxBuffer[i] *= scale ; buffer[i] = 32565.0 * fftAuxBuffer[i]; } }else{ sf_count_t frames_read = sf_readf_short(f,&buffer[0],sfInfo.frames); if(frames_read<sfInfo.frames){ ofLog(OF_LOG_ERROR,"ofOpenALSoundPlayer_TimelineAdditions: couldnt read buffer for " + path); return false; } sf_seek(f,0,SEEK_SET); frames_read = sf_readf_float(f,&fftAuxBuffer[0],sfInfo.frames); if(frames_read<sfInfo.frames){ ofLog(OF_LOG_ERROR,"ofOpenALSoundPlayer_TimelineAdditions: couldnt read fft buffer for " + path); return false; } } sf_close(f); channels = sfInfo.channels; duration = float(sfInfo.frames) / float(sfInfo.samplerate); samplerate = sfInfo.samplerate; return true; }
void test_readf_short_or_die (SNDFILE *file, int pass, short *test, sf_count_t frames, int line_num) { sf_count_t count ; if ((count = sf_readf_short (file, test, frames)) != frames) { printf ("\n\nLine %d", line_num) ; if (pass > 0) printf (" (pass %d)", pass) ; printf (" : sf_readf_short failed with short readf (%ld => %ld).\n", SF_COUNT_TO_LONG (frames), SF_COUNT_TO_LONG (count)) ; fflush (stdout) ; puts (sf_strerror (file)) ; exit (1) ; } ; return ; } /* test_readf_short */