Exemplo n.º 1
0
bool SoundFile::openRead(const void* data, std::size_t sizeInBytes)
{
    // If the file is already opened, first close it
    if (m_file)
        sf_close(m_file);

    // Prepare the memory I/O structure
    SF_VIRTUAL_IO io;
    io.get_filelen = &Memory::getLength;
    io.read        = &Memory::read;
    io.seek        = &Memory::seek;
    io.tell        = &Memory::tell;

    // Initialize the memory data
    m_memory.begin   = static_cast<const char*>(data);
    m_memory.current = m_memory.begin;
    m_memory.size    = sizeInBytes;

    // Open the sound file
    SF_INFO fileInfo;
    fileInfo.format = 0;
    m_file = sf_open_virtual(&io, SFM_READ, &fileInfo, &m_memory);
    if (!m_file)
    {
        err() << "Failed to open sound file from memory (" << sf_strerror(m_file) << ")" << std::endl;
        return false;
    }

    // Initialize the internal state from the loaded information
    initialize(fileInfo);

    return true;
}
Exemplo n.º 2
0
////////////////////////////////////////////////////////////
/// /see sf::SoundFile::OpenRead
////////////////////////////////////////////////////////////
bool SoundFileDefault::OpenRead(const char* Data, std::size_t SizeInBytes, std::size_t& NbSamples, unsigned int& ChannelsCount, unsigned int& SampleRate)
{
    // If the file is already opened, first close it
    if (myFile)
        sf_close(myFile);

    // Define the I/O custom functions for reading from memory
    SF_VIRTUAL_IO VirtualIO;
    VirtualIO.get_filelen = &SoundFileDefault::MemoryGetLength;
    VirtualIO.read        = &SoundFileDefault::MemoryRead;
    VirtualIO.seek        = &SoundFileDefault::MemorySeek;
    VirtualIO.tell        = &SoundFileDefault::MemoryTell;
    VirtualIO.write       = &SoundFileDefault::MemoryWrite;

    // Initialize the memory data
    myMemory.DataStart = Data;
    myMemory.DataPtr   = Data;
    myMemory.TotalSize = SizeInBytes;

    // Open the sound file
    SF_INFO FileInfos;
    myFile = sf_open_virtual(&VirtualIO, SFM_READ, &FileInfos, &myMemory);
    if (!myFile)
    {
        std::cerr << "Failed to read sound file from memory" << std::endl;
        return false;
    }

    // Set the sound parameters
    ChannelsCount = FileInfos.channels;
    SampleRate    = FileInfos.samplerate;
    NbSamples     = static_cast<std::size_t>(FileInfos.frames) * ChannelsCount;

    return true;
}
Exemplo n.º 3
0
bool SoundFile::openRead(InputStream& stream)
{
    // If the file is already opened, first close it
    if (m_file)
        sf_close(m_file);

    // Prepare the memory I/O structure
    SF_VIRTUAL_IO io;
    io.get_filelen = &Stream::getLength;
    io.read        = &Stream::read;
    io.seek        = &Stream::seek;
    io.tell        = &Stream::tell;

    // Initialize the stream data
    m_stream.source = &stream;
    m_stream.size = stream.getSize();

    // Make sure that the stream's reading position is at the beginning
    stream.seek(0);

    // Open the sound file
    SF_INFO fileInfo;
    fileInfo.format = 0;
    m_file = sf_open_virtual(&io, SFM_READ, &fileInfo, &m_stream);
    if (!m_file)
    {
        err() << "Failed to open sound file from stream (" << sf_strerror(m_file) << ")" << std::endl;
        return false;
    }

    // Initialize the internal state from the loaded information
    initialize(fileInfo);

    return true;
}
Exemplo n.º 4
0
bool SoundFile::openRead(const void* data, std::size_t sizeInBytes)
{
    // If the file is already opened, first close it
    if (m_file)
        sf_close(m_file);

    // Prepare the memory I/O structure
    SF_VIRTUAL_IO io;
    io.get_filelen = &Memory::getLength;
    io.read        = &Memory::read;
    io.seek        = &Memory::seek;
    io.tell        = &Memory::tell;

    // Initialize the memory data
    m_memory.DataStart = static_cast<const char*>(data);
    m_memory.DataPtr   = m_memory.DataStart;
    m_memory.TotalSize = sizeInBytes;

    // Open the sound file
    SF_INFO fileInfos;
    m_file = sf_open_virtual(&io, SFM_READ, &fileInfos, &m_memory);
    if (!m_file)
    {
        err() << "Failed to open sound file from memory (" << sf_strerror(m_file) << ")" << std::endl;
        return false;
    }

    // Set the sound parameters
    m_channelCount = fileInfos.channels;
    m_sampleRate   = fileInfos.samplerate;
    m_sampleCount  = static_cast<std::size_t>(fileInfos.frames) * m_channelCount;

    return true;
}
Exemplo n.º 5
0
int EncoderWave::initEncoder(int samplerate, QString errorMessage) {

    // set sfInfo.
    // m_sfInfo.format is setup on setEncoderSettings previous to calling initEncoder.
    m_sfInfo.samplerate = samplerate;
    m_sfInfo.channels = 2;
    m_sfInfo.frames = 0;
    m_sfInfo.sections = 0;
    m_sfInfo.seekable = 0;

    // Opens a soundfile from a virtual file I/O context which is provided by the caller.
    // This is usually used to interface libsndfile to a stream or buffer based system.
    // Apart from the sfvirtual and the user_data parameters this function behaves like sf_open.
    m_pSndfile = sf_open_virtual (&m_virtualIo, SFM_WRITE, &m_sfInfo, m_pCallback) ;

    int ret=0;
    if (m_pSndfile == nullptr) {
        errorMessage = QString("Error initializing Wave recording. sf_open_virtual returned: ")
            +  sf_strerror(nullptr);
        qDebug() << errorMessage;
        ret = -1;
    } else {
        initStream();
    };
    return ret;
}
static void
sndfile_stream_decode(struct decoder *decoder, struct input_stream *is)
{
	GError *error = NULL;
	SNDFILE *sf;
	SF_INFO info;
	struct audio_format audio_format;
	size_t frame_size;
	sf_count_t read_frames, num_frames;
	int buffer[4096];
	enum decoder_command cmd;

	info.format = 0;

	sf = sf_open_virtual(&vio, SFM_READ, &info, is);
	if (sf == NULL) {
		g_warning("sf_open_virtual() failed");
		return;
	}

	/* for now, always read 32 bit samples.  Later, we could lower
	   MPD's CPU usage by reading 16 bit samples with
	   sf_readf_short() on low-quality source files. */
	if (!audio_format_init_checked(&audio_format, info.samplerate,
				       SAMPLE_FORMAT_S32,
				       info.channels, &error)) {
		g_warning("%s", error->message);
		g_error_free(error);
		return;
	}

	decoder_initialized(decoder, &audio_format, info.seekable,
			    frame_to_time(info.frames, &audio_format));

	frame_size = audio_format_frame_size(&audio_format);
	read_frames = sizeof(buffer) / frame_size;

	do {
		num_frames = sf_readf_int(sf, buffer, read_frames);
		if (num_frames <= 0)
			break;

		cmd = decoder_data(decoder, is,
				   buffer, num_frames * frame_size,
				   0);
		if (cmd == DECODE_COMMAND_SEEK) {
			sf_count_t c =
				time_to_frame(decoder_seek_where(decoder),
					      &audio_format);
			c = sf_seek(sf, c, SEEK_SET);
			if (c < 0)
				decoder_seek_error(decoder);
			else
				decoder_command_finished(decoder);
			cmd = DECODE_COMMAND_NONE;
		}
	} while (cmd == DECODE_COMMAND_NONE);

	sf_close(sf);
}
Exemplo n.º 7
0
bool SoundFile::openRead(InputStream& stream)
{
    // If the file is already opened, first close it
    if (m_file)
        sf_close(m_file);

    // Prepare the memory I/O structure
    SF_VIRTUAL_IO io;
    io.get_filelen = &Stream::getLength;
    io.read        = &Stream::read;
    io.seek        = &Stream::seek;
    io.tell        = &Stream::tell;

    // Open the sound file
    SF_INFO fileInfos;
    m_file = sf_open_virtual(&io, SFM_READ, &fileInfos, &stream);
    if (!m_file)
    {
        err() << "Failed to open sound file from stream (" << sf_strerror(m_file) << ")" << std::endl;
        return false;
    }

    // Set the sound parameters
    m_channelCount = fileInfos.channels;
    m_sampleRate   = fileInfos.samplerate;
    m_sampleCount  = static_cast<std::size_t>(fileInfos.frames) * m_channelCount;

    return true;
}
Exemplo n.º 8
0
////////////////////////////////////////////////////////////
/// Check if a given file in memory is supported by this loader
////////////////////////////////////////////////////////////
bool SoundFileDefault::IsFileSupported(const char* Data, std::size_t SizeInBytes)
{
    // Define the I/O custom functions for reading from memory
    SF_VIRTUAL_IO VirtualIO;
    VirtualIO.get_filelen = &SoundFileDefault::MemoryGetLength;
    VirtualIO.read        = &SoundFileDefault::MemoryRead;
    VirtualIO.seek        = &SoundFileDefault::MemorySeek;
    VirtualIO.tell        = &SoundFileDefault::MemoryTell;
    VirtualIO.write       = &SoundFileDefault::MemoryWrite;

    // Initialize the memory data
    MemoryInfos Memory;
    Memory.DataStart = Data;
    Memory.DataPtr   = Data;
    Memory.TotalSize = SizeInBytes;

    // Open the sound file
    SF_INFO FileInfos;
    SNDFILE* File = sf_open_virtual(&VirtualIO, SFM_READ, &FileInfos, &Memory);

    if (File)
    {
        sf_close(File);
        return true;
    }
    else
    {
        return false;
    }
}
bool LibsndfileDecoder::Open(FILE* file) {
	file_=file;
	soundfile=sf_open_virtual(&vio,SFM_READ,&soundinfo,file);
	sf_command(soundfile, SFC_SET_SCALE_FLOAT_INT_READ, NULL, SF_TRUE);
	output_format=Format::F32;
	finished=false;
	return soundfile!=0;
}
Exemplo n.º 10
0
SFReader::SFReader(IO &fp)
{
 fp.seekS(0);

 memset(&sfinfo, 0, sizeof(sfinfo));
 if(!(sf = sf_open_virtual(&IOAPI::sndfile, SFM_READ, &sfinfo, &fp)))
	throw(0);
}
Exemplo n.º 11
0
bool CSndFile::open(QIODevice *io, int mode)
{
    if (io)
    {
        m_snd = sf_open_virtual(&m_vioPtr, mode, &m_info, io);
        return (m_snd != 0);
    }
    return false;
}
Exemplo n.º 12
0
bool CSndFile::open(int mode)
{
    if (m_io)
    {
        m_snd = sf_open_virtual(&m_vioPtr, mode, &m_info, m_io);
        return (m_snd != 0);
    }
    return false;
}
Exemplo n.º 13
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)));
 }
Exemplo n.º 14
0
bool M_loadSound(const char * filename, void * data)
{
	SF_VIRTUAL_IO io;
	io.get_filelen = &M_SFGetFileLen;
	io.seek = &M_SFSeek;
	io.read = &M_SFRead;
	io.write = &M_SFWrite;
	io.tell = &M_SFTell;
	
	// open file
    SF_INFO infos;
	MFile* File = M_fopen(filename, "rb");
    SNDFILE * file = sf_open_virtual(&io, SFM_READ, &infos, File);
	if(! file){
		M_fclose(File);
		printf("ERROR Load Sound : unable to read %s file\n", filename);
		return false;
	}
	
	int nbSamples  = infos.channels * (int)infos.frames;
    int sampleRate = infos.samplerate;
	
	M_SOUND_FORMAT format;
    switch(infos.channels)
    {
		case 1 :
			format = M_SOUND_FORMAT_MONO16;
			break;
		case 2 :
			format = M_SOUND_FORMAT_STEREO16;
			break;
		default :
			printf("ERROR Load Sound : non supported format\n");
			M_fclose(File);
			return false;
    }
	
	MSound * sound = (MSound *)data;
	
	unsigned int size = nbSamples*2;
	sound->create(format, size, (unsigned int)sampleRate);
	
    // 16 bits file reading
	if(sf_read_short(file, (short*)sound->getData(), nbSamples) < nbSamples)
	{
		printf("ERROR Load Sound : unable to read samples\n");
		M_fclose(File);
        return false;
	}

    sf_close(file);
	M_fclose(File);
	return true;
}
Exemplo n.º 15
0
yz::AlBuffer::AlBuffer(yz::physfs* file, const int number) {
    LOG_FUNCTION
    this->number = number;
    SF_INFO fileInfo;
    SF_VIRTUAL_IO io;
    io.get_filelen = &yz::AlBuffer::Stream::getLength;
    io.read = &yz::AlBuffer::Stream::read;
    io.seek = &yz::AlBuffer::Stream::seek;
    io.tell = &yz::AlBuffer::Stream::tell;
    this->soundFile = sf_open_virtual(&io, SFM_READ, &fileInfo, file);
    this->init(fileInfo);
}
Exemplo n.º 16
0
 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)));
 }
Exemplo n.º 17
0
bool SndfileDecoder::Open(FileSpecifier& File)
{
  Close();

  sfinfo.format = 0;
  OpenedFile openedFile;
  if (File.Open(openedFile)) {
    rwops = openedFile.TakeRWops();
    sndfile = sf_open_virtual(&sf_virtual, SFM_READ, &sfinfo, rwops);
  }

  return sndfile;
}
Exemplo n.º 18
0
    sndStream(std::istream *_fstream)
      : alureStream(_fstream), sndFile(NULL), format(AL_NONE)
    {
        memset(&sndInfo, 0, sizeof(sndInfo));

        if(!sndfile_handle) return;

        static SF_VIRTUAL_IO streamIO = {
            get_filelen, seek,
            read, write, tell
        };
        sndFile = sf_open_virtual(&streamIO, SFM_READ, &sndInfo, this);
    }
Exemplo n.º 19
0
static const char *GuessSndFile (char *buf, int len)
{
	SF_VIRTUAL_IO io;
	io.get_filelen = dummy_vio_get_filelen;
	io.seek = dummy_vio_seek;
	io.read = dummy_vio_read;
	io.write = dummy_vio_write;
	io.tell = dummy_vio_tell;

	struct dummy_file file;
	file.buffer = buf;
	file.length = len;
	file.curpos = 0;

	SF_INFO info;
	memset(&info, 0, sizeof(SF_INFO));

	SNDFILE* sndfile = sf_open_virtual (&io, SFM_READ, &info, &file);
	if( sndfile == NULL )
	{
		int err = sf_error(NULL);
		fprintf(stderr, "Error occurred: %s\n", sf_error_number(err));
		switch(err)
		{
		case SF_ERR_UNRECOGNISED_FORMAT:
		case SF_ERR_UNSUPPORTED_ENCODING:
			sf_close(sndfile);
			return NULL;
		case SF_ERR_SYSTEM:
		case SF_ERR_MALFORMED_FILE:
		default:
			sf_close(sndfile);
			return QUE_STRING;
			break;
		}
	}
	else
	{
		sf_close(sndfile);
		SF_FORMAT_INFO format_info;
		format_info.format = info.format;
		int ret = sf_command (NULL /* sndfile */, SFC_GET_FORMAT_INFO, &format_info, sizeof(SF_FORMAT_INFO)) ;
		/* fprintf(stderr, "%s [%s]\n", format_info.name, format_info.extension); */
		return format_info.name;
	}
	
	return NULL;
}
Exemplo n.º 20
0
static PyObject*
_rpitx_broadcast_fm(PyObject* self, PyObject* args) {
	float frequency;

	assert(sizeof(sampleBase) == sizeof(unsigned long));
	if (!PyArg_ParseTuple(args, "Lif", &sampleBase, &sampleLength, &frequency)) {
		struct module_state *st = GETSTATE(self);
		PyErr_SetString(st->error, "Invalid arguments");
		return NULL;
	}

	sampleOffset = 0;

	SF_VIRTUAL_IO virtualIo = {
		.get_filelen = virtualSndfileGetLength,
		.seek = virtualSndfileSeek,
		.read = virtualSndfileRead,
		.write = NULL,
		.tell = virtualSndfileTell
	};
	SF_INFO sfInfo;
	sndFile = sf_open_virtual(&virtualIo, SFM_READ, &sfInfo, sampleBase);
	if (sf_error(sndFile) != SF_ERR_NO_ERROR) {
		char message[100];
		snprintf(
				message,
				COUNT_OF(message),
				"Unable to open sound file: %s",
				sf_strerror(sndFile));
		message[COUNT_OF(message) - 1] = '\0';
		struct module_state *st = GETSTATE(self);
		PyErr_SetString(st->error, message);
		return NULL;
	}
	bitRate = sfInfo.samplerate;

	int skipSignals[] = {
		SIGALRM,
		SIGVTALRM,
		SIGCHLD,  // We fork whenever calling broadcast_fm
		SIGWINCH,  // Window resized
		0
	};
	pitx_run(MODE_RF, bitRate, frequency * 1000.0, 0.0, 0, formatRfWrapper, reset, skipSignals);
	sf_close(sndFile);

	Py_RETURN_NONE;
}
Exemplo n.º 21
0
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;
}
Exemplo n.º 22
0
SoundFile::SoundFile(const QString &fname) {
	siInfo.frames = 0;
	siInfo.channels = 1;
	siInfo.samplerate = 0;
	siInfo.sections = 0;
	siInfo.seekable = 0;

	sfFile = NULL;

	qfFile.setFileName(fname);

	if (qfFile.open(QIODevice::ReadOnly)) {
		static SF_VIRTUAL_IO svi = {&SoundFile::vio_get_filelen, &SoundFile::vio_seek, &SoundFile::vio_read, &SoundFile::vio_write, &SoundFile::vio_tell};

		sfFile = sf_open_virtual(&svi, SFM_READ, &siInfo, this);
	}
}
Exemplo n.º 23
0
static gboolean
xmms_sndfile_init (xmms_xform_t *xform)
{
	xmms_sndfile_data_t *data;

	g_return_val_if_fail (xform, FALSE);

	data = g_new0 (xmms_sndfile_data_t, 1);
	g_return_val_if_fail (data, FALSE);

	xmms_xform_private_data_set (xform, data);

	data->sfvirtual.get_filelen = &xmms_sf_virtual_get_filelen;
	data->sfvirtual.seek = &xmms_sf_virtual_seek;
	data->sfvirtual.read = &xmms_sf_virtual_read;
	data->sfvirtual.write = &xmms_sf_virtual_write;
	data->sfvirtual.tell = &xmms_sf_virtual_tell;

	data->sndfile = sf_open_virtual (&data->sfvirtual, SFM_READ,
	                                 &data->sf_info, xform);
	if (data->sndfile == NULL) {
		char errstr[1024];
		sf_error_str (NULL, errstr, sizeof (errstr));
		xmms_log_error ("libsndfile: sf_open_virtual failed with \"%s\".",
		                errstr);
		g_free (data);
		return FALSE;
	}

	xmms_sndfile_get_media_info (xform);

	xmms_xform_outdata_type_add (xform,
	                             XMMS_STREAM_TYPE_MIMETYPE,
	                             "audio/pcm",
	                             XMMS_STREAM_TYPE_FMT_FORMAT,
	                             XMMS_SAMPLE_FORMAT_S32,
	                             XMMS_STREAM_TYPE_FMT_CHANNELS,
	                             data->sf_info.channels,
	                             XMMS_STREAM_TYPE_FMT_SAMPLERATE,
	                             data->sf_info.samplerate,
	                             XMMS_STREAM_TYPE_END);

	return TRUE;
}
Exemplo n.º 24
0
bool SndFileDecoder::open(FileReader *reader)
{
	if (!IsSndFilePresent()) return false;
	
	SF_VIRTUAL_IO sfio = { file_get_filelen, file_seek, file_read, file_write, file_tell };

	Reader = reader;
	SndInfo.format = 0;
	SndFile = sf_open_virtual(&sfio, SFM_READ, &SndInfo, this);
	if (SndFile)
	{
		if (SndInfo.channels == 1 || SndInfo.channels == 2)
			return true;

		sf_close(SndFile);
		SndFile = 0;
	}
    return false;
}
Exemplo n.º 25
0
gboolean
ags_sndfile_rw_open(AgsPlayable *playable, gchar *name,
		    gboolean create)
{
  AgsSndfile *sndfile;
  sf_count_t multi_frames;
  
  sndfile = AGS_SNDFILE(playable);

  sndfile->info->frames = 0;
  sndfile->info->seekable = 0;
  sndfile->info->sections = 0;

  g_message("export to: %s\n  samplerate: %d\n  channels: %d\n  format: %x\0",
	    name,
	    sndfile->info->samplerate,
	    sndfile->info->channels,
	    sndfile->info->format);

  if(!sf_format_check(sndfile->info)){
    g_warning("invalid format");
  }

  if((AGS_SNDFILE_VIRTUAL & (sndfile->flags)) == 0){
    if(name != NULL){
      sndfile->file = (SNDFILE *) sf_open(name, SFM_RDWR, sndfile->info);
    }
  }else{
    sndfile->file = (SNDFILE *) sf_open_virtual(ags_sndfile_virtual_io, SFM_RDWR, sndfile->info, sndfile);
  }

  multi_frames = sndfile->info->frames * sndfile->info->channels;
  //  sf_command(sndfile->file, SFC_FILE_TRUNCATE, &(multi_frames), sizeof(multi_frames));
  //  sf_command (sndfile, SFC_SET_SCALE_INT_FLOAT_WRITE, NULL, SF_TRUE);
  //  sf_seek(sndfile->file, 0, SEEK_SET);

  //  sndfile->info->frames = multi_frames;

  if(sndfile->file == NULL)
    return(FALSE);
  else
    return(TRUE);
}
Exemplo n.º 26
0
bool SoundFile::openRead(File& stream)
{
	// -- Close any previously open self made stream
	if(m_ownedStream)
	{
		delete m_ownedStream;
		m_ownedStream = NULL;
	}

    // If the file is already opened, first close it
    if (m_file)
        sf_close(m_file);

    // Prepare the memory I/O structure
    SF_VIRTUAL_IO io;
    io.get_filelen = &Stream::getLength;
    io.read        = &Stream::read;
    io.seek        = &Stream::seek;
    io.tell        = &Stream::tell;

    // Initialize the stream data
    m_stream.source = &stream;
    m_stream.size = stream.getSize();

    // Make sure that the stream's reading position is at the beginning
    stream.seek(0);

    // Open the sound file
    SF_INFO fileInfo;
    fileInfo.format = 0;
    m_file = sf_open_virtual(&io, SFM_READ, &fileInfo, &m_stream);
    if (!m_file)
    {
        Log("Failed to open sound file from stream (%s)", sf_strerror(m_file));
        return false;
    }

    // Initialize the internal state from the loaded information
    initialize(fileInfo);
	
    return true;
}
Exemplo n.º 27
0
    bool SoundBuffer::LoadFromFile(const std::string& filePath)
    {
		SF_INFO soundInfo;

#if defined(UTH_SYSTEM_ANDROID)
		AAsset* asset = FileReader::loadSound(filePath);

		SF_VIRTUAL_IO virtualIO;
		virtualIO.get_filelen = &FileReader::getAssetLength;
		virtualIO.seek = &FileReader::seekAsset;
		virtualIO.read = &FileReader::readAsset;
		virtualIO.tell = &FileReader::tellAsset;	

		SNDFILE* file = sf_open_virtual(&virtualIO, SFM_READ, &soundInfo, asset);
#elif defined(UTH_SYSTEM_WINDOWS)
		SNDFILE* file = sf_open(("assets/" + filePath).c_str(), SFM_READ, &soundInfo);
#endif

		if (!file)
		{
			int error = sf_error(file);
			WriteError("Failed to open sound file, error &d", file, error);
			WriteError(sf_error_number(error));
		}

		WriteLog("Frames: %d\n"		, soundInfo.frames);
		WriteLog("Samplerae: %d\n"	, soundInfo.samplerate);
		WriteLog("Channels: %d\n"	, soundInfo.channels);
		WriteLog("Format: %d\n"		, soundInfo.format);


		m_soundInfo.frames			= static_cast<int>(soundInfo.frames * soundInfo.channels);
		m_soundInfo.channels		= soundInfo.channels;
		m_soundInfo.sampleRate		= soundInfo.samplerate;

		m_soundInfo.soundBuffer = new short[m_soundInfo.frames];
		sf_read_short(file, m_soundInfo.soundBuffer, m_soundInfo.frames);

		sf_close(file);

		return true;	
    }
Exemplo n.º 28
0
/*
 * call-seq:
 *   CSound.new(path, mode, info) => snd
 *   CSound.new(io, mode, info) => snd
 *
 * Returns a new <code>CSound</code> object for the audio file at the given path
 * or using the given fixed-length IO-like object with the given mode. Valid modes
 * are <code>"r"</code>, <code>"w"</code>, or <code>"rw"</code>.
 * <code>StringIO</code> is the only valid IO-like object in the standard library,
 * although any object that implements <code>size</code>, <code>seek</code>,
 * <code>read</code>, <code>write</code>, and <code>tell</code> will work.
 */
static VALUE ra_sound_init(VALUE self, VALUE source, VALUE mode, VALUE info) {
    RA_SOUND *snd;
    Data_Get_Struct(self, RA_SOUND, snd);

    // Get mode
    const char *m = StringValueCStr(mode);
    if(strcmp(m,      "rw") == 0) snd->mode = SFM_RDWR;
    else if(strcmp(m, "r") == 0)  snd->mode = SFM_READ;
    else if(strcmp(m, "w") == 0)  snd->mode = SFM_WRITE;
    else rb_raise(rb_eArgError, "invalid access mode %s", m);

    // Set info
    snd->info = info;

    // Open sound file
    SF_INFO *sf_info;
    Data_Get_Struct(info, SF_INFO, sf_info);
    if(TYPE(source) == T_STRING) {
        // Open sound file at the path
        const char *p = StringValueCStr(source);
        snd->snd = sf_open(p, snd->mode, sf_info);
    } else {
        // Check if the source implements the right methods
        if(!rb_respond_to(source, id_size)) rb_raise(eRubyAudioError, "source does not implement size");
        if(!rb_respond_to(source, id_seek)) rb_raise(eRubyAudioError, "source does not implement seek");
        if(!rb_respond_to(source, id_read)) rb_raise(eRubyAudioError, "source does not implement read");
        if(!rb_respond_to(source, id_write)) rb_raise(eRubyAudioError, "source does not implement write");
        if(!rb_respond_to(source, id_tell)) rb_raise(eRubyAudioError, "source does not implement tell");

        // Open sound using the virtual IO API
        snd->vio_source = source;
        SF_VIRTUAL_IO vir_io = {ra_vir_size, ra_vir_seek, ra_vir_read, ra_vir_write, ra_vir_tell};
        snd->snd = sf_open_virtual(&vir_io, snd->mode, sf_info, (void*)source);
    }
    if(snd->snd == NULL) rb_raise(eRubyAudioError, sf_strerror(snd->snd));
    snd->closed = 0;

    return self;
}
Exemplo n.º 29
0
gboolean
ags_sndfile_open(AgsPlayable *playable, gchar *name)
{
  AgsSndfile *sndfile;

  sndfile = AGS_SNDFILE(playable);

  sndfile->info = (SF_INFO *) malloc(sizeof(SF_INFO));
  sndfile->info->format = 0;

  if((AGS_SNDFILE_VIRTUAL & (sndfile->flags)) == 0){
    if(name != NULL){
      sndfile->file = (SNDFILE *) sf_open(name, SFM_READ, sndfile->info);
    }
  }else{
    sndfile->file = (SNDFILE *) sf_open_virtual(ags_sndfile_virtual_io, SFM_READ, sndfile->info, sndfile);
  }

  if(sndfile->file == NULL)
    return(FALSE);
  else
    return(TRUE);
}
Exemplo n.º 30
0
	void Sound::createFromEmbed(File& file) {
		uint32_t size = file.readDword();
		
		SNDFILE* stream = NULL;
		SF_INFO info;
		
		vio_Stream vio = {file.tell(), size, &file};

		//Open stream
		if(!(stream = sf_open_virtual(&sfio, SFM_READ, &info, &vio)))
			goto end;

		//Read audio data
		c_samples = info.frames;
		channels = info.channels;
		sample_rate = info.samplerate;
		samples = new float[c_samples * channels];

		if(sf_readf_float(stream, samples, c_samples) != c_samples) {
			goto error;
		}
		goto end;
	error:
		if(samples) {
			delete [] samples;
			samples = nullptr;
		}
		c_samples = 0;
		channels = 0;

	end:
		if(stream)
			sf_close(stream);
		
		//Make sure file's in the right spot for the next thing
		file.seek(vio.origin + vio.size);
	}