Main Page   Namespace List   Class Hierarchy   Compound List   File List   Namespace Members   Compound Members   Related Pages  

Audio.h

00001 //
00002 // OpenVRML
00003 //
00004 // Copyright (C) 1999  Kumaran Santhanam
00005 // 
00006 // This library is free software; you can redistribute it and/or
00007 // modify it under the terms of the GNU Lesser General Public
00008 // License as published by the Free Software Foundation; either
00009 // version 2.1 of the License, or (at your option) any later version.
00010 // 
00011 // This library is distributed in the hope that it will be useful,
00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014 // Lesser General Public License for more details.
00015 // 
00016 // You should have received a copy of the GNU Lesser General Public
00017 // License along with this library; if not, write to the Free Software
00018 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00019 // 
00020 
00021 #ifndef AUDIO_H
00022 #define AUDIO_H
00023 
00024 #include <stdio.h>
00025 #include <math.h>
00026 #include <string>
00027 #include "common.h"
00028 
00029 namespace OpenVRML {
00030 
00031     class Doc;
00032     class MFString;
00033 
00034     OPENVRML_SCOPE enum AudioEncoding { AUDIO_LINEAR, AUDIO_ULAW };
00035 
00036     class OPENVRML_SCOPE Audio {
00037         Doc *           _doc;
00038 
00039         AudioEncoding   _encoding;
00040         int             _channels;
00041         int             _bits_per_sample;
00042         int             _samples_per_sec;
00043 
00044         // Samples are stored in aligned blocks.  Sometimes, the
00045         // block will be larger than the sample itself.  Usually,
00046         // however, an 8-bit sample will be in a 1-byte block and
00047         // a 16-bit sample will be in a 2-byte block.
00048         int             _sample_blocksize;
00049 
00050         int             _num_samples;
00051         unsigned char * _samples;
00052 
00053     public:
00054         explicit Audio(const std::string & url, Doc * relative = 0);
00055         ~Audio ();
00056 
00057         bool setURL(const std::string & url, Doc * relative = 0);
00058         bool tryURLs(const MFString & urls, Doc * relative = 0);
00059 
00060         const char *url() const;
00061 
00062         AudioEncoding encoding() const        { return _encoding; }
00063         int channels() const                  { return _channels; }
00064         int bitsPerSample() const             { return _bits_per_sample; }
00065         int samplesPerSec() const             { return _samples_per_sec; }
00066         int sampleBlockSize() const           { return _sample_blocksize; }
00067         int numSamples() const                { return _num_samples; }
00068 
00069         int numBytes() const         { return _num_samples * _sample_blocksize; }
00070         const unsigned char *samples() const  { return _samples; }
00071 
00072         double duration() const  {
00073             if (_samples_per_sec > 0)
00074                 return (double)_num_samples/(double)_samples_per_sec;
00075             else
00076                 return 0;
00077         }
00078 
00079         // Get the sample index given a floating point time index
00080         // If the time index is greater than the duration, the sample
00081         // index is wrapped back to the beginning of the sample.
00082         // From: Alex Funk <Alexander.Funk@nord-com.net>
00083         // Avoid int overflow when multiplying time_index by samples_per_sec
00084         // Modified to use fmod() by Kumaran Santhanam.
00085         int getByteIndex(double time_index) const  {
00086           if (_num_samples > 0 && _samples_per_sec > 0)
00087             return _sample_blocksize *
00088               (int)(fmod(time_index, duration()) * (double)_samples_per_sec);
00089           else
00090             return -1;
00091         }
00092 
00093     private:
00094         bool wavread (FILE *fp);
00095     };
00096 }
00097 
00098 #endif // AUDIO_H