Urbi SDK Remote for C++  2.7.3
usound.cc
Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2009-2011, Gostai S.A.S.
00003  *
00004  * This software is provided "as is" without warranty of any kind,
00005  * either expressed or implied, including but not limited to the
00006  * implied warranties of fitness for a particular purpose.
00007  *
00008  * See the LICENSE file for more information.
00009  */
00010 
00012 
00013 #include <sstream>
00014 #include <libport/cassert>
00015 #include <libport/cstring>
00016 #include <libport/debug.hh>
00017 #include <libport/format.hh>
00018 
00019 #include <urbi/usound.hh>
00020 
00021 #define cardinality_of(Array) (sizeof (Array) / sizeof (*(Array)))
00022 
00023 GD_CATEGORY(Urbi.UValue);
00024 
00025 namespace urbi
00026 {
00027 
00028   /*---------------.
00029   | USoundFormat.  |
00030   `---------------*/
00031 
00032   static const char* formats[] =
00033   {
00034     "raw",
00035     "wav",
00036     "mp3",
00037     "ogg",
00038     "unknown format",
00039   };
00040 
00041   const char*
00042   format_string(USoundFormat f)
00043   {
00044     if (f < 0 || int(cardinality_of(formats)) <= f)
00045     {
00046       GD_FERROR("invalid USoundFormat value: %d", f);
00047       f = SOUND_UNKNOWN;
00048     }
00049     return formats[f];
00050   }
00051 
00052   USoundFormat
00053   parse_sound_format(const std::string& s)
00054   {
00055     for (unsigned i = 0; i < cardinality_of(formats); ++i)
00056       if (s == formats[i])
00057         return static_cast<USoundFormat>(i);
00058     return SOUND_UNKNOWN;
00059   }
00060 
00061 
00062   /*---------------------.
00063   | USoundSampleFormat.  |
00064   `---------------------*/
00065 
00066   std::istream&
00067   operator>> (std::istream& is, USoundSampleFormat& f)
00068   {
00069     int v = 0;
00070     is >> v;
00071     f = USoundSampleFormat(v);
00072     return is;
00073   }
00074 
00075   static
00076   std::ostream&
00077   operator<<(std::ostream& o, USoundSampleFormat f)
00078   {
00079     switch (f)
00080     {
00081     case SAMPLE_SIGNED:
00082       return o << "signed";
00083     case SAMPLE_UNSIGNED:
00084       return o << "unsigned";
00085     default:
00086       return o << "unknown[" << (int)f << "]";
00087     }
00088     unreachable();
00089   }
00090 
00091 
00092   /*---------.
00093   | USound.  |
00094   `---------*/
00095 
00096   void
00097   USound::init()
00098   {
00099     data = 0;
00100     size = sampleSize = channels = rate = 0;
00101     soundFormat = SOUND_UNKNOWN;
00102     sampleFormat = SAMPLE_UNSIGNED;
00103   }
00104 
00105   USound
00106   USound::make()
00107   {
00108     USound res;
00109     res.init();
00110     return res;
00111   }
00112 
00113   bool
00114   USound::operator==(const USound &b) const
00115   {
00116     return !memcmp(this, &b, sizeof(USound));
00117   }
00118 
00119   const char*
00120   USound::format_string() const
00121   {
00122     return ::urbi::format_string(soundFormat);
00123   }
00124 
00125   std::string
00126   USound::headers_() const
00127   {
00128     return libport::format("%s %s %s %s %d",
00129                            format_string(),
00130                            channels, rate,
00131                            sampleSize, int(sampleFormat));
00132   }
00133 
00134   std::ostream&
00135   USound::dump(std::ostream& o) const
00136   {
00137     return o <<  "sound(format: " << format_string() << ", "
00138              <<          "size: " << size << ", "
00139              <<      "channels: " << channels << ", "
00140              <<          "rate: " << rate << ", "
00141              <<   "sample size: " << sampleSize << ", "
00142              << "sample format: " << sampleFormat
00143              << ")";
00144   }
00145 
00146   std::ostream&
00147   operator<< (std::ostream& o, const USound& s)
00148   {
00149     return s.dump(o);
00150   }
00151 
00152 }