summaryrefslogtreecommitdiff
path: root/libs/ardour/externalsource.cc
blob: 4fb102a4a0159f4099ec9c4d4c77a0aa26cec8f1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
/*
    Copyright (C) 2006 Paul Davis 

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

*/

#include <sys/stat.h>
#include <unistd.h>
#include <sys/time.h>

#include <sndfile.h>

#include <pbd/mountpoint.h>
#include <ardour/externalsource.h>
#include <ardour/sndfilesource.h>
#include <ardour/sndfile_helpers.h>

// if these headers come before sigc++ is included
// the parser throws ObjC++ errors. (nil is a keyword)
#ifdef HAVE_COREAUDIO 
#include <ardour/coreaudio_source.h>
#include <AudioToolbox/ExtendedAudioFile.h>
#include <AudioToolbox/AudioFormat.h>
#endif // HAVE_COREAUDIO

#include "i18n.h"

using namespace ARDOUR;

string ExternalSource::peak_dir = "";

ExternalSource::ExternalSource (const XMLNode& node)
	: Source (node)
{
}

ExternalSource::ExternalSource (const string& idstr, bool build_peak)
	: Source(build_peak)
{
}

ExternalSource::~ExternalSource ()
{
}

jack_nframes_t
ExternalSource::read_unlocked (Sample *dst, jack_nframes_t start, jack_nframes_t cnt, char * workbuf) const
{
	return read (dst, start, cnt, workbuf);
}

string
ExternalSource::peak_path (string audio_path)
{
	/* XXX hardly bombproof! fix me */

	struct stat stat_file;
	struct stat stat_mount;

	string mp = mountpoint (audio_path);

	stat (audio_path.c_str(), &stat_file);
	stat (mp.c_str(), &stat_mount);

	char buf[32];
	snprintf (buf, sizeof (buf), "%ld-%ld-%d.peak", stat_mount.st_ino, stat_file.st_ino, channel);

	string res = peak_dir;
	res += buf;

	return res;
}

#ifdef HAVE_COREAUDIO

ExternalSource*
ExternalSource::create (const XMLNode& node)
{
	ExternalSource* es = 0;

	try {
		es = new CoreAudioSource (node);
	} 
	
	catch (failed_constructor& err) {
		es = new SndFileSource (node);
	}

	es = new SndFileSource (node);

	return es;
}

#else

ExternalSource*
ExternalSource::create (const XMLNode& node)
{
	return new SndFileSource (node);
}

#endif // HAVE_COREAUDIO

#ifdef HAVE_COREAUDIO
ExternalSource*
ExternalSource::create (const string& idstr, bool build_peak)
{
	ExternalSource* es = 0;

	try {
		es = new CoreAudioSource (idstr, build_peak);
	}

	catch (failed_constructor& err) {
		es = new SndFileSource (idstr, build_peak);
	}

	return es;
}

#else

ExternalSource*
ExternalSource::create (const string& idstr, bool build_peak)
{
	return new SndFileSource (idstr, build_peak);
}

#endif // HAVE_COREAUDIO

#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
ExternalSource::get_soundfile_info (string path, SoundFileInfo& _info, string& error_msg)
{
#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) { 
		char errbuf[256];
		error_msg = sf_error_str (0, errbuf, sizeof (errbuf) - 1);
		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;
}