MCPcopy Create free account
hub / github.com/cinder/Cinder / determineFileType

Function determineFileType

src/cinder/audio/linux/FileAudioLoader.cpp:166–216  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

164
165//!
166FileType determineFileType( const std::string &extension, const ci::IStreamRef& stream )
167{
168 FileType result = FileType::UNKNOWN;
169
170 // Try MP3 first
171 if( extension == ".mp3" ) {
172 ProcessScopedMpg123::initialize();
173
174 stream->seekAbsolute( 0 );
175
176 int ret = MPG123_OK;
177 mpg123_handle* handle = mpg123_new( nullptr, &ret );
178 if( ( MPG123_OK == ret ) && ( MPG123_OK == mpg123_replace_reader_handle( handle, IStreamMpg123::read, IStreamMpg123::seek, nullptr ) ) ) {
179 if( MPG123_OK == mpg123_open_handle( handle, stream.get() ) ) {
180 long rate = 0;
181 int channels = 0;
182 int encodings = -1;
183 off_t len = MPG123_ERR;
184 if( ( MPG123_OK == mpg123_getformat( handle, &rate, &channels, &encodings ) ) && ( len =mpg123_length( handle ) ) ) {
185 if( ( MPG123_ERR != len ) && ( len > 0 ) ) {
186 result = FileType::MP3;
187 }
188 }
189 }
190 mpg123_delete( handle );
191 }
192 }
193
194 // If not an .mp3, try libsndfile
195 if( FileType::UNKNOWN == result ) {
196 stream->seekAbsolute( 0 );
197
198 SF_VIRTUAL_IO vio;
199 vio.get_filelen = IStreamSndFile::get_filelen;
200 vio.seek = IStreamSndFile::seek;
201 vio.read = IStreamSndFile::read;
202 vio.write = IStreamSndFile::write;
203 vio.tell = IStreamSndFile::tell;
204
205 SF_INFO info;
206 SNDFILE* handle = sf_open_virtual( &vio, SFM_READ, &info, static_cast<void*>( stream.get() ) );
207 if( nullptr != handle ) {
208 result = FileType::PCM;
209 sf_close( handle );
210 }
211 }
212
213 stream->seekAbsolute( 0 );
214
215 return result;
216}
217
218// ----------------------------------------------------------------------------------------------------
219// FileLoader

Callers 1

initMethod · 0.85

Calls 2

seekAbsoluteMethod · 0.45
getMethod · 0.45

Tested by

no test coverage detected