Exemplo n.º 1
0
void AudioFileSndfile::write(const void* buffer, unsigned frames)
{
    if (!_handle || (_mode != ModeWrite && _mode != ModeReadWrite)) {
        RUNTIME_ERROR("Attempt to write file not opened for writing.");
    }

    flushChunks();
    sf_count_t count;

    switch (_info.sampleType) {
    case Sound::Type::Float32:
    case Sound::Type::Int24E:
        count = sf_writef_float(_handle.get(), static_cast<const Sound::Float32*>(buffer), frames);
        break;
    case Sound::Type::Float64:
        count = sf_writef_double(_handle.get(), static_cast<const Sound::Float64*>(buffer), frames);
        break;
    case Sound::Type::Int8:
        count = sf_write_raw(_handle.get(), buffer, frames * _info.channels);
        break;
    case Sound::Type::Int16:
        count = sf_writef_short(_handle.get(), static_cast<const Sound::Int16*>(buffer), frames);
        break;
    case Sound::Type::Int32:
        count = sf_writef_int(_handle.get(), static_cast<const Sound::Int32*>(buffer), frames);
        break;
    case Sound::Type::Int64:
    case Sound::Type::Precise:
    case Sound::Type::Count:
        RUNTIME_ERROR("Writing for this sample format is not supported");
    }
}
Exemplo n.º 2
0
bool WavEncoder::write(const QString &filename,
                       const uint8_t *rawBuffer,
                       const unsigned int bufferSize)
{
    // Set file settings, 16bit Stereo PCM
    SF_INFO info;
    info.format = SF_FORMAT_WAV | SF_FORMAT_PCM_16;
    info.channels = 2;
    info.samplerate = 44100;

    // Open sound file for writing
    SNDFILE *sndFile = sf_open(qPrintable(filename), SFM_WRITE, &info);
    if (sndFile == NULL) {
        qCritical() << "WavEncoder: error opening sound file: "
                    << sf_strerror(sndFile);
        return false;
    }

    // Write
    unsigned int writtenBytes = sf_write_raw(sndFile, rawBuffer, bufferSize);

    // Check correct number of frames saved
    if (writtenBytes != bufferSize) {
        qCritical() << "WavEncoder: did not write enough frames for source";
        sf_close(sndFile);
        return false;
    }

    // Tidy up
    sf_write_sync(sndFile);
    sf_close(sndFile);

    return true;
}
Exemplo n.º 3
0
static int decode(struct aufilt_dec_st *st, void *sampv, size_t *sampc)
{
	struct sndfile_dec *sf = (struct sndfile_dec *)st;
	size_t num_bytes;

	if (!st || !sampv || !sampc)
		return EINVAL;

	num_bytes = *sampc * aufmt_sample_size(sf->fmt);

	sf_write_raw(sf->dec, sampv, num_bytes);

	return 0;
}
Exemplo n.º 4
0
void WavRecord::RcvBuffer(TimedOctetSeq data)
{
  RTC_DEBUG(("RcvBuffer start"));
  if (is_active == true) {
    m_mutex.lock();
    RTC_DEBUG(("RcvBuffer:mutex lock"));
    sf_count_t frames = data.data.length();
    sf_write_raw(sfw, &data.data[0], frames);
    m_mutex.unlock();
    RTC_DEBUG(("RcvBuffer:mutex unlock"));
  }
  RTC_DEBUG(("RcvBuffer finish"));
  return;
}
Exemplo n.º 5
0
int
writeWAVfile( unsigned char *buf, int len )
{
   if ( sFxx == NULL ) return 0;   //file not open

   int rc = sf_write_raw( sFxx, buf, len );

   if ( rc != len ) {
      sf_error_str( NULL, sErr,sizeof(sErr) );
      printf("saveWAVfile() ERROR rc=%d!=%d: %s:",
         rc, len, sErr );
      return -1;
   }
   return 0;
}
Exemplo n.º 6
0
void
test_write_raw_or_die (SNDFILE *file, int pass, const void *test, sf_count_t items, int line_num)
{	sf_count_t count ;

	if ((count = sf_write_raw (file, test, items)) != items)
	{	printf ("\n\nLine %d", line_num) ;
		if (pass > 0)
			printf (" (pass %d)", pass) ;
		printf (" : sf_write_raw failed with short write (%ld => %ld).\n",
						SF_COUNT_TO_LONG (items), SF_COUNT_TO_LONG (count)) ;
		fflush (stdout) ;
		puts (sf_strerror (file)) ;
		exit (1) ;
		} ;

	return ;
} /* test_write_raw_or_die */
Exemplo n.º 7
0
static switch_status_t sndfile_file_write(switch_file_handle_t *handle, void *data, size_t *len)
{
	size_t inlen = *len;
	sndfile_context *context = handle->private_info;

	if (switch_test_flag(handle, SWITCH_FILE_DATA_RAW)) {
		*len = (size_t) sf_write_raw(context->handle, data, inlen);
	} else if (switch_test_flag(handle, SWITCH_FILE_DATA_INT)) {
		*len = (size_t) sf_writef_int(context->handle, (int *) data, inlen);
	} else if (switch_test_flag(handle, SWITCH_FILE_DATA_SHORT)) {
		*len = (size_t) sf_writef_short(context->handle, (short *) data, inlen);
	} else if (switch_test_flag(handle, SWITCH_FILE_DATA_FLOAT)) {
		*len = (size_t) sf_writef_float(context->handle, (float *) data, inlen);
	} else if (switch_test_flag(handle, SWITCH_FILE_DATA_DOUBLE)) {
		*len = (size_t) sf_writef_double(context->handle, (double *) data, inlen);
	} else {
		*len = (size_t) sf_writef_int(context->handle, (int *) data, inlen);
	}

	handle->sample_count += *len;

	return sf_error(context->handle) == SF_ERR_NO_ERROR ? SWITCH_STATUS_SUCCESS : SWITCH_STATUS_FALSE;
}
Exemplo n.º 8
0
/* This is called whenever new data may is available */
static void stream_read_callback(pa_stream *s, size_t length, void *userdata) {

    pa_assert(s);
    pa_assert(length > 0);

    if (raw) {
        pa_assert(!sndfile);

        if (stdio_event)
            mainloop_api->io_enable(stdio_event, PA_IO_EVENT_OUTPUT);

        while (pa_stream_readable_size(s) > 0) {
            const void *data;

            if (pa_stream_peek(s, &data, &length) < 0) {
                pa_log(_("pa_stream_peek() failed: %s"), pa_strerror(pa_context_errno(context)));
                quit(1);
                return;
            }

            pa_assert(data);
            pa_assert(length > 0);

            if (buffer) {
                buffer = pa_xrealloc(buffer, buffer_length + length);
                memcpy((uint8_t*) buffer + buffer_length, data, length);
                buffer_length += length;
            } else {
                buffer = pa_xmalloc(length);
                memcpy(buffer, data, length);
                buffer_length = length;
                buffer_index = 0;
            }

            pa_stream_drop(s);
        }

    } else {
        pa_assert(sndfile);

        while (pa_stream_readable_size(s) > 0) {
            sf_count_t bytes;
            const void *data;

            if (pa_stream_peek(s, &data, &length) < 0) {
                pa_log(_("pa_stream_peek() failed: %s"), pa_strerror(pa_context_errno(context)));
                quit(1);
                return;
            }

            pa_assert(data);
            pa_assert(length > 0);

            if (writef_function) {
                size_t k = pa_frame_size(&sample_spec);

                if ((bytes = writef_function(sndfile, data, (sf_count_t) (length/k))) > 0)
                    bytes *= (sf_count_t) k;

            } else
                bytes = sf_write_raw(sndfile, data, (sf_count_t) length);

            if (bytes < (sf_count_t) length)
                quit(1);

            pa_stream_drop(s);
        }
    }
}
Exemplo n.º 9
0
/* This is called whenever new data may is available */
static void stream_read_callback(pa_stream *s, size_t length, void *userdata) {

    pa_assert(s);
    pa_assert(length > 0);

    if (raw) {
        pa_assert(!sndfile);

        if (stdio_event)
            mainloop_api->io_enable(stdio_event, PA_IO_EVENT_OUTPUT);

        while (pa_stream_readable_size(s) > 0) {
            const void *data;

            if (pa_stream_peek(s, &data, &length) < 0) {
                pa_log(_("pa_stream_peek() failed: %s"), pa_strerror(pa_context_errno(context)));
                quit(1);
                return;
            }

            pa_assert(length > 0);

            /* If there is a hole in the stream, we generate silence, except
             * if it's a passthrough stream in which case we skip the hole. */
            if (data || !(flags & PA_STREAM_PASSTHROUGH)) {
                buffer = pa_xrealloc(buffer, buffer_length + length);
                if (data)
                    memcpy((uint8_t *) buffer + buffer_length, data, length);
                else
                    pa_silence_memory((uint8_t *) buffer + buffer_length, length, &sample_spec);

                buffer_length += length;
            }

            pa_stream_drop(s);
        }

    } else {
        pa_assert(sndfile);

        while (pa_stream_readable_size(s) > 0) {
            sf_count_t bytes;
            const void *data;

            if (pa_stream_peek(s, &data, &length) < 0) {
                pa_log(_("pa_stream_peek() failed: %s"), pa_strerror(pa_context_errno(context)));
                quit(1);
                return;
            }

            pa_assert(length > 0);

            if (!data && (flags & PA_STREAM_PASSTHROUGH)) {
                pa_stream_drop(s);
                continue;
            }

            if (!data && length > silence_buffer_length) {
                silence_buffer = pa_xrealloc(silence_buffer, length);
                pa_silence_memory((uint8_t *) silence_buffer + silence_buffer_length, length - silence_buffer_length, &sample_spec);
                silence_buffer_length = length;
            }

            if (writef_function) {
                size_t k = pa_frame_size(&sample_spec);

                if ((bytes = writef_function(sndfile, data ? data : silence_buffer, (sf_count_t) (length/k))) > 0)
                    bytes *= (sf_count_t) k;

            } else
                bytes = sf_write_raw(sndfile, data ? data : silence_buffer, (sf_count_t) length);

            if (bytes < (sf_count_t) length)
                quit(1);

            pa_stream_drop(s);
        }
    }
}
Exemplo n.º 10
0
int cf_sndfile_write(CODECFILTER_USERDATA_T   inst, char * buf, int len) {
 struct codecfilter_sndfile_inst * obj = (struct codecfilter_sndfile_inst *) inst;
 struct roar_stream * s = ROAR_STREAM(obj->stream);
 int ret;

 ROAR_WARN("cf_sndfile_write(*): obj->opened=%i", obj->opened);

 if ( !obj->opened ) {
  if ( s->fh == -1 ) {
   errno = EAGAIN;
   return -1;
  }

  switch (s->info.codec) {
   case ROAR_CODEC_PCM_S_LE:
   case ROAR_CODEC_PCM_S_BE:
     switch (s->info.bits) {
      case  8:
        obj->info.format = SF_FORMAT_PCM_S8;
       break;
      case 16:
        obj->info.format = SF_FORMAT_PCM_16;
       break;
      case 24:
        obj->info.format = SF_FORMAT_PCM_24;
       break;
      case 32:
        obj->info.format = SF_FORMAT_PCM_32;
       break;
     }
     //obj->info.format |= s->info.codec == ROAR_CODEC_PCM_S_LE ? SF_ENDIAN_LITTLE : SF_ENDIAN_BIG;
     obj->info.format |= SF_ENDIAN_FILE;
     obj->info.format |= SF_FORMAT_WAV;
    break;
   default:
     ROAR_ERR("cf_sndfile_write(*): codec(%s) not supported!", roar_codec2str(s->info.bits));
     return -1;
    break;
  }

  obj->info.samplerate = s->info.rate;
  obj->info.channels   = s->info.channels;
  obj->info.sections   = 1;
  obj->info.frames     = 182592; // 2147483647;
  obj->info.seekable   = 1;
  obj->info.format     = 0x00010002;

  if ( (obj->state = sf_open_fd(s->fh, SFM_WRITE, &(obj->info), 0)) == NULL ) {
   ROAR_ERR("cf_sndfile_write(*): can not sf_open_fd(*)!");
   ROAR_ERR("cf_sndfile_write(*): s->fh=%i", s->fh);
   ROAR_ERR("cf_sndfile_write(*): obj->info={.format=0x%.8x, .samplerate=%i, .channels=%i}", obj->info.format, obj->info.samplerate, obj->info.channels);
   return -1;
  }

  obj->opened = 1;
//  errno = EAGAIN;
 }

 ROAR_WARN("cf_sndfile_write(*): obj->opened=%i", obj->opened);
 ret = sf_write_raw(obj->state, (void*) buf, len);
 ROAR_WARN("cf_sndfile_write(inst=%p, buf=%p, len=%i) = %i", inst, buf, len, ret);
 return ret;
}