From be60137cfb31ef1266f5132a9fd465c01ce074a9 Mon Sep 17 00:00:00 2001 From: Taybin Rutkin Date: Tue, 7 Mar 2006 23:41:52 +0000 Subject: [PATCH] Wrapper around SF_INFO and AudioFileBasicDescription. git-svn-id: svn://localhost/trunk/ardour2@357 d708f5d6-7413-0410-9779-e7cbd77b26cf --- libs/ardour/SConscript | 4 +- libs/ardour/ardour/sndfile_helpers.h | 9 +++ libs/ardour/sndfile_helpers.cc | 104 +++++++++++++++++++++++++++ 3 files changed, 115 insertions(+), 2 deletions(-) diff --git a/libs/ardour/SConscript b/libs/ardour/SConscript index 58e2cb908c..59d3d90e89 100644 --- a/libs/ardour/SConscript +++ b/libs/ardour/SConscript @@ -163,8 +163,8 @@ if conf.CheckCHeader('/System/Library/Frameworks/CoreMIDI.framework/Headers/Core ardour.Append(LINKFLAGS="-framework CoreMIDI") if conf.CheckCHeader('/System/Library/Frameworks/AudioToolbox.framework/Headers/ExtendedAudioFile.h'): - ardour.Append(LINKFLAGS="-framework AudioToolbox") - extra_sources += coreaudio_files + ardour.Append(CXXFLAGS="-DHAVE_COREAUDIO") + extra_sources += coreaudio_files ardour = conf.Finish () diff --git a/libs/ardour/ardour/sndfile_helpers.h b/libs/ardour/ardour/sndfile_helpers.h index bae73a0377..b616a5470e 100644 --- a/libs/ardour/ardour/sndfile_helpers.h +++ b/libs/ardour/ardour/sndfile_helpers.h @@ -35,4 +35,13 @@ int sndfile_data_width (int format); string sndfile_major_format(int); string sndfile_minor_format(int); +struct SoundFileInfo { + float samplerate; + uint16_t channels; + int64_t length; + std::string format_name; +}; + +bool get_soundfile_info (string path, SoundFileInfo& _info); + #endif /* __sndfile_helpers_h__ */ diff --git a/libs/ardour/sndfile_helpers.cc b/libs/ardour/sndfile_helpers.cc index 21d8c72b2b..af9816c29c 100644 --- a/libs/ardour/sndfile_helpers.cc +++ b/libs/ardour/sndfile_helpers.cc @@ -4,6 +4,11 @@ #include #include +#ifdef HAVE_COREAUDIO +#include +#include +#endif // HAVE_COREAUDIO + #include "i18n.h" using std::map; @@ -191,3 +196,102 @@ sndfile_minor_format(int format) return "-Unknown-"; } } + +#ifdef HAVE_COREAUDIO +std::string +CFStringRefToStdString(CFStringRef stringRef) +{ + CFIndex size = + CFStringGetMaximumSizeForEncoding(CFStringGetLength(stringRef) , + kCFStringEncodingASCII); + char *buf = new char[size]; + + std::string result; + + if(CFStringGetCString(stringRef, buf, size, kCFStringEncodingASCII)) { + result = buf; + } + delete [] buf; + return result; +} +#endif // HAVE_COREAUDIO + +bool +get_soundfile_info (string path, SoundFileInfo& _info) +{ +#ifdef HAVE_COREAUDIO + OSStatus err = noErr; + FSRef* ref; + ExtAudioFileRef af = 0; + size_t size; + CFStringRef name; + + err = FSPathMakeRef ((UInt8*)path.c_str(), ref, 0); + if (err != noErr) { + ExtAudioFileDispose (af); + goto libsndfile; + } + + err = ExtAudioFileOpen(ref, &af); + if (err != noErr) { + ExtAudioFileDispose (af); + goto libsndfile; + } + + AudioStreamBasicDescription absd; + memset(&absd, 0, sizeof(absd)); + size = sizeof(AudioStreamBasicDescription); + err = ExtAudioFileGetProperty(af, + kExtAudioFileProperty_FileDataFormat, &size, &absd); + if (err != noErr) { + ExtAudioFileDispose (af); + goto libsndfile; + } + + _info.samplerate = absd.mSampleRate; + _info.channels = absd.mChannelsPerFrame; + + size = sizeof(_info.length); + err = ExtAudioFileGetProperty(af, kExtAudioFileProperty_FileLengthFrames, &size, &_info.length); + if (err != noErr) { + ExtAudioFileDispose (af); + goto libsndfile; + } + + size = sizeof(CFStringRef); + err = AudioFormatGetProperty( + kAudioFormatProperty_FormatName, sizeof(absd), &absd, &size, &name); + if (err != noErr) { + ExtAudioFileDispose (af); + goto libsndfile; + } + + _info.format_name = CFStringRefToStdString(name); + + ExtAudioFileDispose (af); + return true; + +libsndfile: +#endif // HAVE_COREAUDIO + + SNDFILE *sf; + SF_INFO sf_info; + + sf_info.format = 0; // libsndfile says to clear this before sf_open(). + + if ((sf = sf_open ((char*) path.c_str(), SFM_READ, &sf_info)) < 0) { + return false; + } + + sf_close (sf); + + _info.samplerate = sf_info.samplerate; + _info.channels = sf_info.channels; + _info.length = sf_info.frames; + _info.format_name = string_compose("Format: %1, %2", + sndfile_major_format(sf_info.format), + sndfile_minor_format(sf_info.format)); + + return true; +} +