Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-06-02 08:58:13

0001 ////////////////////////////////////////////////////////////
0002 //
0003 // SFML - Simple and Fast Multimedia Library
0004 // Copyright (C) 2007-2023 Laurent Gomila (laurent@sfml-dev.org)
0005 //
0006 // This software is provided 'as-is', without any express or implied warranty.
0007 // In no event will the authors be held liable for any damages arising from the use of this software.
0008 //
0009 // Permission is granted to anyone to use this software for any purpose,
0010 // including commercial applications, and to alter it and redistribute it freely,
0011 // subject to the following restrictions:
0012 //
0013 // 1. The origin of this software must not be misrepresented;
0014 //    you must not claim that you wrote the original software.
0015 //    If you use this software in a product, an acknowledgment
0016 //    in the product documentation would be appreciated but is not required.
0017 //
0018 // 2. Altered source versions must be plainly marked as such,
0019 //    and must not be misrepresented as being the original software.
0020 //
0021 // 3. This notice may not be removed or altered from any source distribution.
0022 //
0023 ////////////////////////////////////////////////////////////
0024 
0025 #ifndef SFML_INPUTSOUNDFILE_HPP
0026 #define SFML_INPUTSOUNDFILE_HPP
0027 
0028 ////////////////////////////////////////////////////////////
0029 // Headers
0030 ////////////////////////////////////////////////////////////
0031 #include <SFML/Audio/Export.hpp>
0032 #include <SFML/System/NonCopyable.hpp>
0033 #include <SFML/System/Time.hpp>
0034 #include <string>
0035 #include <cstddef>
0036 
0037 
0038 namespace sf
0039 {
0040 class InputStream;
0041 class SoundFileReader;
0042 
0043 ////////////////////////////////////////////////////////////
0044 /// \brief Provide read access to sound files
0045 ///
0046 ////////////////////////////////////////////////////////////
0047 class SFML_AUDIO_API InputSoundFile : NonCopyable
0048 {
0049 public:
0050 
0051     ////////////////////////////////////////////////////////////
0052     /// \brief Default constructor
0053     ///
0054     ////////////////////////////////////////////////////////////
0055     InputSoundFile();
0056 
0057     ////////////////////////////////////////////////////////////
0058     /// \brief Destructor
0059     ///
0060     ////////////////////////////////////////////////////////////
0061     ~InputSoundFile();
0062 
0063     ////////////////////////////////////////////////////////////
0064     /// \brief Open a sound file from the disk for reading
0065     ///
0066     /// The supported audio formats are: WAV (PCM only), OGG/Vorbis, FLAC, MP3.
0067     /// The supported sample sizes for FLAC and WAV are 8, 16, 24 and 32 bit.
0068     ///
0069     /// Because of minimp3_ex limitation, for MP3 files with big (>16kb) APEv2 tag,
0070     /// it may not be properly removed, tag data will be treated as MP3 data
0071     /// and there is a low chance of garbage decoded at the end of file.
0072     /// See also: https://github.com/lieff/minimp3
0073     ///
0074     /// \param filename Path of the sound file to load
0075     ///
0076     /// \return True if the file was successfully opened
0077     ///
0078     ////////////////////////////////////////////////////////////
0079     bool openFromFile(const std::string& filename);
0080 
0081     ////////////////////////////////////////////////////////////
0082     /// \brief Open a sound file in memory for reading
0083     ///
0084     /// The supported audio formats are: WAV (PCM only), OGG/Vorbis, FLAC.
0085     /// The supported sample sizes for FLAC and WAV are 8, 16, 24 and 32 bit.
0086     ///
0087     /// \param data        Pointer to the file data in memory
0088     /// \param sizeInBytes Size of the data to load, in bytes
0089     ///
0090     /// \return True if the file was successfully opened
0091     ///
0092     ////////////////////////////////////////////////////////////
0093     bool openFromMemory(const void* data, std::size_t sizeInBytes);
0094 
0095     ////////////////////////////////////////////////////////////
0096     /// \brief Open a sound file from a custom stream for reading
0097     ///
0098     /// The supported audio formats are: WAV (PCM only), OGG/Vorbis, FLAC.
0099     /// The supported sample sizes for FLAC and WAV are 8, 16, 24 and 32 bit.
0100     ///
0101     /// \param stream Source stream to read from
0102     ///
0103     /// \return True if the file was successfully opened
0104     ///
0105     ////////////////////////////////////////////////////////////
0106     bool openFromStream(InputStream& stream);
0107 
0108     ////////////////////////////////////////////////////////////
0109     /// \brief Get the total number of audio samples in the file
0110     ///
0111     /// \return Number of samples
0112     ///
0113     ////////////////////////////////////////////////////////////
0114     Uint64 getSampleCount() const;
0115 
0116     ////////////////////////////////////////////////////////////
0117     /// \brief Get the number of channels used by the sound
0118     ///
0119     /// \return Number of channels (1 = mono, 2 = stereo)
0120     ///
0121     ////////////////////////////////////////////////////////////
0122     unsigned int getChannelCount() const;
0123 
0124     ////////////////////////////////////////////////////////////
0125     /// \brief Get the sample rate of the sound
0126     ///
0127     /// \return Sample rate, in samples per second
0128     ///
0129     ////////////////////////////////////////////////////////////
0130     unsigned int getSampleRate() const;
0131 
0132     ////////////////////////////////////////////////////////////
0133     /// \brief Get the total duration of the sound file
0134     ///
0135     /// This function is provided for convenience, the duration is
0136     /// deduced from the other sound file attributes.
0137     ///
0138     /// \return Duration of the sound file
0139     ///
0140     ////////////////////////////////////////////////////////////
0141     Time getDuration() const;
0142 
0143     ////////////////////////////////////////////////////////////
0144     /// \brief Get the read offset of the file in time
0145     ///
0146     /// \return Time position
0147     ///
0148     ////////////////////////////////////////////////////////////
0149     Time getTimeOffset() const;
0150 
0151     ////////////////////////////////////////////////////////////
0152     /// \brief Get the read offset of the file in samples
0153     ///
0154     /// \return Sample position
0155     ///
0156     ////////////////////////////////////////////////////////////
0157     Uint64 getSampleOffset() const;
0158 
0159     ////////////////////////////////////////////////////////////
0160     /// \brief Change the current read position to the given sample offset
0161     ///
0162     /// This function takes a sample offset to provide maximum
0163     /// precision. If you need to jump to a given time, use the
0164     /// other overload.
0165     ///
0166     /// The sample offset takes the channels into account.
0167     /// If you have a time offset instead, you can easily find
0168     /// the corresponding sample offset with the following formula:
0169     /// `timeInSeconds * sampleRate * channelCount`
0170     /// If the given offset exceeds to total number of samples,
0171     /// this function jumps to the end of the sound file.
0172     ///
0173     /// \param sampleOffset Index of the sample to jump to, relative to the beginning
0174     ///
0175     ////////////////////////////////////////////////////////////
0176     void seek(Uint64 sampleOffset);
0177 
0178     ////////////////////////////////////////////////////////////
0179     /// \brief Change the current read position to the given time offset
0180     ///
0181     /// Using a time offset is handy but imprecise. If you need an accurate
0182     /// result, consider using the overload which takes a sample offset.
0183     ///
0184     /// If the given time exceeds to total duration, this function jumps
0185     /// to the end of the sound file.
0186     ///
0187     /// \param timeOffset Time to jump to, relative to the beginning
0188     ///
0189     ////////////////////////////////////////////////////////////
0190     void seek(Time timeOffset);
0191 
0192     ////////////////////////////////////////////////////////////
0193     /// \brief Read audio samples from the open file
0194     ///
0195     /// \param samples  Pointer to the sample array to fill
0196     /// \param maxCount Maximum number of samples to read
0197     ///
0198     /// \return Number of samples actually read (may be less than \a maxCount)
0199     ///
0200     ////////////////////////////////////////////////////////////
0201     Uint64 read(Int16* samples, Uint64 maxCount);
0202 
0203     ////////////////////////////////////////////////////////////
0204     /// \brief Close the current file
0205     ///
0206     ////////////////////////////////////////////////////////////
0207     void close();
0208 
0209 private:
0210 
0211     ////////////////////////////////////////////////////////////
0212     // Member data
0213     ////////////////////////////////////////////////////////////
0214     SoundFileReader* m_reader;       //!< Reader that handles I/O on the file's format
0215     InputStream*     m_stream;       //!< Input stream used to access the file's data
0216     bool             m_streamOwned;  //!< Is the stream internal or external?
0217     Uint64           m_sampleOffset; //!< Sample Read Position
0218     Uint64           m_sampleCount;  //!< Total number of samples in the file
0219     unsigned int     m_channelCount; //!< Number of channels of the sound
0220     unsigned int     m_sampleRate;   //!< Number of samples per second
0221 };
0222 
0223 } // namespace sf
0224 
0225 
0226 #endif // SFML_INPUTSOUNDFILE_HPP
0227 
0228 
0229 ////////////////////////////////////////////////////////////
0230 /// \class sf::InputSoundFile
0231 /// \ingroup audio
0232 ///
0233 /// This class decodes audio samples from a sound file. It is
0234 /// used internally by higher-level classes such as sf::SoundBuffer
0235 /// and sf::Music, but can also be useful if you want to process
0236 /// or analyze audio files without playing them, or if you want to
0237 /// implement your own version of sf::Music with more specific
0238 /// features.
0239 ///
0240 /// Usage example:
0241 /// \code
0242 /// // Open a sound file
0243 /// sf::InputSoundFile file;
0244 /// if (!file.openFromFile("music.ogg"))
0245 ///     /* error */;
0246 ///
0247 /// // Print the sound attributes
0248 /// std::cout << "duration: " << file.getDuration().asSeconds() << std::endl;
0249 /// std::cout << "channels: " << file.getChannelCount() << std::endl;
0250 /// std::cout << "sample rate: " << file.getSampleRate() << std::endl;
0251 /// std::cout << "sample count: " << file.getSampleCount() << std::endl;
0252 ///
0253 /// // Read and process batches of samples until the end of file is reached
0254 /// sf::Int16 samples[1024];
0255 /// sf::Uint64 count;
0256 /// do
0257 /// {
0258 ///     count = file.read(samples, 1024);
0259 ///
0260 ///     // process, analyze, play, convert, or whatever
0261 ///     // you want to do with the samples...
0262 /// }
0263 /// while (count > 0);
0264 /// \endcode
0265 ///
0266 /// \see sf::SoundFileReader, sf::OutputSoundFile
0267 ///
0268 ////////////////////////////////////////////////////////////