Пример #1
0
int
sndfile_open(struct audio_file *fd, const char *ext)
{
	SNDFILE *hnd;
	SF_INFO info;
	int fno;

	if (fd->stream) {
		/* Not yet */
		return (-1);
	}

	/* Rewind the file to the beginning */
	fno = fileno(fd->fp);
	lseek(fno, 0, SEEK_SET);

	if ((hnd = sf_open_fd(fno, SFM_READ, &info, 0)) == NULL)
		return (-1);
	fd->drv_data = (void *)hnd;

	fd->srate = info.samplerate;
	fd->channels = info.channels;
	fd->time_len = info.frames / fd->srate;

	/* Metadata - libsndfile only has artist + title */
	fd->artist = g_strdup(sf_get_string(hnd, SF_STR_ARTIST));
	fd->title = g_strdup(sf_get_string(hnd, SF_STR_TITLE));
	fd->album = g_strdup(sf_get_string(hnd, SF_STR_ALBUM));

	return (0);
}
Пример #2
0
static void
string_short_rdwr_test (const char *filename, int typemajor)
{	SNDFILE *file ;
	SF_INFO sfinfo ;
	sf_count_t frames = BUFFER_LEN ;
	const char * str ;

	print_test_name (__func__, filename) ;

	memset (&sfinfo, 0, sizeof (sfinfo)) ;
	sfinfo.format		= typemajor | SF_FORMAT_PCM_16 ;
	sfinfo.samplerate	= 44100 ;
	sfinfo.channels		= 1 ;
	sfinfo.frames		= 0 ;

	file = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, SF_FALSE, __LINE__) ;

	/* Write data to file. */
	test_write_short_or_die (file, 0, data_out, BUFFER_LEN, __LINE__) ;

	sf_set_string (file, SF_STR_TITLE, long_title) ;
	sf_set_string (file, SF_STR_ARTIST, long_artist) ;
	sf_close (file) ;

	/* Open the file RDWR. */
	file = test_open_file_or_die (filename, SFM_RDWR, &sfinfo, SF_FALSE, __LINE__) ;
	exit_if_true (frames != sfinfo.frames, "\n\nLine %d : Frame count %" PRId64 " should be %" PRId64 ".\n", __LINE__, sfinfo.frames, frames) ;
	str = sf_get_string (file, SF_STR_TITLE) ;
	exit_if_true (str == NULL, "\n\nLine %d : SF_STR_TITLE string is NULL.\n", __LINE__) ;
	exit_if_true (strcmp (str, long_title) != 0, "\n\nLine %d : SF_STR_TITLE doesn't match what was written.\n", __LINE__) ;
	str = sf_get_string (file, SF_STR_ARTIST) ;
	exit_if_true (str == NULL, "\n\nLine %d : SF_STR_TITLE string is NULL.\n", __LINE__) ;
	exit_if_true (strcmp (str, long_artist) != 0, "\n\nLine %d : SF_STR_ARTIST doesn't match what was written.\n", __LINE__) ;

	/* Change title and artist. */
	sf_set_string (file, SF_STR_TITLE, title) ;
	sf_set_string (file, SF_STR_ARTIST, artist) ;

	sf_close (file) ;

	file = test_open_file_or_die (filename, SFM_READ, &sfinfo, SF_FALSE, __LINE__) ;

	check_log_buffer_or_die (file, __LINE__) ;

	str = sf_get_string (file, SF_STR_TITLE) ;
	exit_if_true (str == NULL, "\n\nLine %d : SF_STR_TITLE string is NULL.\n", __LINE__) ;
	exit_if_true (strcmp (str, title) != 0, "\n\nLine %d : SF_STR_TITLE doesn't match what was written.\n", __LINE__) ;

	str = sf_get_string (file, SF_STR_ARTIST) ;
	exit_if_true (str == NULL, "\n\nLine %d : SF_STR_ARTIST string is NULL.\n", __LINE__) ;
	exit_if_true (strcmp (str, artist) != 0, "\n\nLine %d : SF_STR_ARTIST doesn't match what was written.\n", __LINE__) ;

	sf_close (file) ;
	unlink (filename) ;

	puts ("ok") ;
} /* string_short_rdwr_test */
Пример #3
0
static void
string_rdwr_grow_test (const char *filename, int typemajor)
{	SNDFILE *file ;
	SF_INFO sfinfo ;
	sf_count_t frames ;
	const char * str ;

	print_test_name (__func__, filename) ;

	/* Create a file that contains some strings. Then open the file in RDWR mode and
		 grow the file by writing more audio data to it. Check that the audio data has
		 been added to the file, and that the strings are still there. */

	/* Create a short file that contains a string. */
	memset (&sfinfo, 0, sizeof (sfinfo)) ;
	sfinfo.samplerate	= 44100 ;
	sfinfo.channels		= 2 ;
	sfinfo.frames		= 0 ;
	sfinfo.format		= typemajor | SF_FORMAT_PCM_16 ;

	file = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, SF_TRUE, __LINE__) ;
	/* Write data to file. */
	test_write_short_or_die (file, 0, data_out, BUFFER_LEN, __LINE__) ;

	/* Write some strings at end of file. */
	sf_set_string (file, SF_STR_TITLE , title) ;
	sf_set_string (file, SF_STR_COMMENT, comment) ;
	sf_close (file) ;


	/* Now open file again in SFM_RDWR mode and write more audio data to it. */
	file = test_open_file_or_die (filename, SFM_RDWR, &sfinfo, SF_TRUE, __LINE__) ;
	/* Write more data to file.  */
	test_write_short_or_die (file, 0, data_out, BUFFER_LEN, __LINE__) ;
	sf_close (file) ;


	/* Now open file again. It should now contain two BUFFER_LEN's worth of frames and the strings. */
	frames = 2 * BUFFER_LEN / sfinfo.channels ;
	file = test_open_file_or_die (filename, SFM_READ, &sfinfo, SF_TRUE, __LINE__) ;
	exit_if_true (frames != sfinfo.frames, "\n\nLine %d : Frame count %" PRId64 " should be %" PRId64 ".\n", __LINE__, sfinfo.frames, frames) ;

	/* Check the strings */
	str = sf_get_string (file, SF_STR_TITLE) ;
	exit_if_true (str == NULL, "\n\nLine %d : SF_STR_TITLE string is NULL.\n", __LINE__) ;
	exit_if_true (strcmp (str, title) != 0, "\n\nLine %d : SF_STR_TITLE doesn't match what was written.\n", __LINE__) ;

	str = sf_get_string (file, SF_STR_COMMENT) ;
	exit_if_true (str == NULL, "\n\nLine %d : SF_STR_COMMENT string is NULL.\n", __LINE__) ;
	exit_if_true (strcmp (str, comment) != 0, "\n\nLine %d : SF_STR_COMMENT doesn't match what was written.\n", __LINE__) ;

	sf_close (file) ;
	unlink (filename) ;

	puts ("ok") ;
} /* string_rdwr_grow_test */
Пример #4
0
static void
string_rdwr_test (const char *filename, int typemajor)
{	SNDFILE *file ;
	SF_INFO sfinfo ;
	sf_count_t frames ;
	const char * str ;

	print_test_name (__func__, filename) ;
	create_short_sndfile (filename, typemajor | SF_FORMAT_PCM_16, 2) ;

	memset (&sfinfo, 0, sizeof (sfinfo)) ;
	file = test_open_file_or_die (filename, SFM_RDWR, &sfinfo, SF_FALSE, __LINE__) ;
	frames = sfinfo.frames ;
	sf_set_string (file, SF_STR_TITLE, title) ;
	sf_close (file) ;

	file = test_open_file_or_die (filename, SFM_READ, &sfinfo, SF_FALSE, __LINE__) ;
	exit_if_true (frames != sfinfo.frames, "\n\nLine %d : Frame count %" PRId64 " should be %" PRId64 ".\n", __LINE__, sfinfo.frames, frames) ;
	str = sf_get_string (file, SF_STR_TITLE) ;
	exit_if_true (str == NULL, "\n\nLine %d : SF_STR_TITLE string is NULL.\n", __LINE__) ;
	exit_if_true (strcmp (str, title) != 0, "\n\nLine %d : SF_STR_TITLE doesn't match what was written.\n", __LINE__) ;
	sf_close (file) ;

	file = test_open_file_or_die (filename, SFM_RDWR, &sfinfo, SF_FALSE, __LINE__) ;
	frames = sfinfo.frames ;
	sf_set_string (file, SF_STR_TITLE, title) ;
	sf_close (file) ;

	file = test_open_file_or_die (filename, SFM_RDWR, &sfinfo, SF_FALSE, __LINE__) ;
	str = sf_get_string (file, SF_STR_TITLE) ;
	exit_if_true (str == NULL, "\n\nLine %d : SF_STR_TITLE string is NULL.\n", __LINE__) ;
	sf_set_string (file, SF_STR_ARTIST, artist) ;
	sf_close (file) ;

	file = test_open_file_or_die (filename, SFM_READ, &sfinfo, SF_FALSE, __LINE__) ;

	str = sf_get_string (file, SF_STR_ARTIST) ;
	exit_if_true (str == NULL, "\n\nLine %d : SF_STR_ARTIST string is NULL.\n", __LINE__) ;
	exit_if_true (strcmp (str, artist) != 0, "\n\nLine %d : SF_STR_ARTIST doesn't match what was written.\n", __LINE__) ;

	str = sf_get_string (file, SF_STR_TITLE) ;
	exit_if_true (str == NULL, "\n\nLine %d : SF_STR_TITLE string is NULL.\n", __LINE__) ;
	exit_if_true (strcmp (str, title) != 0, "\n\nLine %d : SF_STR_TITLE doesn't match what was written.\n", __LINE__) ;

	exit_if_true (frames != sfinfo.frames, "\n\nLine %d : Frame count %" PRId64 " should be %" PRId64 ".\n", __LINE__, sfinfo.frames, frames) ;

	sf_close (file) ;
	unlink (filename) ;

	puts ("ok") ;
} /* string_rdwr_test */
Пример #5
0
static void
copy_metadata (SNDFILE *outfile, SNDFILE *infile, int channels)
{	SF_INSTRUMENT inst ;
	SF_BROADCAST_INFO_2K binfo ;
	const char *str ;
	int k, chanmap [256] ;

	for (k = SF_STR_FIRST ; k <= SF_STR_LAST ; k++)
	{	str = sf_get_string (infile, k) ;
		if (str != NULL)
			sf_set_string (outfile, k, str) ;
		} ;

	memset (&inst, 0, sizeof (inst)) ;
	memset (&binfo, 0, sizeof (binfo)) ;

	if (channels < ARRAY_LEN (chanmap))
	{	size_t size = channels * sizeof (chanmap [0]) ;

		if (sf_command (infile, SFC_GET_CHANNEL_MAP_INFO, chanmap, size) == SF_TRUE)
			sf_command (outfile, SFC_SET_CHANNEL_MAP_INFO, chanmap, size) ;
		} ;

	if (sf_command (infile, SFC_GET_INSTRUMENT, &inst, sizeof (inst)) == SF_TRUE)
		sf_command (outfile, SFC_SET_INSTRUMENT, &inst, sizeof (inst)) ;

	if (sf_command (infile, SFC_GET_BROADCAST_INFO, &binfo, sizeof (binfo)) == SF_TRUE)
		sf_command (outfile, SFC_SET_BROADCAST_INFO, &binfo, sizeof (binfo)) ;

} /* copy_metadata */
Пример #6
0
static void
software_string_test (const char *filename)
{	size_t k ;

	print_test_name (__func__, filename) ;

	for (k = 0 ; k < 50 ; k++)
	{	const char *result ;
		char sfname [64] = "" ;
		SNDFILE *file ;
		SF_INFO info ;

		sf_info_setup (&info, SF_FORMAT_WAV | SF_FORMAT_PCM_16, 44100, 1) ;
		file = test_open_file_or_die (filename, SFM_WRITE, &info, SF_TRUE, __LINE__) ;

		snprintf (sfname, MIN (k, sizeof (sfname)), "%s", "abcdefghijklmnopqrestvwxyz0123456789abcdefghijklmnopqrestvwxyz") ;

		exit_if_true (sf_set_string (file, SF_STR_SOFTWARE, sfname),
			"\n\nLine %d : sf_set_string (f, SF_STR_SOFTWARE, '%s') failed : %s\n", __LINE__, sfname, sf_strerror (file)) ;

		sf_close (file) ;

		file = test_open_file_or_die (filename, SFM_READ, &info, SF_TRUE, __LINE__) ;
		result = sf_get_string (file, SF_STR_SOFTWARE) ;

		exit_if_true (result == NULL, "\n\nLine %d : sf_get_string (file, SF_STR_SOFTWARE) returned NULL.\n\n", __LINE__) ;

		exit_if_true (strstr (result, sfname) != result,
			"\n\nLine %d : Can't fine string '%s' in '%s'\n\n", __LINE__, sfname, result) ;
		sf_close (file) ;
		} ;

	unlink (filename) ;
	puts ("ok") ;
} /* software_string_test */
Пример #7
0
static bool
sndfile_scan_file(const char *path_fs,
		  const struct tag_handler *handler, void *handler_ctx)
{
	SNDFILE *sf;
	SF_INFO info;
	const char *p;

	info.format = 0;

	sf = sf_open(path_fs, SFM_READ, &info);
	if (sf == NULL)
		return false;

	if (!audio_valid_sample_rate(info.samplerate)) {
		sf_close(sf);
		g_warning("Invalid sample rate in %s\n", path_fs);
		return false;
	}

	tag_handler_invoke_duration(handler, handler_ctx,
				    info.frames / info.samplerate);

	p = sf_get_string(sf, SF_STR_TITLE);
	if (p != NULL)
		tag_handler_invoke_tag(handler, handler_ctx,
				       TAG_TITLE, p);

	p = sf_get_string(sf, SF_STR_ARTIST);
	if (p != NULL)
		tag_handler_invoke_tag(handler, handler_ctx,
				       TAG_ARTIST, p);

	p = sf_get_string(sf, SF_STR_DATE);
	if (p != NULL)
		tag_handler_invoke_tag(handler, handler_ctx,
				       TAG_DATE, p);

	sf_close(sf);

	return true;
}
Пример #8
0
static void
copy_metadata (SNDFILE *outfile, SNDFILE *infile)
{	const char *str ;
	int k, err = 0 ;

	for (k = SF_STR_FIRST ; k <= SF_STR_LAST ; k++)
	{	str = sf_get_string (infile, k) ;
		if (str != NULL)
			err = sf_set_string (outfile, k, str) ;
		} ;

} /* copy_metadata */
Пример #9
0
static switch_status_t sndfile_file_get_string(switch_file_handle_t *handle, switch_audio_col_t col, const char **string)
{
	sndfile_context *context = handle->private_info;
	const char *s;

	if ((s = sf_get_string(context->handle, (int) col))) {
		*string = s;
		return SWITCH_STATUS_SUCCESS;
	}

	return SWITCH_STATUS_FALSE;
}
static struct tag *
sndfile_tag_dup(const char *path_fs)
{
	SNDFILE *sf;
	SF_INFO info;
	struct tag *tag;
	const char *p;

	info.format = 0;

	sf = sf_open(path_fs, SFM_READ, &info);
	if (sf == NULL)
		return NULL;

	if (!audio_valid_sample_rate(info.samplerate)) {
		sf_close(sf);
		g_warning("Invalid sample rate in %s\n", path_fs);
		return NULL;
	}

	tag = tag_new();
	tag->time = info.frames / info.samplerate;

	p = sf_get_string(sf, SF_STR_TITLE);
	if (p != NULL)
		tag_add_item(tag, TAG_TITLE, p);

	p = sf_get_string(sf, SF_STR_ARTIST);
	if (p != NULL)
		tag_add_item(tag, TAG_ARTIST, p);

	p = sf_get_string(sf, SF_STR_DATE);
	if (p != NULL)
		tag_add_item(tag, TAG_DATE, p);

	sf_close(sf);

	return tag;
}
Пример #11
0
const char* AudioFileSndfile::string(IAudioFile::StringEntry entry)
{
    const char* result = sf_get_string(_handle.get(), entry);

    if (!result) {
        int errorNumber = sf_error(_handle.get());

        if (errorNumber != SF_ERR_NO_ERROR) {
            SNDFILE_ERROR(errorNumber, "Can't read string entry");
        }
    }

    return result;
}
Пример #12
0
static void
copy_metadata (SNDFILE *outfile, SNDFILE *infile)
{	SF_INSTRUMENT inst ;
	const char *str ;
	int k, err = 0 ;

	for (k = SF_STR_FIRST ; k <= SF_STR_LAST ; k++)
	{	str = sf_get_string (infile, k) ;
		if (str != NULL)
			err = sf_set_string (outfile, k, str) ;
		} ;

	memset (&inst, 0, sizeof (inst)) ;
	if (sf_command (infile, SFC_GET_INSTRUMENT, &inst, sizeof (inst)) == SF_TRUE)
		sf_command (outfile, SFC_SET_INSTRUMENT, &inst, sizeof (inst)) ;

} /* copy_metadata */
Пример #13
0
static void
string_header_update (const char *filename, int typemajor)
{	SNDFILE *file , *file1 ;
	SF_INFO sfinfo , sfinfo1 ;
	sf_count_t frames ;
	const char * str ;
	const int GROW_BUFFER_AMOUNT = 4 ; /* this should be less than half the size of the string header */

	print_test_name (__func__, filename) ;

	/* Create a short file. */
	memset (&sfinfo, 0, sizeof (sfinfo)) ;
	sfinfo.samplerate	= 44100 ;
	sfinfo.channels		= 2 ;
	sfinfo.frames		= 0 ;
	sfinfo.format		= typemajor | SF_FORMAT_PCM_16 ;

	file = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, SF_TRUE, __LINE__) ;
	test_write_short_or_die (file, 0, data_out, BUFFER_LEN, __LINE__) ;
	sf_set_string (file, SF_STR_TITLE, long_title) ;
	sf_close (file) ;


	/* Check that SFC_UPDATE_HEADER_NOW correctly calculates datalength. */
	file = test_open_file_or_die (filename, SFM_RDWR, &sfinfo, SF_TRUE, __LINE__) ;
	/* Write a very small amount of new audio data that doesn't completely overwrite the existing header. */
	test_write_short_or_die (file, 0, data_out, GROW_BUFFER_AMOUNT, __LINE__) ;

	/* Update the header without closing the file. */
	sf_command (file, SFC_UPDATE_HEADER_NOW, NULL, 0) ;

	/* The file should now contain BUFFER_LEN + GROW_BUFFER_AMOUNT frames.
		Open a second handle to the file and check the reported length. */
	memset (&sfinfo1, 0, sizeof (sfinfo1)) ;
	file1 = test_open_file_or_die (filename, SFM_READ, &sfinfo1, SF_TRUE, __LINE__) ;

	frames = (BUFFER_LEN + GROW_BUFFER_AMOUNT) / sfinfo.channels ;
	exit_if_true (frames != sfinfo1.frames, "\n\nLine %d : Frame count %" PRId64 " should be %" PRId64 ".\n", __LINE__, sfinfo1.frames, frames) ;

	/* The strings are probably not readable by the second soundfile handle because write_tailer has not yet been called.
		It's a design decision whether SFC_UPDATE_HEADER_NOW should write the tailer. I think it's fine that it doesn't.  */

	sf_close (file1) ;
	sf_close (file) ;


	/* Check that sf_close correctly calculates datalength. */
	file = test_open_file_or_die (filename, SFM_RDWR, &sfinfo, SF_TRUE, __LINE__) ;
	/* Write a very small amount of new audio data that doesn't completely overwrite the existing header. */
	test_write_short_or_die (file, 0, data_out, GROW_BUFFER_AMOUNT, __LINE__) ;
	sf_close (file) ;


	/* Open file again and verify data and string. */
	file = test_open_file_or_die (filename, SFM_READ, &sfinfo, SF_TRUE, __LINE__) ;
	frames = (BUFFER_LEN + 2*GROW_BUFFER_AMOUNT) / sfinfo.channels ;
	exit_if_true (frames != sfinfo.frames, "\n\nLine %d : Frame count %" PRId64 " should be %" PRId64 ".\n", __LINE__, sfinfo.frames, frames) ;
	str = sf_get_string (file, SF_STR_TITLE) ;
	exit_if_true (str == NULL, "\n\nLine %d : SF_STR_TITLE string is NULL.\n", __LINE__) ;
	exit_if_true (strcmp (str, long_title) != 0, "\n\nLine %d : SF_STR_TITLE doesn't match what was written.\n", __LINE__) ;
	sf_close (file) ;
	unlink (filename) ;
	puts ("ok") ;
} /* string_header_update */
Пример #14
0
QString CSndFile::getString(int id)
{
    return QString::fromUtf8(sf_get_string(m_snd, id));
}
Пример #15
0
static void
string_start_end_test (const char *filename, int typemajor)
{	const char	*cptr ;
	SNDFILE		*file ;
	SF_INFO		sfinfo ;
	int			errors = 0 ;

	print_test_name ("string_start_end_test", filename) ;

	memset (&sfinfo, 0, sizeof (sfinfo)) ;
	sfinfo.samplerate	= 44100 ;
	sfinfo.channels		= 1 ;
	sfinfo.frames		= 0 ;
	sfinfo.format		= typemajor | SF_FORMAT_PCM_16 ;

	file = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, SF_TRUE, __LINE__) ;

	/* Write stuff at start of file. */
	sf_set_string (file, SF_STR_TITLE, filename) ;
	sf_set_string (file, SF_STR_SOFTWARE, software) ;
	sf_set_string (file, SF_STR_ARTIST, artist) ;
	sf_set_string (file, SF_STR_GENRE, genre) ;
	sf_set_string (file, SF_STR_TRACKNUMBER, trackno) ;

	/* Write data to file. */
	test_write_short_or_die (file, 0, data_out, BUFFER_LEN, __LINE__) ;
	test_seek_or_die (file, 0, SEEK_SET, 0, sfinfo.channels, __LINE__) ;

	/* Write more stuff at end of file. */
	sf_set_string (file, SF_STR_COPYRIGHT, copyright) ;
	sf_set_string (file, SF_STR_COMMENT, comment) ;
	sf_set_string (file, SF_STR_DATE, date) ;
	sf_set_string (file, SF_STR_ALBUM, album) ;
	sf_set_string (file, SF_STR_LICENSE, license) ;

	sf_close (file) ;

	file = test_open_file_or_die (filename, SFM_READ, &sfinfo, SF_TRUE, __LINE__) ;

	check_log_buffer_or_die (file, __LINE__) ;

	if (sfinfo.frames != BUFFER_LEN)
	{	printf ("***** Bad frame count %d (should be %d)\n\n", (int) sfinfo.frames, BUFFER_LEN) ;
		errors ++ ;
		} ;

	cptr = sf_get_string (file, SF_STR_TITLE) ;
	if (cptr == NULL || strcmp (filename, cptr) != 0)
	{	if (errors++ == 0)
			puts ("\n") ;
		printf ("    Bad filename  : %s\n", cptr) ;
		} ;

	cptr = sf_get_string (file, SF_STR_COPYRIGHT) ;
	if (cptr == NULL || strcmp (copyright, cptr) != 0)
	{	if (errors++ == 0)
			puts ("\n") ;
		printf ("    Bad copyright : %s\n", cptr) ;
		} ;

	cptr = sf_get_string (file, SF_STR_SOFTWARE) ;
	if (cptr == NULL || strstr (cptr, software) != cptr)
	{	if (errors++ == 0)
			puts ("\n") ;
		printf ("    Bad software  : %s\n", cptr) ;
		} ;

	if (str_count (cptr, "libsndfile") != 1)
	{	if (errors++ == 0)
			puts ("\n") ;
		printf ("    Bad software  : %s\n", cptr) ;
		} ;

	cptr = sf_get_string (file, SF_STR_ARTIST) ;
	if (cptr == NULL || strcmp (artist, cptr) != 0)
	{	if (errors++ == 0)
			puts ("\n") ;
		printf ("    Bad artist    : %s\n", cptr) ;
		} ;

	cptr = sf_get_string (file, SF_STR_COMMENT) ;
	if (cptr == NULL || strcmp (comment, cptr) != 0)
	{	if (errors++ == 0)
			puts ("\n") ;
		printf ("    Bad comment   : %s\n", cptr) ;
		} ;

	if (typemajor != SF_FORMAT_AIFF)
	{	cptr = sf_get_string (file, SF_STR_DATE) ;
		if (cptr == NULL || strcmp (date, cptr) != 0)
		{	if (errors++ == 0)
				puts ("\n") ;
			printf ("    Bad date      : %s\n", cptr) ;
			} ;

		cptr = sf_get_string (file, SF_STR_GENRE) ;
		if (cptr == NULL || strcmp (genre, cptr) != 0)
		{	if (errors++ == 0)
				puts ("\n") ;
			printf ("    Bad genre     : %s\n", cptr) ;
			} ;
		} ;

	switch (typemajor)
	{	case SF_FORMAT_AIFF :
		case SF_FORMAT_WAV :
		case SF_FORMAT_WAVEX :
		case SF_ENDIAN_BIG | SF_FORMAT_WAV :
		case SF_FORMAT_RF64 :
			/* These formats do not support the following. */
			break ;

		default :
			cptr = sf_get_string (file, SF_STR_ALBUM) ;
			if (cptr == NULL || strcmp (album, cptr) != 0)
			{	if (errors++ == 0)
					puts ("\n") ;
				printf ("    Bad album     : %s\n", cptr) ;
				} ;

			cptr = sf_get_string (file, SF_STR_LICENSE) ;
			if (cptr == NULL || strcmp (license, cptr) != 0)
			{	if (errors++ == 0)
					puts ("\n") ;
				printf ("    Bad license   : %s\n", cptr) ;
				} ;

			cptr = sf_get_string (file, SF_STR_TRACKNUMBER) ;
			if (cptr == NULL || strcmp (trackno, cptr) != 0)
			{	if (errors++ == 0)
					puts ("\n") ;
				printf ("    Bad track no. : %s\n", cptr) ;
				} ;
			break ;
		} ;

	if (errors > 0)
	{	printf ("\n*** Error count : %d ***\n\n", errors) ;
		dump_log_buffer (file) ;
		exit (1) ;
		} ;

	sf_close (file) ;
	unlink (filename) ;

	puts ("ok") ;
} /* string_start_end_test */
Пример #16
0
static
float *readaudio_snd(const char *filename, long *sr, unsigned int *buflen,\
		     const float nbsecs, AudioMetaData *mdata, int *error){

    SF_INFO sf_info;
    sf_info.format=0;
    SNDFILE *sndfile = sf_open(filename, SFM_READ, &sf_info);
    if (sndfile == NULL){
      *error = PHERR_SNDFILEOPEN;
      return NULL;
    }
    
    /* normalize */ 
    sf_command(sndfile, SFC_SET_NORM_FLOAT, NULL, SF_TRUE);

    if (mdata){
	init_mdata(mdata);
	/* extract metadata from file */ 
	const char *tmp = sf_get_string(sndfile, SF_STR_TITLE);
	mdata->title2 = (tmp) ? strdup(tmp): NULL;
      
	tmp = sf_get_string(sndfile,SF_STR_ARTIST);
	mdata->tpe1= (tmp) ? strdup(tmp) : NULL;
	mdata->composer = (tmp) ? strdup(tmp) : NULL;
      
	tmp = sf_get_string(sndfile,SF_STR_DATE);
	mdata->date = (tmp) ? strdup(tmp):NULL;
    } 

    *sr = (long)sf_info.samplerate;

    /*allocate input buffer for signal*/
    unsigned int src_frames = (nbsecs <= 0) ? sf_info.frames : (nbsecs*sf_info.samplerate);
    src_frames = (sf_info.frames < src_frames) ? sf_info.frames : src_frames;
    float *inbuf = (float*)malloc(src_frames*sf_info.channels*sizeof(float));
    if (inbuf == NULL){
      *error = PHERR_MEMALLOC;
      return NULL;
    }
    /*read frames */ 
    sf_count_t cnt_frames = sf_readf_float(sndfile, inbuf, src_frames);

      float *buf = (float*)malloc(cnt_frames*sizeof(float));
      if (buf == NULL){
	*error = PHERR_MEMALLOC;
	return NULL;
      }
      *buflen = cnt_frames;
      
    
    /*average across all channels*/
    int  i,j,indx=0;
    for (i=0;i<cnt_frames*sf_info.channels;i+=sf_info.channels){
	buf[indx] = 0;
	for (j=0;j<sf_info.channels;j++){
	    buf[indx] += inbuf[i+j];
	}
	buf[indx++] /= sf_info.channels;
    }
    free(inbuf);

    return buf;
}
Пример #17
0
int PCMImportFileHandle::Import(TrackFactory *trackFactory,
                                Track ***outTracks,
                                int *outNumTracks,
                                Tags *tags)
{
    wxASSERT(mFile);

    CreateProgress();

    WaveTrack **channels = new WaveTrack *[mInfo.channels];

    int c;
    for (c = 0; c < mInfo.channels; c++) {
        channels[c] = trackFactory->NewWaveTrack(mFormat, mInfo.samplerate);

        if (mInfo.channels > 1)
            switch (c) {
            case 0:
                channels[c]->SetChannel(Track::LeftChannel);
                break;
            case 1:
                channels[c]->SetChannel(Track::RightChannel);
                break;
            default:
                channels[c]->SetChannel(Track::MonoChannel);
            }
    }

    if (mInfo.channels == 2) {
        channels[0]->SetLinked(true);
        channels[1]->SetTeamed(true);
    }

    sampleCount fileTotalFrames = (sampleCount)mInfo.frames;
    sampleCount maxBlockSize = channels[0]->GetMaxBlockSize();
    bool cancelled = false;

    wxString copyEdit =
        gPrefs->Read(wxT("/FileFormats/CopyOrEditUncompressedData"), wxT("edit"));

    // Fall back to "edit" if it doesn't match anything else
    bool doEdit = true;
    if (copyEdit.IsSameAs(wxT("copy"), false))
        doEdit = false;

    // If the format is not seekable, we must use 'copy' mode,
    // because 'edit' mode depends on the ability to seek to an
    // arbitrary location in the file.
    if (!mInfo.seekable)
        doEdit = false;

    if (doEdit) {
        wxLogDebug(wxT("Importing PCM Start\n"));

        // If this mode has been selected, we form the tracks as
        // aliases to the files we're editing, i.e. ("foo.wav", 12000-18000)
        // instead of actually making fresh copies of the samples.

        for (sampleCount i = 0; i < fileTotalFrames; i += maxBlockSize) {

            sampleCount blockLen = maxBlockSize;
            if (i + blockLen > fileTotalFrames)
                blockLen = fileTotalFrames - i;

            for (c = 0; c < mInfo.channels; c++)
                channels[c]->AppendAlias(mFilename, i, blockLen, c);

            cancelled = !mProgress->Update(i, fileTotalFrames);
            if (cancelled)
                break;
        }

#ifdef EXPERIMENTAL_ONDEMAND
        //now go over the wavetrack/waveclip/sequence and load all the blockfiles into a ComputeSummaryTask.
        //Add this task to the ODManager and the Track itself.
        wxLogDebug(wxT("Importing PCM \n"));
        for (c = 0; c < mInfo.channels; c++)
        {
            ODComputeSummaryTask* computeTask;
            computeTask=new ODComputeSummaryTask;

            computeTask->SetWaveTrack(channels[c]);

            ODManager::Instance()->AddTaskToWaveTrack(computeTask,channels[c]);
        }


#endif

    }
    else {
        // Otherwise, we're in the "copy" mode, where we read in the actual
        // samples from the file and store our own local copy of the
        // samples in the tracks.

        samplePtr srcbuffer = NewSamples(maxBlockSize * mInfo.channels,
                                         mFormat);
        samplePtr buffer = NewSamples(maxBlockSize, mFormat);

        unsigned long framescompleted = 0;

        long block;
        do {
            block = maxBlockSize;

            if (mFormat == int16Sample)
                block = sf_readf_short(mFile, (short *)srcbuffer, block);
            else
                block = sf_readf_float(mFile, (float *)srcbuffer, block);

            if (block) {
                for(c=0; c<mInfo.channels; c++) {
                    if (mFormat==int16Sample) {
                        for(int j=0; j<block; j++)
                            ((short *)buffer)[j] =
                                ((short *)srcbuffer)[mInfo.channels*j+c];
                    }
                    else {
                        for(int j=0; j<block; j++)
                            ((float *)buffer)[j] =
                                ((float *)srcbuffer)[mInfo.channels*j+c];
                    }

                    channels[c]->Append(buffer, mFormat, block);
                }
                framescompleted += block;
            }

            cancelled = !mProgress->Update((long long unsigned)framescompleted,
                                           (long long unsigned)fileTotalFrames);
            if (cancelled)
                break;

        } while (block > 0);
    }

    if (cancelled) {
        for (c = 0; c < mInfo.channels; c++)
            delete channels[c];
        delete[] channels;

        return eImportCancelled;
    }

    *outNumTracks = mInfo.channels;
    *outTracks = new Track *[mInfo.channels];
    for(c = 0; c < mInfo.channels; c++) {
        channels[c]->Flush();
        (*outTracks)[c] = channels[c];
    }
    delete[] channels;

    const char *str;

    str = sf_get_string(mFile, SF_STR_TITLE);
    if (str) {
        tags->SetTag(TAG_TITLE, UTF8CTOWX(str));
    }

    str = sf_get_string(mFile, SF_STR_ARTIST);
    if (str) {
        tags->SetTag(TAG_ARTIST, UTF8CTOWX(str));
    }

    str = sf_get_string(mFile, SF_STR_COMMENT);
    if (str) {
        tags->SetTag(TAG_COMMENTS, UTF8CTOWX(str));
    }

    str = sf_get_string(mFile, SF_STR_DATE);
    if (str) {
        tags->SetTag(TAG_YEAR, UTF8CTOWX(str));
    }

    str = sf_get_string(mFile, SF_STR_COPYRIGHT);
    if (str) {
        tags->SetTag(wxT("Copyright"), UTF8CTOWX(str));
    }

    str = sf_get_string(mFile, SF_STR_SOFTWARE);
    if (str) {
        tags->SetTag(wxT("Software"), UTF8CTOWX(str));
    }

    return eImportSuccess;
}
Пример #18
0
static void
string_start_test (const char *filename, int formattype)
{	const char	*cptr ;
	SNDFILE		*file ;
	SF_INFO		sfinfo ;
	int			errors = 0 ;
	int			typemajor = SF_FORMAT_TYPEMASK & formattype ;

	print_test_name ("string_start_test", filename) ;

	memset (&sfinfo, 0, sizeof (sfinfo)) ;
	sfinfo.samplerate	= 44100 ;
	sfinfo.channels		= 1 ;
	sfinfo.frames		= 0 ;

	switch (formattype)
	{	case SF_FORMAT_OGG | SF_FORMAT_OPUS :
			/* Opus only supports some discrete sample rates. */
			sfinfo.samplerate = 48000 ;
			break ;

		case SF_FORMAT_OGG | SF_FORMAT_VORBIS :
			break ;

		default :
			formattype |= SF_FORMAT_PCM_16 ;
			break ;
		} ;
	sfinfo.format = formattype ;

	file = test_open_file_or_die (filename, SFM_WRITE, &sfinfo, SF_TRUE, __LINE__) ;

	/* Write stuff at start of file. */
	sf_set_string (file, SF_STR_TITLE, filename) ;
	sf_set_string (file, SF_STR_SOFTWARE, software) ;
	sf_set_string (file, SF_STR_ARTIST, artist) ;
	sf_set_string (file, SF_STR_COPYRIGHT, copyright) ;
	sf_set_string (file, SF_STR_COMMENT, comment) ;
	sf_set_string (file, SF_STR_DATE, date) ;
	sf_set_string (file, SF_STR_ALBUM, album) ;
	sf_set_string (file, SF_STR_LICENSE, license) ;

	/* Write data to file. */
	test_write_short_or_die (file, 0, data_out, BUFFER_LEN, __LINE__) ;

	sf_close (file) ;

	file = test_open_file_or_die (filename, SFM_READ, &sfinfo, SF_TRUE, __LINE__) ;

	check_log_buffer_or_die (file, __LINE__) ;

	if (sfinfo.frames != BUFFER_LEN)
	{	printf ("***** Bad frame count %d (should be %d)\n\n", (int) sfinfo.frames, BUFFER_LEN) ;
		errors ++ ;
		} ;

	cptr = sf_get_string (file, SF_STR_TITLE) ;
	if (cptr == NULL || strcmp (filename, cptr) != 0)
	{	if (errors++ == 0)
			puts ("\n") ;
		printf ("    Bad filename  : %s\n", cptr) ;
		} ;

	cptr = sf_get_string (file, SF_STR_COPYRIGHT) ;
	if (cptr == NULL || strcmp (copyright, cptr) != 0)
	{	if (errors++ == 0)
			puts ("\n") ;
		printf ("    Bad copyright : %s\n", cptr) ;
		} ;

	cptr = sf_get_string (file, SF_STR_SOFTWARE) ;
	if (cptr == NULL || strstr (cptr, software) != cptr)
	{	if (errors++ == 0)
			puts ("\n") ;
		printf ("    Bad software  : %s\n", cptr) ;
		} ;

	if (cptr && str_count (cptr, "libsndfile") != 1)
	{	if (errors++ == 0)
			puts ("\n") ;
		printf ("    Bad software  : %s\n", cptr) ;
		} ;

	cptr = sf_get_string (file, SF_STR_ARTIST) ;
	if (cptr == NULL || strcmp (artist, cptr) != 0)
	{	if (errors++ == 0)
			puts ("\n") ;
		printf ("    Bad artist    : %s\n", cptr) ;
		} ;

	cptr = sf_get_string (file, SF_STR_COMMENT) ;
	if (cptr == NULL || strcmp (comment, cptr) != 0)
	{	if (errors++ == 0)
			puts ("\n") ;
		printf ("    Bad comment   : %s\n", cptr) ;
		} ;

	if (typemajor != SF_FORMAT_AIFF)
	{	cptr = sf_get_string (file, SF_STR_DATE) ;
		if (cptr == NULL || strcmp (date, cptr) != 0)
		{	if (errors++ == 0)
				puts ("\n") ;
			printf ("    Bad date      : %s\n", cptr) ;
			} ;
		} ;

	if (typemajor != SF_FORMAT_WAV && typemajor != SF_FORMAT_AIFF)
	{	cptr = sf_get_string (file, SF_STR_ALBUM) ;
		if (cptr == NULL || strcmp (album, cptr) != 0)
		{	if (errors++ == 0)
				puts ("\n") ;
			printf ("    Bad album     : %s\n", cptr) ;
			} ;
		} ;

	if (typemajor != SF_FORMAT_WAV && typemajor != SF_FORMAT_AIFF && typemajor != SF_FORMAT_RF64)
	{	cptr = sf_get_string (file, SF_STR_LICENSE) ;
		if (cptr == NULL || strcmp (license, cptr) != 0)
		{	if (errors++ == 0)
				puts ("\n") ;
			printf ("    Bad license   : %s\n", cptr) ;
			} ;
		} ;

	if (errors > 0)
	{	printf ("\n*** Error count : %d ***\n\n", errors) ;
		dump_log_buffer (file) ;
		exit (1) ;
		} ;

	sf_close (file) ;
	unlink (filename) ;

	puts ("ok") ;
} /* string_start_test */