Beispiel #1
0
static void
concat_data_fp (SNDFILE *wfile, SNDFILE *rofile, int channels)
{	static double	data [BUFFER_LEN] ;
	int		frames, readcount ;

	frames = BUFFER_LEN / channels ;
	readcount = frames ;

	sf_seek (wfile, 0, SEEK_END) ;

	while (readcount > 0)
	{	readcount = sf_readf_double (rofile, data, frames) ;
		sf_writef_double (wfile, data, readcount) ;
		} ;

	return ;
} /* concat_data_fp */
Beispiel #2
0
void
test_writef_double_or_die (SNDFILE *file, int pass, double *test, sf_count_t frames, int line_num)
{	sf_count_t count ;

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

	return ;
} /* test_writef_double */
Beispiel #3
0
void
soundfile_write_double(soundfile_t *sf, double *buffer, index_t n_frames) {
	if (sf->t == sft_libsndfile) {
		sf_count_t count; 
		if ((count = sf_writef_double(sf->p, buffer, n_frames)) != n_frames)
			sdie(sf->p, "sf_writef_double returned %d (expected %d): ", count, n_frames);
	} else {
		WavpackContext *wpc = sf->p;
		int32_t sample_buffer[n_frames*sf->channels];
		for (int i = 0; i < 10; i++)
			dp(30, "buffer[%d]=%g\n", i, (double)buffer[i]);
		
		double multiplier = 1L << (sf->bits_per_sample - 1);
		for (int i = 0; i < n_frames*sf->channels; i++)
  	  		sample_buffer[i] = multiplier*buffer[i]; // FIXME may overflow buffer[1] == 1
		if (!WavpackPackSamples(sf->p, sample_buffer, n_frames))
            die("WavpackPackSamples failed: %s\n", WavpackGetErrorMessage(wpc));
	}
}
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;
}
Beispiel #5
0
/*
 * call-seq:
 *   snd.write(buf) => integer
 *
 * Writes the entire contents of the given buffer to the sound and returns the
 * number of frames written.
 */
static VALUE ra_sound_write(VALUE self, VALUE buf) {
    RA_SOUND *snd;
    Data_Get_Struct(self, RA_SOUND, snd);
    if(snd->closed) rb_raise(eRubyAudioError, "closed sound");

    // Get buffer struct
    RA_BUFFER *b;
    Data_Get_Struct(buf, RA_BUFFER, b);

    // Get info struct
    SF_INFO *info;
    Data_Get_Struct(snd->info, SF_INFO, info);

    // Check buffer channels matches actual channels
    if(b->channels != info->channels) {
        rb_raise(eRubyAudioError, "channel count mismatch: %d vs %d", b->channels, info->channels);
    }

    // Write data
    sf_count_t written;
    switch(b->type) {
        case RA_BUFFER_TYPE_SHORT:
            written = sf_writef_short(snd->snd, b->data, b->real_size);
            break;
        case RA_BUFFER_TYPE_INT:
            written = sf_writef_int(snd->snd, b->data, b->real_size);
            break;
        case RA_BUFFER_TYPE_FLOAT:
            written = sf_writef_float(snd->snd, b->data, b->real_size);
            break;
        case RA_BUFFER_TYPE_DOUBLE:
            written = sf_writef_double(snd->snd, b->data, b->real_size);
            break;
    }

    return OFFT2NUM(written);
}
static void
interleave_double (STATE * state)
{	int max_read_len, read_len ;
	int ch, k ;

	do
	{	max_read_len = 0 ;

		for (ch = 0 ; ch < state->channels ; ch ++)
		{	read_len = sf_read_double (state->infile [ch], state->din.d, BUFFER_LEN) ;
			if (read_len < BUFFER_LEN)
				memset (state->din.d + read_len, 0, sizeof (state->din.d [0]) * (BUFFER_LEN - read_len)) ;

			for (k = 0 ; k < BUFFER_LEN ; k++)
				state->dout.d [k * state->channels + ch] = state->din.d [k] ;

			max_read_len = MAX (max_read_len, read_len) ;
		} ;

		sf_writef_double (state->outfile, state->dout.d, max_read_len) ;
	}
	while (max_read_len > 0) ;

} /* interleave_double */
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])

{

	//--------------------------------------------
	// VARIABLES
	//--------------------------------------------

	//--
	// samples and rate
	//--
	
	// NOTE: this includes the samples array pointer, number of channels and samples, and rate
	
	void *X; 
	
	int n, ch, r, e;
	
	//--
	// output file name
	//--
	
	char *f;
	
	int len;
	
	//--
	// format and encoding
	//--
	
	int fmt_code, enc_code;
	
	//--
	// libsndfile structures
	//--
		
	SNDFILE *out_file;

	SF_INFO out_info;
	
	
	mxClassID cls;

	//--------------------------------------------
	// INPUT
	//--------------------------------------------

	// TODO: add some check on the number of channels and samples
	
	//--------------------------------
	// samples and rate
	//--------------------------------

	X = mxGetPr(prhs[0]);
	
	cls = mxGetClassID(prhs[0]);

	n = mxGetN(prhs[0]);
	
	ch = mxGetM(prhs[0]); 

	r = (int) mxGetScalar(prhs[1]);

	//--------------------------------
	// output file
	//--------------------------------

	len = mxGetM(prhs[2]) * mxGetN(prhs[2]) + 1;

	f = mxCalloc(len, sizeof(char));

	mxGetString(prhs[2], f, len);
	
	//--------------------------------
	// format and encoding
	//--------------------------------
	
	// NOTE: although we could extract the format from the file name it it easier in MATLAB
	
	fmt_code = (int) mxGetScalar(prhs[3]);
	
	enc_code = (int) mxGetScalar(prhs[4]);
	
	//--------------------------------------------
	// COMPUTATION
	//--------------------------------------------

	//--------------------------------
	// fill info structure
	//--------------------------------
	
	out_info.frames = n;
	
	out_info.channels = ch;
			
	out_info.samplerate = r;
	
	out_info.sections = 1;
	
	out_info.seekable = 1;
	
	// NOTE: look at 'sndfile.h' for the meanings of these
	
	switch (fmt_code) {
		
		case 0:
			fmt_code = SF_FORMAT_WAV; break;
					
		case 1:
			fmt_code = SF_FORMAT_AIFF; break;
			
		case 2:
			fmt_code = SF_FORMAT_AU; break;
			
		case 3:
			fmt_code = SF_FORMAT_W64; break;
			
		case 4:
			fmt_code = SF_FORMAT_FLAC; break;
			
		default:
			mexErrMsgTxt("Unsupported major file format.");

	}
	
	// NOTE: look at 'sndfile.h' for the meanings of these
	
	switch (enc_code) {
		
		case 0:
			if (fmt_code == SF_FORMAT_AU) {
				mexErrMsgTxt("Unsupported encoding for major file format.");
			}
			enc_code = SF_FORMAT_PCM_U8; break;

		case 1:
			if ((fmt_code == SF_FORMAT_WAV) || (fmt_code == SF_FORMAT_WAV)) {
				mexErrMsgTxt("Unsupported encoding for major file format.");
			}
			enc_code = SF_FORMAT_PCM_S8; break;
			
		case 2:
			enc_code = SF_FORMAT_PCM_16; break;
			
		case 3:
			enc_code = SF_FORMAT_PCM_24; break;
			
		case 4:
			enc_code = SF_FORMAT_PCM_32; break;
			
		case 5:
			enc_code = SF_FORMAT_FLOAT; break;
			
		case 6:
			enc_code = SF_FORMAT_DOUBLE; break;
			
		case 7:
			enc_code = SF_FORMAT_ULAW; break;
			
		case 8:
			enc_code = SF_FORMAT_ALAW; break;
			
		case 9:
			if ((fmt_code == SF_FORMAT_AIFF) || (fmt_code == SF_FORMAT_AU)) {
				mexErrMsgTxt("Unsupported encoding for major file format.");
			}
			enc_code = SF_FORMAT_IMA_ADPCM; break;

		case 10:
			if ((fmt_code == SF_FORMAT_AIFF) || (fmt_code == SF_FORMAT_AU)) {
				mexErrMsgTxt("Unsupported encoding for major file format.");
			}
			enc_code = SF_FORMAT_MS_ADPCM; break;
			
		case 11:
			if (fmt_code == SF_FORMAT_AU) {
				mexErrMsgTxt("Unsupported encoding for major file format.");
			}
			enc_code = SF_FORMAT_GSM610; 
			break;
			
		default:
			mexErrMsgTxt("Unsupported encoding.");
			
	}
	
	out_info.format = fmt_code | enc_code;
	
	if (!sf_format_check(&out_info)) mexErrMsgTxt("failed format check.");
	
	//--------------------------------
	// create output file
	//--------------------------------
	
	//--
	// open output file (create if needed)
	//--
	
	out_file = sf_open (f, SFM_WRITE, &out_info);
	
	if (out_file == NULL) {
		
		// grab log buffer (very informative about errors)
		
		char  buffer [2048] ;
        sf_command (out_file, SFC_GET_LOG_INFO, buffer, sizeof (buffer)) ;		
		mexPrintf("%s\n", buffer);
			
		// there must be an error, display it, and break.
		
		mexErrMsgTxt(sf_error_number(sf_error(out_file))) ;
		
	}
		
	//--
	// write samples to output file
	//--
	
	// NOTE: set normalization to true, this assumes samples are in the -1 to 1 range
	
	switch (cls){
		
		case (mxDOUBLE_CLASS):
	
			sf_command (out_file, SFC_SET_NORM_DOUBLE, NULL, SF_TRUE);
	
			sf_writef_double (out_file, (double *) X, n);
			
			break;
			
		case (mxSINGLE_CLASS):
			
			sf_command (out_file, SFC_SET_NORM_FLOAT, NULL, SF_TRUE);
	
			sf_writef_float (out_file, (float *) X, n);
			
			break;
			
		default:
			
			mexErrMsgTxt("Unsupported class.\n");
			
	}

	
	//--
	// close file
	//--

	sf_close (out_file);

}
Beispiel #8
0
static int cbMix(void *outputBuffer, void *inputBuffer, unsigned int nFrames, double streamTime, RtAudioStreamStatus status, void *data){

	if(status)
		printf("RtAudio callback status %d\n",status);
		
	DAC *dac = (DAC *)data;
	
	int channels = dac->outparams->nChannels; //*((int *)data);
	double windowsize = nFrames / dac->sampleRate;
	if(inputBuffer && outputBuffer){ //duplex , must have the same channels
		memcpy( outputBuffer, inputBuffer, nFrames * channels * sizeof(double) );
	}else{
		memset(outputBuffer,0,nFrames * channels * sizeof(double));
	}
	
	if(outputBuffer){
		if (dac->callback_state!=0){
			lua_State *L = dac->callback_state;
			lua_rawgeti(L, LUA_REGISTRYINDEX, dac->thecallback_ref);
			lua_pushnumber(L, nFrames); /* push 1st argument */
			lua_pushnumber(L, streamTime); /* push 2 argument */
			if (lua_pcall(L, 2, 1, 0) != 0){
				//printf("Error running callback function : %s\n",lua_tostring(L, -1));
				luaL_error(L, "error running callback function: %s",lua_tostring(L, -1));
				lua_pop(L,1);
				return 1;
			}
			if(lua_isboolean(L, -1) && lua_toboolean(L, -1)==0){ //if false delete outputBuffer
				memset(outputBuffer,0,nFrames * channels * sizeof(double));
				lua_pop(L, 1);
			}else{
				if(!lua_istable(L,-1)){
					printf("error running callback function: did not returned table\n");
					//luaL_error(L, "error running function 'thecallback': did not returned table");
					lua_pop(L, 1);
					return 1;
				}
				double *buffer = (double *) outputBuffer;
				//interleaved is more simple
				for (int i=1; i<=nFrames*channels; i++ ) {
					lua_rawgeti(L, -1,i);
					*buffer++ = (double)luaL_checknumber(L, -1);
					lua_pop(L, 1);
				}
				lua_pop(L, 1); /* pop returned value */
			}
		}
#if defined(USE_SNDFILE)
		if(!dac->playSoundFiles((double *)outputBuffer,nFrames,channels,streamTime,windowsize)){
			printf("error on playSoundFiles");
			return 1;
		}
#endif
		if(dac->callback_state_post!=0){
			lua_State *L = dac->callback_state_post;
			lua_rawgeti(L, LUA_REGISTRYINDEX, dac->thecallback_post_ref);
			lua_pushnumber(L, nFrames);
			//table with data
			double *buffer = (double *) outputBuffer;
			lua_createtable(L,nFrames*channels,0);
			for (int i=1; i<=nFrames*channels; i++ ) {
				lua_pushnumber(L,(double)*buffer++);
				lua_rawseti(L, -2,i);
			}
			lua_pushnumber(L, streamTime); /* push 2 argument */
			if (lua_pcall(L, 3, 1, 0) != 0){
				//printf("error running function %s: %s\n",thecallback_name_post.c_str(),lua_tostring(L, -1));
				luaL_error(L, "error running callback post function : %s",lua_tostring(L, -1));
				lua_pop(L,1);
				return 1;
			}
			if(lua_isboolean(L, -1) && lua_toboolean(L, -1)==0){ //if false delete outputBuffer
				memset(outputBuffer,0,nFrames * channels * sizeof(double));
				lua_pop(L, 1);
				return 0; //nothing to record
			}
			if(!lua_istable(L,-1)){
				//printf("error running function %s: did not returned table\n",thecallback_name.c_str());
				//luaL_error(L, "error running function 'thecallback': did not returned table");
				//return 1;
				//nothing, leave untouched
				lua_pop(L, 1);
			}else{
				double *buffer = (double *) outputBuffer;
				//interleaved is more simple
				for (int i=1; i<=nFrames*channels; i++ ) {
					lua_rawgeti(L, -1,i);
					*buffer++ = (double)luaL_checknumber(L, -1);
					lua_pop(L, 1);
				}
				lua_pop(L, 1); /* pop returned value */
			}
		}
		if(dac->recordfile){
			sf_writef_double(dac->recordfile, (double *)outputBuffer, nFrames);
		}
	}
    return 0;
}
Beispiel #9
0
 int Soundfile::writeFrame(double *inputFrame)
 {
   return sf_writef_double(sndfile, inputFrame, 1);
 }
Beispiel #10
0
int main(int argc, char *argv[]){
	if(argc != 3){
		fprintf(stderr, "expected ./reverse input.wav output.wav\n");
		exit(1);
	}

	SF_INFO si;
	SNDFILE *sf1;
	if((sf1 = sf_open(argv[1], SFM_READ, &si)) == NULL){
		fprintf(stderr, "cant read %s file\n",argv[1]);
		exit(1);
	}
	printf("channels : %d ch\n", si.channels);
	if(si.format == (SF_FORMAT_WAV |SF_FORMAT_PCM_S8)){
		printf("format   : wav PCM 8bit\n");
	}else if(si.format == (SF_FORMAT_WAV |SF_FORMAT_PCM_16)){
		printf("format   : wav PCM 16bit\n");
	}else if(si.format == (SF_FORMAT_WAV |SF_FORMAT_PCM_24)){
		printf("format   : wav PCM 24bit\n");
	}else if(si.format == (SF_FORMAT_WAV |SF_FORMAT_PCM_32)){
		printf("format   : wav PCM 32bit\n");
	}
	printf("frames : %d\n", (int)si.frames);

	double *buffer = (double *)malloc(si.frames * si.channels * sizeof(double));
	if(buffer == NULL){
		fprintf(stderr, "cant allocate memory for buffer\n");
		exit(1);
	}
	int numframes = sf_readf_double(sf1, buffer, si.frames);
	if(numframes != si.frames){
		fprintf(stderr, "cant read enough frames\n");
	}

	int f;
	double temp1, temp2;
	for(f = 0; f < numframes / 2; f++){
		temp1 = buffer[f * 2];
		temp2 = buffer[f * 2 + 1];
		buffer[f * 2] = buffer[(numframes - f) * 2];
		buffer[f * 2 + 1] = buffer[(numframes - f) * 2 + 1];
		buffer[(numframes - f) * 2] = temp1;
		buffer[(numframes - f) * 2 + 1] = temp2;
	}

	SNDFILE *sf2;
	if((sf2 = sf_open(argv[2], SFM_WRITE, &si)) == NULL){
		fprintf(stderr, "cant read %s file\n",argv[2]);
		exit(1);
	}

	int wirrtenframes = sf_writef_double(sf2, buffer, numframes);
	if(wirrtenframes != numframes){
		fprintf(stderr, "cant write enough frames\n");
		exit(1);
	}

	sf_close(sf1);
	sf_close(sf2);
	free(buffer);
	return 0;
}
Beispiel #11
0
int64_t _ambix_writef_float64   (ambix_t*ambix, const float64_t*data, int64_t frames) {
  return (int64_t)sf_writef_double(PRIVATE(ambix)->sf_file, (double*)data, frames) ;
}
Beispiel #12
0
/* Output to a file is really simple. */
int output_to_file(void *out_file, double *samples, size_t count) {
  return sf_writef_double((SNDFILE *)out_file,samples,count);
}