f_cnt_t SampleBuffer::decodeSampleSF( const char * _f, sample_t * & _buf, ch_cnt_t & _channels, sample_rate_t & _samplerate ) { SNDFILE * snd_file; SF_INFO sf_info; sf_info.format = 0; f_cnt_t frames = 0; bool sf_rr = false; if( ( snd_file = sf_open( _f, SFM_READ, &sf_info ) ) != NULL ) { frames = sf_info.frames; _buf = new sample_t[sf_info.channels * frames]; sf_rr = sf_read_float( snd_file, _buf, sf_info.channels * frames ); if( sf_rr < sf_info.channels * frames ) { #ifdef DEBUG_LMMS qDebug( "SampleBuffer::decodeSampleSF(): could not read" " sample %s: %s", _f, sf_strerror( NULL ) ); #endif } _channels = sf_info.channels; _samplerate = sf_info.samplerate; sf_close( snd_file ); } else { #ifdef DEBUG_LMMS qDebug( "SampleBuffer::decodeSampleSF(): could not load " "sample %s: %s", _f, sf_strerror( NULL ) ); #endif } //write down either directly or convert i->f depending on file type if ( frames > 0 && _buf != NULL ) { directFloatWrite ( _buf, frames, _channels); } return frames; }
int main(int argc, char *argv[]) { printf("Wav Read Test\n"); SF_INFO sndInfo; SNDFILE *sndFile = sf_open(argv[1], SFM_READ, &sndInfo); if (sndFile == NULL) { fprintf(stderr, "Error reading source file '%s': %s\n", argv[1], sf_strerror(sndFile)); return 1; } // Check format - 16bit PCM if (sndInfo.format != (SF_FORMAT_WAV | SF_FORMAT_PCM_16)) { fprintf(stderr, "Input should be 16bit Wav\n"); sf_close(sndFile); return 1; } // Allocate memory double *waveTable = malloc(sndInfo.channels * sndInfo.frames * sizeof(double)); if (waveTable == NULL) { fprintf(stderr, "Could not allocate memory for data\n"); sf_close(sndFile); return 1; } // Load data long numFrames = sf_readf_double(sndFile, waveTable, sndInfo.channels*sndInfo.frames); long i; for(i=0;i<sndInfo.frames;i+=10000){ printf("Sample %d :\t%f \n",i,waveTable[i]); } // Output Info printf("Read %ld frames from %s, Sample rate: %d, Length: %fs\n", numFrames, argv[1], sndInfo.samplerate, (float)numFrames/sndInfo.samplerate); long size = 38+5*BLOCKSIZE+MFCC_FREQ_BANDS; double *FVec; sf_close(sndFile); double *wavetable=malloc(BLOCKSIZE*sizeof(double));//{0}; for(i=0;i<numFrames;i+=BLOCKSIZE){ wavetable=&(waveTable[i]); FVec = extractall(wavetable,sndInfo.frames,sndInfo.samplerate); } for(i=0;i<size;i++) printf("%f\t",FVec[i]); return 0; }
/* mast_fill_input_buffer() Make sure input buffer if full of audio */ size_t mast_fill_input_buffer( MastAudioBuffer* buffer ) { size_t total_read = 0; int frames_wanted = buffer->get_write_space(); int frames_read = 0; if (frames_wanted==0) { // No audio wanted MAST_DEBUG( "Tried to fill buffer when it is full" ); return 0; } // Loop until buffer is full while( frames_wanted > 0 ) { frames_wanted = buffer->get_write_space(); frames_read = sf_readf_float( g_input_file, buffer->get_write_ptr(), frames_wanted ); // Add on the frames read if (frames_read > 0) { buffer->add_frames( frames_read ); total_read += frames_read; } // Reached end of file? if (frames_read < frames_wanted) { MAST_DEBUG("Reached end of file (frames_wanted=%d, frames_read=%d)", frames_wanted, frames_read); if (g_loop_file) { // Seek back to the beginning if (sf_seek( g_input_file, 0, SEEK_SET )) { MAST_ERROR("Failed to seek to start of file: %s", sf_strerror( g_input_file ) ); return 0; } } else { // Reached end of file (and don't want to loop) break; } } } if (total_read == 0) { MAST_ERROR("Failed to read from file: %s", sf_strerror( g_input_file ) ); } return total_read; }
static void encode_file (const char *infilename, const char *outfilename, int filetype) { static float buffer [BUFFER_LEN] ; SNDFILE *infile, *outfile ; SF_INFO sfinfo ; int k, readcount ; printf (" %s -> %s ", infilename, outfilename) ; fflush (stdout) ; k = 16 - strlen (outfilename) ; PUT_DOTS (k) ; if (! (infile = sf_open (infilename, SFM_READ, &sfinfo))) { printf ("Error : could not open file : %s\n", infilename) ; puts (sf_strerror (NULL)) ; exit (1) ; } sfinfo.format = filetype ; if (! sf_format_check (&sfinfo)) { sf_close (infile) ; printf ("Invalid encoding\n") ; return ; } ; if (! (outfile = sf_open (outfilename, SFM_WRITE, &sfinfo))) { printf ("Error : could not open file : %s\n", outfilename) ; puts (sf_strerror (NULL)) ; exit (1) ; } ; while ((readcount = sf_read_float (infile, buffer, BUFFER_LEN)) > 0) sf_write_float (outfile, buffer, BUFFER_LEN) ; sf_close (infile) ; sf_close (outfile) ; printf ("ok\n") ; return ; } /* encode_file */
bool BufReadCmd::Stage2() { #ifdef NO_LIBSNDFILE SendFailure(&mReplyAddress, "/b_read", "scsynth compiled without libsndfile\n"); scprintf("scsynth compiled without libsndfile\n"); return false; #else SF_INFO fileinfo; SndBuf *buf = World_GetNRTBuf(mWorld, mBufIndex); int framesToEnd = buf->frames - mBufOffset; if (framesToEnd <= 0) return true; SNDFILE* sf = sf_open(mFilename, SFM_READ, &fileinfo); if (!sf) { char str[512]; sprintf(str, "File '%s' could not be opened: %s\n", mFilename, sf_strerror(NULL)); SendFailureWithIntValue(&mReplyAddress, "/b_read", str, mBufIndex); //SendFailure(&mReplyAddress, "/b_read", str); scprintf(str); return false; } if (fileinfo.channels != buf->channels) { char str[512]; sf_close(sf); sprintf(str, "Channel mismatch. File '%s' has %d channels. Buffer has %d channels.\n", mFilename, fileinfo.channels, buf->channels); SendFailureWithIntValue(&mReplyAddress, "/b_read", str, mBufIndex); //SendFailure(&mReplyAddress, "/b_read", str); scprintf(str); return false; } if (mFileOffset < 0) mFileOffset = 0; else if (mFileOffset > fileinfo.frames) mFileOffset = fileinfo.frames; if (mNumFrames < 0 || mNumFrames + mFileOffset > fileinfo.frames) mNumFrames = fileinfo.frames - mFileOffset; if (mNumFrames > framesToEnd) mNumFrames = framesToEnd; sf_seek(sf, mFileOffset, SEEK_SET); if (mNumFrames > 0) { sf_readf_float(sf, buf->data + (mBufOffset * buf->channels), mNumFrames); } if(buf->sndfile) sf_close(buf->sndfile); if (mLeaveFileOpen) { buf->sndfile = sf; } else { sf_close(sf); buf->sndfile = 0; } mSampleRate = (double)fileinfo.samplerate; return true; #endif }
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 */
int main(int argc, char **argv) { MastSendTool *tool = NULL; SF_INFO sfinfo; // Create the send tool object tool = new MastSendTool( MAST_TOOL_NAME ); tool->enable_scheduling(); // Parse the command line arguments // and configure the session parse_cmd_line( argc, argv, tool ); // Open the input file by filename memset( &sfinfo, 0, sizeof(sfinfo) ); g_input_file = sf_open(g_filename, SFM_READ, &sfinfo); if (g_input_file == NULL) MAST_FATAL("Failed to open input file:\n%s", sf_strerror(NULL)); tool->set_input_channels( sfinfo.channels ); tool->set_input_samplerate( sfinfo.samplerate ); // Display some information about the input file print_file_info( g_input_file, &sfinfo ); // Setup signal handlers mast_setup_signals(); // Run the main loop tool->run(); // Clean up delete tool; // Close input file if (sf_close( g_input_file )) { MAST_ERROR("Failed to close input file:\n%s", sf_strerror(g_input_file)); } // Success ! return 0; }
SndFile(Reader reader) : file(NULL), reader(buffer.frontReader()) { info.format = 0; buffer.resize(reader.resource().size() - reader.position()); reader.read(buffer.data(), buffer.size()); file = sf_open_virtual(ioInterface(), SFM_READ, &info, this); if (!file) throw std::runtime_error(std::string(sf_strerror(NULL))); }
static void error_close_test (void) { static short buffer [SHORT_BUFFER] ; const char *filename = "error_close.wav" ; SNDFILE *sndfile ; SF_INFO sfinfo ; FILE *file ; print_test_name (__func__, filename) ; /* Open a FILE* from which we will extract a file descriptor. */ if ((file = fopen (filename, "w")) == NULL) { printf ("\n\nLine %d : fopen returned NULL.\n", __LINE__) ; exit (1) ; } ; /* Set parameters for writing the file. */ memset (&sfinfo, 0, sizeof (sfinfo)) ; sfinfo.channels = 1 ; sfinfo.samplerate = 44100 ; sfinfo.format = SF_FORMAT_WAV | SF_FORMAT_PCM_16 ; sndfile = sf_open_fd (fileno (file), SFM_WRITE, &sfinfo, SF_TRUE) ; if (sndfile == NULL) { printf ("\n\nLine %d : sf_open_fd failed : %s\n", __LINE__, sf_strerror (NULL)) ; exit (1) ; } ; test_write_short_or_die (sndfile, 0, buffer, ARRAY_LEN (buffer), __LINE__) ; /* Now close the fd associated with file before calling sf_close. */ fclose (file) ; if (sf_close (sndfile) == 0) { #if OS_IS_WIN32 OSVERSIONINFOEX osvi ; memset (&osvi, 0, sizeof (OSVERSIONINFOEX)) ; osvi.dwOSVersionInfoSize = sizeof (OSVERSIONINFOEX) ; if (GetVersionEx ((OSVERSIONINFO *) &osvi)) { printf ("\n\nLine %d : sf_close should not have returned zero.\n", __LINE__) ; printf ("\nHowever, this is a known bug in version %d.%d of windows so we'll ignore it.\n\n", (int) osvi.dwMajorVersion, (int) osvi.dwMinorVersion) ; } ; #else printf ("\n\nLine %d : sf_close should not have returned zero.\n", __LINE__) ; exit (1) ; #endif } ; unlink (filename) ; puts ("ok") ; } /* error_close_test */
static int broadcast_dump (const char *filename) { SNDFILE *file ; SF_INFO sfinfo ; SF_BROADCAST_INFO_2K bext ; double time_ref_sec ; int got_bext ; memset (&sfinfo, 0, sizeof (sfinfo)) ; if ((file = sf_open (filename, SFM_READ, &sfinfo)) == NULL) { printf ("Error : Not able to open input file %s.\n", filename) ; fflush (stdout) ; memset (data, 0, sizeof (data)) ; puts (sf_strerror (NULL)) ; return 1 ; } ; memset (&bext, 0, sizeof (SF_BROADCAST_INFO_2K)) ; got_bext = sf_command (file, SFC_GET_BROADCAST_INFO, &bext, sizeof (bext)) ; sf_close (file) ; if (got_bext == SF_FALSE) { printf ("Error : File '%s' does not contain broadcast information.\n\n", filename) ; return 1 ; } ; /* ** From : http://www.ebu.ch/en/technical/publications/userguides/bwf_user_guide.php ** ** Time Reference: ** This field is a count from midnight in samples to the first sample ** of the audio sequence. */ time_ref_sec = ((pow (2.0, 32) * bext.time_reference_high) + (1.0 * bext.time_reference_low)) / sfinfo.samplerate ; printf ("Description : %.*s\n", (int) sizeof (bext.description), bext.description) ; printf ("Originator : %.*s\n", (int) sizeof (bext.originator), bext.originator) ; printf ("Origination ref : %.*s\n", (int) sizeof (bext.originator_reference), bext.originator_reference) ; printf ("Origination date : %.*s\n", (int) sizeof (bext.origination_date), bext.origination_date) ; printf ("Origination time : %.*s\n", (int) sizeof (bext.origination_time), bext.origination_time) ; if (bext.time_reference_high == 0 && bext.time_reference_low == 0) printf ("Time ref : 0\n") ; else printf ("Time ref : 0x%x%08x (%.6f seconds)\n", bext.time_reference_high, bext.time_reference_low, time_ref_sec) ; printf ("BWF version : %d\n", bext.version) ; printf ("UMID : %.*s\n", (int) sizeof (bext.umid), bext.umid) ; printf ("Coding history : %.*s\n", bext.coding_history_size, bext.coding_history) ; return 0 ; } /* broadcast_dump */
WavFileReader::WavFileReader(FileSource source, bool fileUpdating) : m_file(0), m_source(source), m_path(source.getLocalFilename()), m_buffer(0), m_bufsiz(0), m_lastStart(0), m_lastCount(0), m_updating(fileUpdating) { m_frameCount = 0; m_channelCount = 0; m_sampleRate = 0; m_fileInfo.format = 0; m_fileInfo.frames = 0; m_file = sf_open(m_path.toLocal8Bit(), SFM_READ, &m_fileInfo); if (!m_file || (!fileUpdating && m_fileInfo.channels <= 0)) { std::cerr << "WavFileReader::initialize: Failed to open file at \"" << m_path << "\" (" << sf_strerror(m_file) << ")" << std::endl; if (m_file) { m_error = QString("Couldn't load audio file '%1':\n%2") .arg(m_path).arg(sf_strerror(m_file)); } else { m_error = QString("Failed to open audio file '%1'") .arg(m_path); } return; } if (m_fileInfo.channels > 0) { m_frameCount = m_fileInfo.frames; m_channelCount = m_fileInfo.channels; m_sampleRate = m_fileInfo.samplerate; } // std::cerr << "WavFileReader: Frame count " << m_frameCount << ", channel count " << m_channelCount << ", sample rate " << m_sampleRate << std::endl; }
void testApp::loadSoundFiles(){ //assume libsndfile looks in the folder where the app is run //therefore ../../../ gets to the bin folder //we then need dat/sounds/to get to the sound folder //this is different to the usual OF default folder const char *infilename = "../../../data/03Ingenue.wav";//PicturesMixer6.aif";// //ac_guitar_final.wav string loadfilename = "03Ingenue.wav";//PicturesMixer6.aif"; loadedAudio.loadSound(loadfilename); // Open Input File with lib snd file if (! (infile = sf_open (infilename, SFM_READ, &sfinfo))) { // Open failed printf ("SF OPEN routine Not able to open input file %s.\n", infilename) ; // Print the error message from libsndfile. puts (sf_strerror (NULL)) ; } else{ printf("SF OPEN opened file %s okay.\n", infilename); sndfileInfoString = "Opened okay "; }; //end adam //end libsndfile load part const char *outfilename = "../../../output.wav" ; // output file name // Open the output file. if (! (outfile = sf_open (outfilename, SFM_WRITE, &sfinfo))) { printf ("Not able to open output file %s.\n", outfilename) ; sndfileInfoString = "NOT OPENED"; puts (sf_strerror (NULL)) ; } ; }
static void linux_play (int argc, char *argv []) { static short buffer [BUFFER_LEN] ; SNDFILE *sndfile ; SF_INFO sfinfo ; int k, audio_device, 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 ; } ; audio_device = linux_open_dsp_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] ; write (audio_device, buffer, readcount * sizeof (short)) ; } ; } else { while ((readcount = sf_read_short (sndfile, buffer, BUFFER_LEN))) write (audio_device, buffer, readcount * sizeof (short)) ; } ; sf_close (sndfile) ; close (audio_device) ; } ; return ; } /* linux_play */
uint_t aubio_sink_sndfile_close (aubio_sink_sndfile_t *s) { if (!s->handle) { return AUBIO_FAIL; } if (sf_close(s->handle)) { AUBIO_ERR("sink_sndfile: Error closing file %s: %s", s->path, sf_strerror (NULL)); return AUBIO_FAIL; } s->handle = NULL; return AUBIO_OK; }
/* Return error string (or NULL) */ static const char* error_str_sndfile( audioin_t* audioin ) { SNDFILE* file = audioin->file; if (sf_error(file) == SF_ERR_NO_ERROR) { // No error return NULL; } else { // Return error string return sf_strerror( file ); } }
bool SndfileSoundLoader::Load(const char* path) { memset(&m_sfinfo, 0, sizeof(m_sfinfo)); m_sndfile = sf_open(path, SFM_READ, &m_sfinfo); if (m_sndfile == NULL) { const char* err = sf_strerror(NULL); wxLogWarning("Cannot open file %s: %s", path, err); return false; } wxLogMessage("Using libsndfile sound loader."); return true; }
void SoundSourceSndFile::close() { if (m_pSndFile) { const int closeResult = sf_close(m_pSndFile); if (0 == closeResult) { m_pSndFile = nullptr; } else { qWarning() << "Failed to close file:" << closeResult << sf_strerror(m_pSndFile) << getUrlString(); } } }
SINT SoundSourceSndFile::readSampleFrames( SINT numberOfFrames, CSAMPLE* sampleBuffer) { const sf_count_t readCount = sf_readf_float(m_pSndFile, sampleBuffer, numberOfFrames); if (0 <= readCount) { return readCount; } else { qWarning() << "Failed to read from libsnd file:" << readCount << sf_strerror(m_pSndFile); return 0; } }
static int cart_dump (const char *filename) { SNDFILE *file ; SF_INFO sfinfo ; SF_CART_INFO_VAR (1024) cart ; int got_cart, k ; memset (&sfinfo, 0, sizeof (sfinfo)) ; memset (&cart, 0, sizeof (cart)) ; if ((file = sf_open (filename, SFM_READ, &sfinfo)) == NULL) { printf ("Error : Not able to open input file %s.\n", filename) ; fflush (stdout) ; memset (data, 0, sizeof (data)) ; puts (sf_strerror (NULL)) ; return 1 ; } ; got_cart = sf_command (file, SFC_GET_CART_INFO, &cart, sizeof (cart)) ; sf_close (file) ; if (got_cart == SF_FALSE) { printf ("Error : File '%s' does not contain cart information.\n\n", filename) ; return 1 ; } ; printf ("Version : %.*s\n", (int) sizeof (cart.version), cart.version) ; printf ("Title : %.*s\n", (int) sizeof (cart.title), cart.title) ; printf ("Artist : %.*s\n", (int) sizeof (cart.artist), cart.artist) ; printf ("Cut id : %.*s\n", (int) sizeof (cart.cut_id), cart.cut_id) ; printf ("Category : %.*s\n", (int) sizeof (cart.category), cart.category) ; printf ("Classification : %.*s\n", (int) sizeof (cart.classification), cart.classification) ; printf ("Out cue : %.*s\n", (int) sizeof (cart.out_cue), cart.out_cue) ; printf ("Start date : %.*s\n", (int) sizeof (cart.start_date), cart.start_date) ; printf ("Start time : %.*s\n", (int) sizeof (cart.start_time), cart.start_time) ; printf ("End date : %.*s\n", (int) sizeof (cart.end_date), cart.end_date) ; printf ("End time : %.*s\n", (int) sizeof (cart.end_time), cart.end_time) ; printf ("App id : %.*s\n", (int) sizeof (cart.producer_app_id), cart.producer_app_id) ; printf ("App version : %.*s\n", (int) sizeof (cart.producer_app_version), cart.producer_app_version) ; printf ("User defined : %.*s\n", (int) sizeof (cart.user_def), cart.user_def) ; printf ("Level ref. : %d\n", cart.level_reference) ; printf ("Post timers :\n") ; for (k = 0 ; k < ARRAY_LEN (cart.post_timers) ; k++) if (cart.post_timers [k].usage [0]) printf (" %d %.*s %d\n", k, (int) sizeof (cart.post_timers [k].usage), cart.post_timers [k].usage, cart.post_timers [k].value) ; printf ("Reserved : %.*s\n", (int) sizeof (cart.reserved), cart.reserved) ; printf ("Url : %.*s\n", (int) sizeof (cart.url), cart.url) ; printf ("Tag text : %.*s\n", cart.tag_text_size, cart.tag_text) ; return 0 ; } /* cart_dump */
SndFile(const std::wstring& filename) : file(NULL), reader(buffer.frontReader()) { info.format = 0; #ifdef GOSU_IS_WIN loadFile(buffer, filename); file = sf_open_virtual(ioInterface(), SFM_READ, &info, this); #else file = sf_open(wstringToUTF8(filename).c_str(), SFM_READ, &info); #endif if (!file) throw std::runtime_error(std::string(sf_strerror(NULL))); }
/////////////////////////////////////////////////////////////////////////////////// // startRecording ------------------------------------------------------------------------ /////////////////////////////////////////////////////////////////////////////////// bool Echo::startRecording(SF_INFO * recordingInfo){ string tempFilapath = saveFolder+filename; outfile = sf_open (tempFilapath.c_str(), SFM_WRITE, recordingInfo) ; bool success; if (!outfile) { cerr<<"Error opening ["<<tempFilapath<<"] : "<<sf_strerror (outfile)<<endl; success = false; return success; } success = true; return success; }
SINT SoundSourceSndFile::seekSampleFrame( SINT frameIndex) { DEBUG_ASSERT(isValidFrameIndex(frameIndex)); const sf_count_t seekResult = sf_seek(m_pSndFile, frameIndex, SEEK_SET); if (0 <= seekResult) { return seekResult; } else { qWarning() << "Failed to seek libsnd file:" << seekResult << sf_strerror(m_pSndFile); return sf_seek(m_pSndFile, 0, SEEK_CUR); } }
static void sndio_play (int argc, char *argv []) { struct sio_hdl *hdl ; struct sio_par par ; short buffer [BUFFER_LEN] ; SNDFILE *sndfile ; SF_INFO sfinfo ; int k, readcount ; for (k = 1 ; k < argc ; k++) { 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 ((hdl = sio_open (NULL, SIO_PLAY, 0)) == NULL) { fprintf (stderr, "open sndio device failed") ; return ; } ; sio_initpar (&par) ; par.rate = sfinfo.samplerate ; par.pchan = sfinfo.channels ; par.bits = 16 ; par.sig = 1 ; par.le = SIO_LE_NATIVE ; if (! sio_setpar (hdl, &par) || ! sio_getpar (hdl, &par)) { fprintf (stderr, "set sndio params failed") ; return ; } ; if (! sio_start (hdl)) { fprintf (stderr, "sndio start failed") ; return ; } ; while ((readcount = sf_read_short (sndfile, buffer, BUFFER_LEN))) sio_write (hdl, buffer, readcount * sizeof (short)) ; sio_close (hdl) ; } ; return ; } /* sndio_play */
bool SOUNDFILESAVER_save(const char *filename, enum SOUNDFILESAVER_what what_to_save, float samplerate, int libsndfile_format, float post_recording_length, const char **error_string){ PlayStop(); { SF_INFO sf_info; memset(&sf_info,0,sizeof(sf_info)); sf_info.samplerate = samplerate; sf_info.channels = 2; sf_info.format = libsndfile_format; { g_sndfile = sf_open(filename,SFM_WRITE,&sf_info); if(g_sndfile==NULL){ printf("Why: \"%s\"\n",sf_strerror(NULL)); if(error_string!=NULL) *error_string = sf_strerror(NULL); return false; } } } g_saving_was_successful = true; g_filename = talloc_strdup(filename); g_post_writing_left = post_recording_length; g_save_state=BEFORE_WRITING; PaUtil_FullMemoryBarrier(); { MIXER_start_saving_soundfile(); if(what_to_save==SAVE_SONG) PlaySongFromStart(root->song->tracker_windows); else PlayBlockFromStart(root->song->tracker_windows,false); } PaUtil_FullMemoryBarrier(); g_save_state=IS_WRITING; return true; }
static void *ad_open_sndfile(const char *fn, struct adinfo *nfo) { sndfile_audio_decoder *priv = (sndfile_audio_decoder*) calloc(1, sizeof(sndfile_audio_decoder)); priv->sfinfo.format=0; if(!(priv->sffile = sf_open(fn, SFM_READ, &priv->sfinfo))){ dbg(0, "unable to open file '%s'.", fn); puts(sf_strerror(NULL)); int e = sf_error(NULL); dbg(0, "error=%i", e); free(priv); return NULL; } ad_info_sndfile(priv, nfo); return (void*) priv; }
static void filesystem_full_test (int format) { SNDFILE *file ; SF_INFO sfinfo ; struct stat buf ; const char *filename = "/dev/full", *errorstr ; int frames ; #if (defined (WIN32) || defined (_WIN32)) /* Can't run this test on Win32 so return. */ return ; #endif /* Make sure errno is zero before doing anything else. */ errno = 0 ; print_test_name ("filesystem_full_test", filename) ; if (stat (filename, &buf) != 0) { puts ("/dev/full missing") ; return ; } ; if (S_ISCHR (buf.st_mode) == 0 && S_ISBLK (buf.st_mode) == 0) { puts ("/dev/full is not a device file") ; return ; } ; sfinfo.samplerate = 44100 ; sfinfo.format = format ; sfinfo.channels = 1 ; sfinfo.frames = 0 ; frames = BUFFER_LEN / sfinfo.channels ; if ((file = sf_open (filename, SFM_WRITE, &sfinfo)) != NULL) { printf ("\n\nLine %d : Error, file should not have openned.\n", __LINE__ - 1) ; exit (1) ; } ; errorstr = sf_strerror (file) ; if (strstr (errorstr, " space ") == NULL || strstr (errorstr, "device") == NULL) { printf ("\n\nLine %d : Error bad error string : %s.\n", __LINE__ - 1, errorstr) ; exit (1) ; } ; puts ("ok") ; } /* filesystem_full_test */
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())); } } else { file_.reset(sf_open(filename_.c_str(), SFM_READ, &info_), sf_close); if (not file_) { Output::Error("libsndfile open error: %s", sf_strerror(NULL)); } } } 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; }
int main (void) { SNDFILE *file ; SF_INFO sfinfo ; int k ; int *buffer ; printf ("size int %d\n", sizeof (int)); printf ("size off_t %d\n", sizeof (off_t)); if (! (buffer = malloc (2 * SAMPLE_COUNT * sizeof (int)))) { printf ("Malloc failed.\n") ; exit (0) ; } ; memset (&sfinfo, 0, sizeof (sfinfo)) ; sfinfo.samplerate = SAMPLE_RATE ; sfinfo.frames = SAMPLE_COUNT ; sfinfo.channels = 2 ; sfinfo.format = (SF_FORMAT_WAV | SF_FORMAT_PCM_24) ; if (! (file = sf_open ("sine.wav", SFM_WRITE, &sfinfo))) { printf ("Error : Not able to open output file.\n") ; return 1 ; } ; if (sfinfo.channels == 1) { for (k = 0 ; k < SAMPLE_COUNT ; k++) buffer [k] = AMPLITUDE * sin (LEFT_FREQ * 2 * k * M_PI) ; } else if (sfinfo.channels == 2) { for (k = 0 ; k < SAMPLE_COUNT ; k++) { buffer [2 * k] = AMPLITUDE * sin (LEFT_FREQ * 2 * k * M_PI) ; buffer [2 * k + 1] = AMPLITUDE * sin (RIGHT_FREQ * 2 * k * M_PI) ; } ; } else { printf ("makesine can only generate mono or stereo files.\n") ; exit (1) ; } ; if (sf_write_int (file, buffer, sfinfo.channels * SAMPLE_COUNT) != sfinfo.channels * SAMPLE_COUNT) puts (sf_strerror (file)) ; sf_close (file) ; return 0 ; } /* main */
WriteWaveFile::WriteWaveFile(wxString filename, int sample_rate, int channels, unsigned long format, int type) : Filename(filename) { memset (&sfinfo, 0, sizeof (sfinfo)); sfinfo.samplerate = sample_rate; sfinfo.frames = 0; // dummy sfinfo.channels = channels; sfinfo.format = type | format; if (!(sffile = sf_open(filename.mb_str(*wxConvCurrent), SFM_WRITE, &sfinfo))) throw Error::File(filename, wxString(sf_strerror(0), *wxConvCurrent)); //WriteSoftware("Wired"); }
Result SoundSourceSndFile::tryOpen(const AudioSourceConfig& /*audioSrcCfg*/) { DEBUG_ASSERT(!m_pSndFile); SF_INFO sfInfo; memset(&sfInfo, 0, sizeof(sfInfo)); #ifdef __WINDOWS__ // Note: we cannot use QString::toStdWString since QT 4 is compiled with // '/Zc:wchar_t-' flag and QT 5 not const QString localFileName(QDir::toNativeSeparators(getLocalFileName())); const ushort* const fileNameUtf16 = localFileName.utf16(); static_assert(sizeof(wchar_t) == sizeof(ushort), "QString::utf16(): wchar_t and ushort have different sizes"); m_pSndFile = sf_wchar_open( reinterpret_cast<wchar_t*>(const_cast<ushort*>(fileNameUtf16)), SFM_READ, &sfInfo); #else m_pSndFile = sf_open(getLocalFileName().toLocal8Bit(), SFM_READ, &sfInfo); #endif if (!m_pSndFile) { // sf_format_check is only for writes qWarning() << "Error opening libsndfile file:" << getUrlString() << sf_strerror(m_pSndFile); return ERR; } if (sf_error(m_pSndFile) > 0) { qWarning() << "Error opening libsndfile file:" << getUrlString() << sf_strerror(m_pSndFile); return ERR; } setChannelCount(sfInfo.channels); setSamplingRate(sfInfo.samplerate); setFrameCount(sfInfo.frames); return OK; }