static void alsa_play (int argc, char *argv []) { static float buffer [BUFFER_LEN] ; SNDFILE *sndfile ; SF_INFO sfinfo ; snd_pcm_t * alsa_dev ; int k, readcount, subformat ; for (k = 1 ; k < argc ; k++) { memset (&sfinfo, 0, sizeof (sfinfo)) ; printf ("Playing %s\n", argv [k]) ; if (! (sndfile = sf_open (argv [k], SFM_READ, &sfinfo))) { puts (sf_strerror (NULL)) ; continue ; } ; if (sfinfo.channels < 1 || sfinfo.channels > 2) { printf ("Error : channels = %d.\n", sfinfo.channels) ; continue ; } ; if ((alsa_dev = alsa_open (sfinfo.channels, (unsigned) sfinfo.samplerate, SF_FALSE)) == NULL) continue ; subformat = sfinfo.format & SF_FORMAT_SUBMASK ; if (subformat == SF_FORMAT_FLOAT || subformat == SF_FORMAT_DOUBLE) { double scale ; int m ; sf_command (sndfile, SFC_CALC_SIGNAL_MAX, &scale, sizeof (scale)) ; if (scale < 1e-10) scale = 1.0 ; else scale = 32700.0 / scale ; while ((readcount = sf_read_float (sndfile, buffer, BUFFER_LEN))) { for (m = 0 ; m < readcount ; m++) buffer [m] *= scale ; alsa_write_float (alsa_dev, buffer, BUFFER_LEN / sfinfo.channels, sfinfo.channels) ; } ; } else { while ((readcount = sf_read_float (sndfile, buffer, BUFFER_LEN))) alsa_write_float (alsa_dev, buffer, BUFFER_LEN / sfinfo.channels, sfinfo.channels) ; } ; snd_pcm_drain (alsa_dev) ; snd_pcm_close (alsa_dev) ; sf_close (sndfile) ; } ; return ; } /* alsa_play */
void IQFileAudioSource::getSamples(Ipp32f* samples_buf) { int len = sf_read_float(_f, samples_buf, 2*_nsamples); if(len == 0) { sf_seek(_f, 0, SEEK_SET); len = sf_read_float(_f, samples_buf, _nsamples); if(!_loop) _done = true; } usleep(_block_delay); }
std::vector<fpoint> get_samples_from_file(std::string filename) { const int BUFFER_LEN = 1024; static float data[BUFFER_LEN]; std::vector<fpoint> out; SNDFILE *infile; SF_INFO sfinfo; sfinfo.channels = 1; sfinfo.samplerate = Fs; sfinfo.format = SF_FORMAT_WAV | SF_FORMAT_FLOAT; int readcount; filename.insert(0, "samples/"); filename.append(".wav"); if (! (infile = sf_open(filename.c_str(), SFM_READ, &sfinfo))) { std::cout << "no file named " << filename << std::endl; return out; } while ((readcount = sf_read_float(infile, data, BUFFER_LEN))) { for(int i = 0; i < readcount; ++i) { out.push_back(data[i]); } } sf_close (infile); return out; }
static OSStatus macosx_audio_out_callback (AudioDeviceID device, const AudioTimeStamp* current_time, const AudioBufferList* data_in, const AudioTimeStamp* time_in, AudioBufferList* data_out, const AudioTimeStamp* time_out, void* client_data) { MacOSXAudioData *audio_data ; int k, size, sample_count, read_count ; float *buffer, rate = 44100 ; audio_data = (MacOSXAudioData*) client_data ; size = data_out->mBuffers[0].mDataByteSize ; sample_count = size / sizeof(float) ; buffer = (float*) data_out->mBuffers [0].mData ; read_count = sf_read_float (audio_data->sndfile, buffer, sample_count) ; if (read_count < sample_count) { memset (&(buffer [read_count]), 0, (sample_count - read_count) * sizeof (float)) ; /* Tell the main application to terminate. */ audio_data->done_playing = SF_TRUE ; } ; return noErr ; } /* macosx_audio_out_callback */
//------------------------------------------------------------ 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; }
//----------------------------------------------------------------------------- // name: int ReadSoundFile( char filename[], TS_FLOAT * data, int datasize ) // desc: Reads given sound file into data array //----------------------------------------------------------------------------- int TreesynthIO::ReadSoundFile( char filename[], TS_FLOAT * data, int datasize ) { if( !sfread ) { sfread = sf_open( filename, SFM_READ, &info ); if( !sfread ) { std::cerr << "TreesynthIO::ReadSoundFile : cannot open file '" << filename << "', quitting" << std::endl; char x[256]; std::cin.getline( x, 256 ); exit(1); } } datasize = rm_next_length; sf_seek( sfread, rm_next_pos, SEEK_SET ); std::cerr << sfread << " " << data << " " << datasize << std::endl; int itemsread = sf_read_float( sfread, data, datasize ); set_next_pos( filename ); // rt audio /*if( !audio_initialize( info.samplerate ) ) { // 44100 std::cerr << "TreesynthIO::ReadSoundFile : cannot open audio interface, quitting" << std::endl; char x[256]; std::cin.getline( x, 256 ); exit(1); }*/ return itemsread; }
void reverse_audio_file(char* filepath, char* fileOutputPath){ SF_INFO info; SNDFILE *input, *output; float *samples; int len, i; /* open input file */ input = sf_open(filepath, SFM_READ, &info); /* allocate buffer */ len = info.frames; samples = malloc(sizeof(float) * len); /* read in input file */ sf_read_float(input, samples, len); /* close the input file */ sf_close(input); /* open output file */ output = sf_open(fileOutputPath, SFM_WRITE, &info); /* write samples in reverse order to output file */ for (i=len-1; i>=0; i--) { sf_write_float(output, &samples[i], 1); } /* close output file */ sf_close(output); /* deallocate buffer */ free(samples); }
long int lp_sndfile_in::read_frames(float *buffer, long int len) { pv_sf_len = (sf_count_t)len; pv_sf_len = sf_read_float(pv_snd_fd, buffer, pv_sf_len); pv_sf_pos = pv_sf_pos + pv_sf_len; return (long int)pv_sf_len; }
soundfile * readsoundfilechunk(char *path, int start, int len) { soundfile * ret; SF_INFO info; SNDFILE *snd; memset(&info, 0, sizeof(info)); if ( (snd = sf_open(path, SFM_READ, &info)) == NULL ) diem("Couldn't open sound file for reading", path); if ( info.channels != 1 ) diem("Sound file has more than one channel", path); if ( (ret = malloc(sizeof(*ret))) == NULL ) die("Couldn't malloc space for soundfile"); if ( (ret->data = malloc(sizeof(float)*len)) == NULL ) die("Couldn't malloc space for sound buffer"); sf_seek(snd, start, SEEK_SET); int actuallen = sf_read_float(snd, ret->data, len); // assumption: channel count is 1, verified above ret->length = actuallen; ret->samplerate = info.samplerate; if ( sf_close(snd) ) diem("Couldn't close sound file", path); return ret; }
int OlaRandom::ReadSoundFile( char filename[] ) { // open file if( sfread ) { sf_close( sfread ); } sfread = sf_open( filename, SFM_READ, &readinfo ); if( !sfread ) { BB_log( BB_LOG_SEVERE, "[TreesynthIO]: Could not open input file '%s', %s", filename, sf_error_number( sf_error( sfread ) ) ); return 0; } strcpy( ifilename, filename ); // determine number of buffers needed origsize = readinfo.frames; std::cerr << "frames: " << origsize << std::endl; SAFE_DELETE_ARRAY(origbuffer); origbuffer = new float[origsize]; // read sf_seek( sfread, 0, SEEK_SET ); int itemsread = sf_read_float( sfread, origbuffer, origsize); sf_close( sfread ); sfread = NULL; return itemsread; }
int prSFRead(struct VMGlobals *g, int numArgsPushed) { PyrSlot *a, *b; a = g->sp - 1; b = g->sp; SNDFILE *file = (SNDFILE*)slotRawPtr(&slotRawObject(a)->slots[0]); if (!isKindOfSlot(b, class_rawarray)) return errWrongType; switch (slotRawObject(b)->obj_format) { case obj_int16 : slotRawObject(b)->size = (int)sf_read_short(file, (short*)slotRawInt8Array(b)->b, slotRawObject(b)->size); break; case obj_int32 : slotRawObject(b)->size = (int)sf_read_int(file, (int*)slotRawInt8Array(b)->b, slotRawObject(b)->size); break; case obj_float : slotRawObject(b)->size = (int)sf_read_float(file, (float*)slotRawInt8Array(b)->b, slotRawObject(b)->size); break; case obj_double : slotRawObject(b)->size = (int)sf_read_double(file, (double*)slotRawInt8Array(b)->b, slotRawObject(b)->size); break; default: error("sample format not supported.\n"); return errFailed; } return errNone; }
static int opensoundsys_play (int argc, char *argv []) { static short buffer [BUFFER_LEN] ; SNDFILE *sndfile ; SF_INFO sfinfo ; int k, audio_device, readcount, writecount, subformat ; for (k = 1 ; k < argc ; k++) { memset (&sfinfo, 0, sizeof (sfinfo)) ; printf ("Playing %s\n", argv [k]) ; if (! (sndfile = sf_open (argv [k], SFM_READ, &sfinfo))) { puts (sf_strerror (NULL)) ; continue ; } ; if (sfinfo.channels < 1 || sfinfo.channels > 2) { printf ("Error : channels = %d.\n", sfinfo.channels) ; continue ; } ; audio_device = opensoundsys_open_device (sfinfo.channels, sfinfo.samplerate) ; subformat = sfinfo.format & SF_FORMAT_SUBMASK ; if (subformat == SF_FORMAT_FLOAT || subformat == SF_FORMAT_DOUBLE) { static float float_buffer [BUFFER_LEN] ; double scale ; int m ; sf_command (sndfile, SFC_CALC_SIGNAL_MAX, &scale, sizeof (scale)) ; if (scale < 1e-10) scale = 1.0 ; else scale = 32700.0 / scale ; while ((readcount = sf_read_float (sndfile, float_buffer, BUFFER_LEN))) { for (m = 0 ; m < readcount ; m++) buffer [m] = scale * float_buffer [m] ; writecount = write (audio_device, buffer, readcount * sizeof (short)) ; } ; } else { while ((readcount = sf_read_short (sndfile, buffer, BUFFER_LEN))) writecount = write (audio_device, buffer, readcount * sizeof (short)) ; } ; if (ioctl (audio_device, SNDCTL_DSP_POST, 0) == -1) perror ("ioctl (SNDCTL_DSP_POST) ") ; if (ioctl (audio_device, SNDCTL_DSP_SYNC, 0) == -1) perror ("ioctl (SNDCTL_DSP_SYNC) ") ; close (audio_device) ; sf_close (sndfile) ; } ; return writecount ; } /* opensoundsys_play */
f_cnt_t SampleBuffer::decodeSampleSF( const char * _f, int_sample_t * & _buf, ch_cnt_t & _channels, sample_rate_t & _samplerate ) { SNDFILE * snd_file; SF_INFO sf_info; f_cnt_t frames = 0; bool sf_rr = false; sample_t * fbuf = 0; if( ( snd_file = sf_open( _f, SFM_READ, &sf_info ) ) != NULL ) { frames = sf_info.frames; // check if float if ( (sf_info.format & SF_FORMAT_SUBMASK) == SF_FORMAT_FLOAT ) // if yes, use float format for buffer { fbuf = new sample_t[sf_info.channels * frames]; sf_rr = sf_read_float( snd_file, fbuf, sf_info.channels * frames ); } else // otherwise, use int { _buf = new int_sample_t[sf_info.channels * frames]; sf_rr = sf_read_short( snd_file, _buf, sf_info.channels * frames ); } if( sf_rr < sf_info.channels * frames ) { #ifdef DEBUG_LMMS printf( "SampleBuffer::decodeSampleSF(): could not read" " sample %s: %s\n", _f, sf_strerror( NULL ) ); #endif } _channels = sf_info.channels; _samplerate = sf_info.samplerate; sf_close( snd_file ); } else { #ifdef DEBUG_LMMS printf( "SampleBuffer::decodeSampleSF(): could not load " "sample %s: %s\n", _f, sf_strerror( NULL ) ); #endif } //write down either directly or convert i->f depending on file type if ( frames > 0 && fbuf != NULL ) { directFloatWrite ( fbuf, frames, _channels); } else if ( frames > 0 && _buf != NULL ) { convertIntToFloat ( _buf, frames, _channels); } return frames; }
sf_count_t xsf_read_float(SNDFILE *sndfile, float *ptr, sf_count_t items) { sf_count_t err = sf_read_float(sndfile, ptr, items); if(err < 0) { xsf_handle_error(sndfile); } return err; }
long int lp_sndfile_in::read_samples(float *buffer, long int len) { pv_sf_len = (sf_count_t)(len / (long int)pv_sf_info.channels); pv_sf_len = sf_read_float(pv_snd_fd, buffer, pv_sf_len); pv_sf_pos = pv_sf_pos + pv_sf_len; pv_len = ((long int)pv_sf_len * (long int)pv_sf_info.channels); return pv_len; }
static OSStatus macosx_audio_out_callback (AudioDeviceID device, const AudioTimeStamp* current_time, const AudioBufferList* data_in, const AudioTimeStamp* time_in, AudioBufferList* data_out, const AudioTimeStamp* time_out, void* client_data) { MacOSXAudioData *audio_data ; int size, sample_count, read_count, k ; float *buffer ; /* Prevent compiler warnings. */ device = device ; current_time = current_time ; data_in = data_in ; time_in = time_in ; time_out = time_out ; audio_data = (MacOSXAudioData*) client_data ; size = data_out->mBuffers [0].mDataByteSize ; sample_count = size / sizeof (float) ; buffer = (float*) data_out->mBuffers [0].mData ; if (audio_data->fake_stereo != 0) { read_count = sf_read_float (audio_data->sndfile, buffer, sample_count / 2) ; for (k = read_count - 1 ; k >= 0 ; k--) { buffer [2 * k ] = buffer [k] ; buffer [2 * k + 1] = buffer [k] ; } ; read_count *= 2 ; } else read_count = sf_read_float (audio_data->sndfile, buffer, sample_count) ; /* Fill the remainder with zeroes. */ if (read_count < sample_count) { if (audio_data->fake_stereo == 0) memset (&(buffer [read_count]), 0, (sample_count - read_count) * sizeof (float)) ; /* Tell the main application to terminate. */ audio_data->done_playing = SF_TRUE ; } ; return noErr ; } /* macosx_audio_out_callback */
// TODO: Add streaming support. bool SoundCtor(JSContext *ctx, unsigned argc, JS::Value *vp){ JS::CallArgs args = CallArgsFromVp(argc, vp); if(!Turbo::CheckForSingleArg(ctx, args, Turbo::String, __func__)) return false; struct Turbo::JSStringHolder<> file(ctx, JS_EncodeString(ctx, args[0].toString())); const std::string full_path = std::string(TS_GetContextEnvironment(ctx)->directories->sound) + file.string; if(!t5::IsFile(full_path)){ Turbo::SetError(ctx, std::string(BRACKNAME " SoundCtor Error no such file ") + file.string); return false; } SF_INFO info; SNDFILE *sound_file = sf_open(full_path.c_str(), SFM_READ, &info); //sf_command(sound_file, SFC_SET_SCALE_FLOAT_INT_READ, nullptr, SF_TRUE); if(!sound_file){ Turbo::SetError(ctx, std::string(BRACKNAME " SoundCtor Error could not open file ") + file.string); return false; } int iters = 0; Sound *sound = nullptr; if(player.supportsFloat32()){ float buffer[0x8000]; sound = new Sound(player.load((float *)nullptr, 0, info.channels, info.samplerate, info.frames)); while(unsigned long this_read = sf_read_float(sound_file, buffer, 0x10000)){ player.addToSound(sound, buffer, SamplesToBytes(this_read)); iters++; } } else if(player.supportsInt16()){ short buffer[0x10000]; sound = new Sound(player.load((short *)nullptr, 0, info.channels, info.samplerate, info.frames)); while(unsigned long this_read = sf_read_short(sound_file, buffer, 0x10000)){ player.addToSound(sound, buffer, SamplesToBytes(this_read)); iters++; } } else{ puts(BRACKNAME " Error bad player on this platform"); } printf(BRACKNAME " SoundCtor Info loaded file %s in %i iterations\n", file.string, iters); sf_close(sound_file); args.rval().set(OBJECT_TO_JSVAL(sound_proto.wrap(ctx, sound))); return true; }
void lsamp_add_file(lsamp_data *ls, const char *filename, const char *keyword) { sqlite3_stmt *pStmt; int rc; int rowid; int count; int bufsize = LSBUFSIZE; int blob_offset = 0; if(!lsamp_open_sfile(ls, filename)) { return; } rc = sqlite3_prepare_v2(ls->db, "INSERT INTO lsamp(keyword, data) VALUES(?,ZEROBLOB(?));", -1, &pStmt, NULL); if(rc) { fprintf(stderr, "SQL Error: %s\n", sqlite3_errmsg(ls->db)); return; } sqlite3_bind_text(pStmt, 1, keyword, -1, SQLITE_STATIC); sqlite3_bind_int(pStmt, 2, ls->sfinfo.frames * sizeof(LSFLOAT)); rc = sqlite3_step(pStmt); rc = sqlite3_finalize(pStmt); sqlite3_blob *pBlob; rc = sqlite3_blob_open(ls->db, "main", "lsamp", "data", sqlite3_last_insert_rowid(ls->db), 1, &pBlob); if(rc) { fprintf(stderr, "Error opening blob: %s\n", sqlite3_errmsg(ls->db)); return; } count = ls->sfinfo.frames; while(count != 0) { #ifdef LS_DEBUG fprintf(stderr, "Reading buffersize %d.\n", bufsize); fflush(stderr); #endif bufsize = sf_read_float(ls->sfile, ls->buf, bufsize); rc = sqlite3_blob_write(pBlob, ls->buf, bufsize * sizeof(LSFLOAT), blob_offset * sizeof(LSFLOAT)); #ifdef LS_DEBUG fprintf(stderr, "%d bytes Written. Writing %d bytes.\n", blob_offset, bufsize); fflush(stderr); #endif if(rc) { fprintf(stderr, "SQL Error: %s\n", sqlite3_errmsg(ls->db)); return; } blob_offset += bufsize; count -= bufsize; } lsamp_close_sfile(ls); sqlite3_blob_close(pBlob); }
static void play_file (const char *filename) { SNDFILE *sndFile; SF_INFO sfInfo; sf_count_t count; PaError error; PaStreamParameters outputParameters; PaStream *stream; // open file if (! (sndFile = sf_open(filename, SFM_READ, &sfInfo))) { printf("Error: could not open file: %s\n", filename); } // initialize portaudio Pa_Initialize(); outputParameters.device = Pa_GetDefaultOutputDevice(); outputParameters.channelCount = sfInfo.channels; outputParameters.sampleFormat = paFloat32; outputParameters.suggestedLatency = 0.2; outputParameters.hostApiSpecificStreamInfo = 0; error = Pa_OpenStream( &stream, NULL, &outputParameters, sfInfo.samplerate, paFramesPerBufferUnspecified, paNoFlag, NULL, NULL); if (error) { printf("Failed to open output, error: %i\n", error); Pa_Terminate(); } error = Pa_StartStream(stream); if (error) { printf("Failed to start stream: %i\n", error); } // read and play file in segments int buffer_channel = BUFFER_LEN * sfInfo.channels; float *buffer = malloc(buffer_channel * sizeof(float)); while ((count = sf_read_float(sndFile, buffer, buffer_channel)) > 0) { error = Pa_WriteStream(stream, buffer, BUFFER_LEN); } // clean up free(buffer); Pa_StopStream(stream); Pa_CloseStream(stream); Pa_Terminate(); sf_close(sndFile); }
CircularBuffer * __read_audio_file(AudioOptions audio_options) { struct stat st; SF_INFO sf_info; long wav_file_size; float * buffer = NULL; CircularBuffer * input_buffer = NULL; SNDFILE * wav_file = NULL; if (stat(audio_options.wav_path, &st) == 0) { wav_file_size = st.st_size; } else { printf("Could not open wav file: %s\n", audio_options.wav_path); fflush(stdout); goto error; } input_buffer = CircularBuffer_create(wav_file_size); printf("Opening wave file %s with size %ld\n", audio_options.wav_path, wav_file_size); if (!(wav_file = sf_open(audio_options.wav_path, SFM_READ, &sf_info))) { printf("Could not open wav file: %s\n", audio_options.wav_path); fflush(stdout); goto error; } buffer = (float *)malloc(sizeof(float) * WAV_BUFFER_SIZE); if (!buffer) { sf_close(wav_file); goto error; } int readcount; while ((readcount = sf_read_float(wav_file, buffer, WAV_BUFFER_SIZE))) { CircularBuffer_produce_blocking(input_buffer, buffer, readcount); } free(buffer); sf_close(wav_file); return input_buffer; error: if (buffer) { free(buffer); } if (input_buffer) { CircularBuffer_destroy(input_buffer); } if (wav_file) { sf_close(wav_file); } return NULL; }
bool StreamTrack::stream(ALuint buffer) { BOOST_ASSERT(m_audioEngine->getSettings().stream_buffer_size >= m_sfInfo.channels - 1); #ifdef AUDIO_OPENAL_FLOAT std::vector<ALfloat> pcm(m_audioEngine->getSettings().stream_buffer_size); #else std::vector<ALshort> pcm(m_audioEngine->getSettings().stream_buffer_size); #endif size_t size = 0; // SBS - C + 1 is important to avoid endless loops if the buffer size isn't a multiple of the channels while(size < pcm.size() - m_sfInfo.channels + 1) { // we need to read a multiple of sf_info.channels here const size_t samplesToRead = (m_audioEngine->getSettings().stream_buffer_size - size) / m_sfInfo.channels * m_sfInfo.channels; #ifdef AUDIO_OPENAL_FLOAT const sf_count_t samplesRead = sf_read_float(m_sndFile, pcm.data() + size, samplesToRead); #else const sf_count_t samplesRead = sf_read_short(m_sndFile, pcm.data() + size, samplesToRead); #endif if(samplesRead > 0) { BOOST_ASSERT(static_cast<std::make_unsigned<sf_count_t>::type>(samplesRead) <= std::numeric_limits<size_t>::max()); size += static_cast<size_t>(samplesRead); continue; } int error = sf_error(m_sndFile); if(error != SF_ERR_NO_ERROR) { logSndfileError(error); return false; } if(m_streamType == StreamType::Background) { sf_seek(m_sndFile, 0, SEEK_SET); } else { break; // Stream is ending - do nothing. } } if(size == 0) return false; alBufferData(buffer, m_format, pcm.data(), static_cast<ALsizei>(size * sizeof(pcm[0])), m_rate); DEBUG_CHECK_AL_ERROR(); return true; }
static int ReadSndSamples(Sound *s, Tcl_Interp *interp, Tcl_Channel ch, char *ibuf, float *obuf, int len) { /* fprintf(stderr, "ReadSndSample(..., %d)\n", len); */ if( ( ch == NULL ) || ( sf_error((SNDFILE*) ch) != 0 ) ) { /* fprintf(stderr, "ReadSndSample ERROR %d\n", sf_error((SNDFILE*) ch)); */ return -1; } /* int nframes = len / Snack_GetNumChannels(s); */ /* nframes = sf_readf_float ((SNDFILE*) ch, obuf, nframes); */ /* return nframes * Snack_GetNumChannels(s); */ return sf_read_float ((SNDFILE*) ch, obuf, len); }
bool ofxSoundFile::sfReadFile(ofSoundBuffer & buffer){ samples_read = sf_read_float (sndFile, &buffer[0], buffer.size()); /*if(samples_read<(int)buffer.size()){ ofLogError() << "ofxSoundFile: couldnt read " << path; return false; }*/ if (subformat == SF_FORMAT_FLOAT || subformat == SF_FORMAT_DOUBLE){ for (int i = 0 ; i < int(buffer.size()) ; i++){ buffer[i] *= scale ; } } return true; }
// ---------------------------------------------------------------------------- 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 SampleStream::getSamples(const char* file, float *buf, int channel, int startFrame, int endFrame) { int numChannelsInFile = sfinfo.channels; if(numChannelsInFile < channel+1) { cout << "Error: " << file << " doesn't contain requested channel" << endl; return 1; } int frameLen = endFrame-startFrame; if(frameLen <= 0 || startFrame < 0 || endFrame <= 0 || endFrame > sfinfo.frames) { cout << "Error: " << file << " invalid frame range requested" << endl; return 1; } sf_seek(sndfile,startFrame,SEEK_SET); float* tempBuf = new float[frameLen*numChannelsInFile]; int subformat = sfinfo.format & SF_FORMAT_SUBMASK; int readcount = sf_read_float(sndfile, tempBuf, frameLen*numChannelsInFile); //FIXME // Pad with zeros in case we couldn't read whole file for(int k = readcount; k <frameLen*numChannelsInFile; k++) tempBuf[k] = 0; if (subformat == SF_FORMAT_FLOAT || subformat == SF_FORMAT_DOUBLE) { double scale ; int m ; sf_command (sndfile, SFC_CALC_SIGNAL_MAX, &scale, sizeof (scale)) ; if (scale < 1e-10) scale = 1.0 ; else scale = 32700.0 / scale ; cout << "File samples scale = " << scale << endl; for (m = 0; m < frameLen; m++) tempBuf[m] *= scale; } for(int n=0;n<frameLen;n++) buf[n] = tempBuf[n*numChannelsInFile+channel]; delete[] tempBuf; return 0; }
int Wave::readData() { size = inHeader.frames * inHeader.channels; data = (float *) malloc(size * sizeof(float)); if (data == NULL) { gLog("[wave] unable to allocate memory\n"); return 0; } if (sf_read_float(fileIn, data, size) != size) gLog("[wave] warning: incomplete read!\n"); sf_close(fileIn); return 1; }
void SoundFile::loadFloatSamples(SNDFILE *f) { QScopedArrayPointer<float> buffer(new float[Length]); double scale; sf_command(f, SFC_CALC_SIGNAL_MAX, &scale, sizeof(scale)) ; if (scale < 1e-10) scale = 1.0 ; else scale = 32700.0 / scale; int readcount = sf_read_float(f, buffer.data(), Length); for (int m = 0; m < readcount; ++m) Data[m] = (short int)(scale * buffer[m]); }
/* Reques for writing length data */ static void sdlLibsndfileCb(void *userdata, Uint8 *stream, int len) { /* Just empty buffer for no reason.. fun yea */ memset( stream, 0x00, len); /* Read with libsndfile */ #if SDL_MAJOR_VERSION == 2 m_iReadcount = sf_read_float(m_SInfile, (float *)stream, len / 4); #else m_iReadcount = sf_read_short(m_SInfile, (short int *)stream, len / 2); #endif if( m_iReadcount <= 0 ) { l_iLoop = 1; } }
void write_sample(SNDFILE *snd, FILE *fp, char *wav, char *name, float *buf, int bufsize, uint32_t *pos, int sr) { SF_INFO info; info.format = 0; int count; SNDFILE *in = sf_open(wav, SFM_READ, &info); fprintf(fp, "[%s]\npos = %g\n", name, 1.0 * (*pos) / sr); uint32_t start = *pos; while((count = sf_read_float(in, buf, bufsize))) { sf_write_float(snd, buf, count); *pos += count; } fprintf(fp, "size = %g\n\n", (1.0 * (*pos) - start) / sr); sf_close(in); }