1 /******************************************************************************* 2 * Copyright 2009-2016 Jörg Müller 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 ******************************************************************************/ 16 17 #pragma once 18 19 #include "IReader.h" 20 21 #ifdef LIBSNDFILE_PLUGIN 22 #define AUD_BUILD_PLUGIN 23 #endif 24 25 /** 26 * @file SndFileReader.h 27 * @ingroup plugin 28 * The SndFileReader class. 29 */ 30 31 #include <string> 32 #include <sndfile.h> 33 #include <memory> 34 35 AUD_NAMESPACE_BEGIN 36 37 class Buffer; 38 39 /** 40 * This class reads a sound file via libsndfile. 41 */ 42 class AUD_PLUGIN_API SndFileReader : public IReader 43 { 44 private: 45 /** 46 * The current position in samples. 47 */ 48 int m_position; 49 50 /** 51 * The sample count in the file. 52 */ 53 int m_length; 54 55 /** 56 * Whether the file is seekable. 57 */ 58 bool m_seekable; 59 60 /** 61 * The specification of the audio data. 62 */ 63 Specs m_specs; 64 65 /** 66 * The sndfile. 67 */ 68 SNDFILE* m_sndfile; 69 70 /** 71 * The virtual IO structure for memory file reading. 72 */ 73 SF_VIRTUAL_IO m_vio; 74 75 /** 76 * The pointer to the memory file. 77 */ 78 std::shared_ptr<Buffer> m_membuffer; 79 80 /** 81 * The current reading pointer of the memory file. 82 */ 83 int m_memoffset; 84 85 // Functions for libsndfile virtual IO functionality 86 AUD_LOCAL static sf_count_t vio_get_filelen(void* user_data); 87 AUD_LOCAL static sf_count_t vio_seek(sf_count_t offset, int whence, void* user_data); 88 AUD_LOCAL static sf_count_t vio_read(void* ptr, sf_count_t count, void* user_data); 89 AUD_LOCAL static sf_count_t vio_tell(void* user_data); 90 91 // delete copy constructor and operator= 92 SndFileReader(const SndFileReader&) = delete; 93 SndFileReader& operator=(const SndFileReader&) = delete; 94 95 public: 96 /** 97 * Creates a new reader. 98 * \param filename The path to the file to be read. 99 * \exception Exception Thrown if the file specified does not exist or 100 * cannot be read with libsndfile. 101 */ 102 SndFileReader(std::string filename); 103 104 /** 105 * Creates a new reader. 106 * \param buffer The buffer to read from. 107 * \exception Exception Thrown if the buffer specified cannot be read 108 * with libsndfile. 109 */ 110 SndFileReader(std::shared_ptr<Buffer> buffer); 111 112 /** 113 * Destroys the reader and closes the file. 114 */ 115 virtual ~SndFileReader(); 116 117 virtual bool isSeekable() const; 118 virtual void seek(int position); 119 virtual int getLength() const; 120 virtual int getPosition() const; 121 virtual Specs getSpecs() const; 122 virtual void read(int& length, bool& eos, sample_t* buffer); 123 }; 124 125 AUD_NAMESPACE_END 126