Urbi SDK Remote for C++  2.7.5
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     GD_FINFO_TRACE("unknown sound format: %s", s);
00059     return SOUND_UNKNOWN;
00060   }
00061 
00062 
00063   /*---------------------.
00064   | USoundSampleFormat.  |
00065   `---------------------*/
00066 
00067   std::istream&
00068   operator>> (std::istream& is, USoundSampleFormat& f)
00069   {
00070     int v = 0;
00071     is >> v;
00072     f = USoundSampleFormat(v);
00073     return is;
00074   }
00075 
00076   static
00077   std::ostream&
00078   operator<<(std::ostream& o, USoundSampleFormat f)
00079   {
00080     switch (f)
00081     {
00082     case SAMPLE_SIGNED:
00083       return o << "signed";
00084     case SAMPLE_UNSIGNED:
00085       return o << "unsigned";
00086     default:
00087       return o << "unknown[" << (int)f << "]";
00088     }
00089     unreachable();
00090   }
00091 
00092 
00093   /*---------.
00094   | USound.  |
00095   `---------*/
00096 
00097   void
00098   USound::init()
00099   {
00100     data = 0;
00101     size = sampleSize = channels = rate = 0;
00102     soundFormat = SOUND_UNKNOWN;
00103     sampleFormat = SAMPLE_UNSIGNED;
00104   }
00105 
00106   USound
00107   USound::make()
00108   {
00109     USound res;
00110     res.init();
00111     return res;
00112   }
00113 
00114   bool
00115   USound::operator==(const USound &b) const
00116   {
00117     return !memcmp(this, &b, sizeof(USound));
00118   }
00119 
00120   const char*
00121   USound::format_string() const
00122   {
00123     return ::urbi::format_string(soundFormat);
00124   }
00125 
00126   std::string
00127   USound::headers_() const
00128   {
00129     return libport::format("%s %s %s %s %d",
00130                            format_string(),
00131                            channels, rate,
00132                            sampleSize, int(sampleFormat));
00133   }
00134 
00135   std::ostream&
00136   USound::dump(std::ostream& o) const
00137   {
00138     return o <<  "sound(format: " << format_string() << ", "
00139              <<          "size: " << size << ", "
00140              <<      "channels: " << channels << ", "
00141              <<          "rate: " << rate << ", "
00142              <<   "sample size: " << sampleSize << ", "
00143              << "sample format: " << sampleFormat
00144              << ")";
00145   }
00146 
00147   std::ostream&
00148   operator<< (std::ostream& o, const USound& s)
00149   {
00150     return s.dump(o);
00151   }
00152 
00153 }