当前位置: 首页>>代码示例>>C++>>正文


C++ sf_readf_float函数代码示例

本文整理汇总了C++中sf_readf_float函数的典型用法代码示例。如果您正苦于以下问题:C++ sf_readf_float函数的具体用法?C++ sf_readf_float怎么用?C++ sf_readf_float使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。


在下文中一共展示了sf_readf_float函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。

示例1: wav_read_samples

int wav_read_samples(struct wav_file *wav, float *buff, int count)
{
    int samples_read, i, j;

    /* If the source is mono, then simply read directly into the output buffer */
    if(wav->file_info.channels == 1)
        return sf_readf_float(wav->file_handle, buff, count);

    /* Otherwise resize internal buffer to hold stereo samples if necessary... */
    if(wav->buff_size < count * wav->file_info.channels)
    {
        wav->buff_size = count * wav->file_info.channels;
        if( !(wav->raw_buff = realloc(wav->raw_buff, wav->buff_size * sizeof(float))))
            return -1;
    }

    /* ...and read samples for all channels interleaved, then convert to mono. */
    samples_read = sf_readf_float(wav->file_handle, wav->raw_buff, count);
    for(i = 0; i < samples_read; i++)
    {
        buff[i] = 0;
        for(j = 0; j < wav->file_info.channels; j++)
            buff[i] += wav->raw_buff[i*wav->file_info.channels+j];
        buff[i] /= wav->file_info.channels;
    }

    return samples_read;
}
开发者ID:pdkelly,项目名称:audiograph,代码行数:28,代码来源:wav.c

示例2: file_buffer_worker

void file_buffer_worker(void *ctx)
{
    AudioFile::pimpl *fileImpl = (AudioFile::pimpl *)ctx;
    int writeBuffer = (fileImpl->currentBufIndex == 0) ? 1 : 0;
    sf_seek(fileImpl->sndfile, fileImpl->framesBuffered, SF_SEEK_SET);
    size_t read = sf_readf_float(fileImpl->sndfile, fileImpl->bufs[writeBuffer], FRAMES_PER_FILE_BUFFER);
    fileImpl->framesBuffered += read;
    if (read < FRAMES_PER_FILE_BUFFER)
    {
        if (fileImpl->looping)
        {
            sf_seek(fileImpl->sndfile, 0, SF_SEEK_SET);
            size_t samplesDidRead = read * fileImpl->sfInfo.channels;
            size_t framesToRead = (FRAMES_PER_FILE_BUFFER - read);
            sf_readf_float(fileImpl->sndfile, &(fileImpl->bufs[writeBuffer][samplesDidRead]), framesToRead);
            fileImpl->framesBuffered = framesToRead;
        }
        else
        {
            fileImpl->needsBuffer = false;
        }
    }
    
    fileImpl->isBuffering = false;
}
开发者ID:lukehabermehl,项目名称:blockdsp,代码行数:25,代码来源:autil_file.cpp

示例3: sf_open

static float *load_interleaved_samples(const wchar_t *filename, SF_INFO *sf_info){
  SNDFILE *sndfile          = sf_open(STRING_get_chars(filename),SFM_READ,sf_info);
  if(sndfile==NULL)
    return NULL;

  float   *ret              = talloc_atomic(sizeof(float) * sf_info->channels * sf_info->frames);
  int      allocated_frames = sf_info->frames;

  int total_read_frames     = sf_readf_float(sndfile, ret, sf_info->frames);

  if(total_read_frames==0)
    return NULL;

  while(true){
    float samples[1024*sf_info->channels];
    int read_now = sf_readf_float(sndfile, samples, 1024);
    if(read_now==0)
      break;

    if(total_read_frames + read_now > allocated_frames){
      allocated_frames = (total_read_frames+read_now) * 2;
      ret = talloc_realloc(ret, allocated_frames * sizeof(float) * sf_info->channels);
    }

    memcpy(ret + (total_read_frames*sf_info->channels), samples, sizeof(float)*1024*sf_info->channels);

    total_read_frames += read_now;
  }

  sf_close(sndfile);

  sf_info->frames = total_read_frames;
  return ret;
}
开发者ID:renno23,项目名称:radium,代码行数:34,代码来源:Sampler_plugin.c

示例4: WARNING

/* if channels is -1, then all channels are read into buffer
 (interleaved).  buf should be big enough to hold them all */
nframes_t
Audio_File_SF::read ( sample_t *buf, int channel, nframes_t len )
{
    if ( len > 256 * 100 )
        WARNING( "warning: attempt to read an insane number of frames (%lu) from soundfile\n", (unsigned long)len );

//    printf( "len = %lu, channels = %d\n", len, _channels );

    lock();

    nframes_t rlen;

    if ( _channels == 1 || channel == -1 )
        rlen = sf_readf_float( _in, buf, len );
    else
    {
        sample_t *tmp = new sample_t[ len * _channels ];

        rlen = sf_readf_float( _in, tmp, len );

        /* extract the requested channel */
        for ( unsigned int i = channel; i < rlen * _channels; i += _channels )
            *(buf++) = tmp[ i ];

        delete[] tmp;
    }

    _current_read += rlen;

    unlock();

    return rlen;
}
开发者ID:elthariel,项目名称:non-daw,代码行数:35,代码来源:Audio_File_SF.C

示例5: assert

int SoundStream::updateState(double time, double dt){
   int status = PV_SUCCESS;
   assert(fileStream);
    
    // if (time >= nextSampleTime) {
    //     nextSampleTime += (1.0 / sampleRate);
       //Read 1 frame
       int numRead = sf_readf_float(fileStream, soundBuf, 1);
       //EOF
       if(numRead == 0){
          sf_seek(fileStream, 0, SEEK_SET);
          numRead = sf_readf_float(fileStream, soundBuf, 1);
          if(numRead == 0){
             fprintf(stderr, "SoundStream:: Fatal error, is the file empty?\n");
             exit(EXIT_FAILURE);
          }
          std::cout << "Rewinding sound file\n";
       }
       else if(numRead > 1){
          fprintf(stderr, "SoundStream:: Fatal error, numRead is bigger than 1\n");
          exit(EXIT_FAILURE);
       }
       for(int fi = 0; fi < getLayerLoc()->nf; fi++){
          soundData[fi] = soundBuf[fi];
       }
   // }
    
   return status;
}
开发者ID:PetaVision,项目名称:Projects,代码行数:29,代码来源:SoundStream.cpp

示例6: ra_sound_read_float

static void ra_sound_read_float(RA_SOUND *snd, RA_BUFFER *buf, sf_count_t frames) {
    static float temp[1024];
    int temp_len = 1024;
    float *data = (float*)buf->data;
    float 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_float(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_float(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_float(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;
}
开发者ID:zmack,项目名称:ruby-audio,代码行数:58,代码来源:ra_sound.c

示例7: playbackThread

void playbackThread(PaStream* stream, char* sampleBlock, SNDFILE* filePtr, bool* stopPlaybackThread, std::mutex* stopPlaybackThread_mutex, bool* playbackThreadFinishedPlaying, std::mutex* playbackThreadFinishedPlaying_mutex){
    PaError err;
    //uses mutex to be thread safe. stopPlaybackThread could change at any time
    playbackThreadFinishedPlaying_mutex->lock();
    *playbackThreadFinishedPlaying = false;
    playbackThreadFinishedPlaying_mutex->unlock();
    stopPlaybackThread_mutex->lock();
    *stopPlaybackThread = false;
    bool stop = *stopPlaybackThread;
    stopPlaybackThread_mutex->unlock();
    long frames = FRAMES_PER_BUFFER;
    memset(sampleBlock, 0, FRAMES_PER_BUFFER*NUM_CHANNELS*SAMPLE_SIZE);
    while (!stop && frames == FRAMES_PER_BUFFER) {
        frames = sf_readf_float(filePtr, (float*)sampleBlock, FRAMES_PER_BUFFER);
        
        err = Pa_WriteStream( stream, sampleBlock, FRAMES_PER_BUFFER );
        if( err != paNoError ){
            printError("Error in worker thread:");
            printPaError(err);
        }

        stopPlaybackThread_mutex->lock();
        stop = *stopPlaybackThread;
        stopPlaybackThread_mutex->unlock();
    }
    playbackThreadFinishedPlaying_mutex->lock();
    *playbackThreadFinishedPlaying = true;
    playbackThreadFinishedPlaying_mutex->unlock();
    //printLog("* Worker ending!");
}
开发者ID:malt3,项目名称:Audiologger,代码行数:30,代码来源:Player.cpp

示例8: sf_seek

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;
}
开发者ID:8c6794b6,项目名称:supercollider,代码行数:29,代码来源:filestream.cpp

示例9: sp_ftbl_loadfile

int sp_ftbl_loadfile(sp_data *sp, sp_ftbl **ft, const char *filename)
{
    *ft = malloc(sizeof(sp_ftbl));
    sp_ftbl *ftp = *ft;
    SF_INFO info;
    memset(&info, 0, sizeof(SF_INFO));
    info.format = 0;
    SNDFILE *snd = sf_open(filename, SFM_READ, &info);
    if(snd == NULL) {
        return SP_NOT_OK;
    }
    size_t size = info.frames * info.channels;

    ftp->tbl = malloc(sizeof(SPFLOAT) * (size + 1));

    sp_ftbl_init(sp, ftp, size);

#ifdef USE_DOUBLE
    sf_readf_double(snd, ftp->tbl, ftp->size);
#else
    sf_readf_float(snd, ftp->tbl, ftp->size);
#endif
    sf_close(snd);
    return SP_OK;
}
开发者ID:carlosypunto,项目名称:AudioKit,代码行数:25,代码来源:ftbl.c

示例10: print

/*!	Read in a soundfile.
	The Array returned is one dimensional. Multi-channel soundfiles return multi-component arrays.
	
	<luacode>
	local sf = audio.soundfile(LuaAV.findfile("gong.wav"))
	print(sf)							--> Array
	print(sf.components)				--> 2 (stereo)
	print(sf.dim[1])					--> 265776 (sample frames)
	print(sf.dim[1]/audio.samplerate)	--> 6.03 (seconds)
	</luacode>
		
	@param soundfile path (string)
	@ret Array containing the soundfile data
	@name M.read
*/
int lua_soundfile_read(lua_State * L) {
	const char * path = luaL_checkstring(L, 1);
	
	SNDFILE *sf;
	SF_INFO sfinfo;
	
	sf = sf_open(path, SFM_READ, &sfinfo);
	if (sf == NULL) luaL_error(L, "failed to open soundfile %s", path);
	
	// call Array constructor with appropriate format:
	lua_pushcfunction(L, Glue<al::ArrayWrapper>::create);
	lua_pushinteger(L, sfinfo.channels);	
	lua_pushinteger(L, AlloFloat32Ty);
	lua_createtable(L, 1, 0);
	lua_pushinteger(L, sfinfo.frames);
	lua_rawseti(L, -2, 1);
	lua_call(L, 3, 1);
	
	al::ArrayWrapper * a = Glue<al::ArrayWrapper>::checkto(L, -1);

	// copy data in:
	sf_readf_float(sf, (float *)a->data.ptr, sfinfo.frames);
	
	sf_close(sf);
	
	return 1;
}
开发者ID:LuaAV,项目名称:LuaAV,代码行数:42,代码来源:luaav_audio_soundfile.cpp

示例11: sf_open

float* Util::loadSound(const std::string& fileName, int& size)
{
	SF_INFO soundInfo;
	SNDFILE* file = sf_open(fileName.c_str(), SFM_READ, &soundInfo);

	if(!file)
	{
		printf("Failed to open sound file");
		return 0;
	}



	sf_count_t frames = soundInfo.frames * soundInfo.channels;
	size = frames;

	float* data = new float[frames];

	sf_readf_float(file, data, frames);

	/*
	for(int i = 0; i < frames; i++)
	{

	}
	*/

	sf_close(file);

	return data;
}
开发者ID:JanneRemes,项目名称:janityengine,代码行数:31,代码来源:UtilWin.cpp

示例12: sa_sndfile_read

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;
}
开发者ID:TagPro-PreciousRoy,项目名称:minimodem,代码行数:33,代码来源:simpleaudio-sndfile.c

示例13: exit

sf_count_t Processor::read_frames(size_t start){
    size_t count = m_bufsize;
    sf_count_t readCount = 0;
    if (!m_file || !m_channelCount) {
        std::cerr << "no file or no channel" << std::endl;
        return 0;
    }
    if ((long)start >= m_fileInfo.frames) {
        std::cerr << "end of file" << std::endl;
	    return 0;
    }
    if (long(start + m_bufsize) > m_fileInfo.frames) {
	    count = m_fileInfo.frames - start;
    }
    if (sf_seek(m_file, start, SEEK_SET) < 0) {
        std::cerr << "sf_seek failed" << std::endl;
	    exit(EXIT_FAILURE);
	}
    if ((readCount = sf_readf_float(m_file, m_buffer, count)) < 0) {
        std::cerr << "sf_readf_float failed" << std::endl;
	    exit(EXIT_FAILURE);
	}
    //std::cout << readCount << std::endl;
    return readCount;
}
开发者ID:giliam,项目名称:data_analysis_soundcloud,代码行数:25,代码来源:Processor.cpp

示例14: while

size_t SndFileDecoder::read(char *buffer, size_t bytes)
{
    short *out = (short*)buffer;
    size_t frames = bytes / SndInfo.channels / 2;
    size_t total = 0;

    // It seems libsndfile has a bug with converting float samples from Vorbis
    // to the 16-bit shorts we use, which causes some PCM samples to overflow
    // and wrap, creating static. So instead, read the samples as floats and
    // convert to short ourselves.
    // Use a loop to convert a handful of samples at a time, avoiding a heap
    // allocation for temporary storage. 64 at a time works, though maybe it
    // could be more.
    while(total < frames)
    {
        size_t todo = MIN<size_t>(frames-total, 64/SndInfo.channels);
        float tmp[64];

        size_t got = (size_t)sf_readf_float(SndFile, tmp, todo);
        if(got < todo) frames = total + got;

        for(size_t i = 0;i < got*SndInfo.channels;i++)
            *out++ = (short)xs_CRoundToInt(clamp(tmp[i] * 32767.f, -32768.f, 32767.f));
        total += got;
    }
    return total * SndInfo.channels * 2;
}
开发者ID:ArcticPheenix,项目名称:gzdoom,代码行数:27,代码来源:sndfile_decoder.cpp

示例15: locker

void
WavFileReader::getInterleavedFrames(size_t start, size_t count,
				    SampleBlock &results) const
{
    if (count == 0) return;
    results.clear();
    results.reserve(count * m_fileInfo.channels);

    QMutexLocker locker(&m_mutex);

    if (!m_file || !m_channelCount) {
        return;
    }

    if ((long)start >= m_fileInfo.frames) {
//        SVDEBUG << "WavFileReader::getInterleavedFrames: " << start
//                  << " > " << m_fileInfo.frames << endl;
	return;
    }

    if (long(start + count) > m_fileInfo.frames) {
	count = m_fileInfo.frames - start;
    }

    sf_count_t readCount = 0;

    if (start != m_lastStart || count != m_lastCount) {

	if (sf_seek(m_file, start, SEEK_SET) < 0) {
//            std::cerr << "sf_seek failed" << std::endl;
	    return;
	}
	
	if (count * m_fileInfo.channels > m_bufsiz) {
//	    std::cerr << "WavFileReader: Reallocating buffer for " << count
//		      << " frames, " << m_fileInfo.channels << " channels: "
//		      << m_bufsiz << " floats" << std::endl;
	    m_bufsiz = count * m_fileInfo.channels;
	    delete[] m_buffer;
	    m_buffer = new float[m_bufsiz];
	}
	
	if ((readCount = sf_readf_float(m_file, m_buffer, count)) < 0) {
//            std::cerr << "sf_readf_float failed" << std::endl;
	    return;
	}

	m_lastStart = start;
	m_lastCount = readCount;
    }

    for (size_t i = 0; i < count * m_fileInfo.channels; ++i) {
        if (i >= m_bufsiz) {
            std::cerr << "INTERNAL ERROR: WavFileReader::getInterleavedFrames: " << i << " >= " << m_bufsiz << std::endl;
        }
	results.push_back(m_buffer[i]);
    }

    return;
}
开发者ID:iopenstack,项目名称:sonic-annotator,代码行数:60,代码来源:WavFileReader.cpp


注:本文中的sf_readf_float函数示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。