libpsynth 0.2.1
/home/raskolnikov/dev/psynth/trunk/src/psynth/app/output_director_alsa.hpp
Go to the documentation of this file.
00001 /***************************************************************************
00002  *                                                                         *
00003  *   PSYCHOSYNTH                                                           *
00004  *   ===========                                                           *
00005  *                                                                         *
00006  *   Copyright (C) Juan Pedro Bolivar Puente 2007, 2008                    *
00007  *                                                                         *
00008  *   This program is free software: you can redistribute it and/or modify  *
00009  *   it under the terms of the GNU General Public License as published by  *
00010  *   the Free Software Foundation, either version 3 of the License, or     *
00011  *   (at your option) any later version.                                   *
00012  *                                                                         *
00013  *   This program is distributed in the hope that it will be useful,       *
00014  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00015  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
00016  *   GNU General Public License for more details.                          *
00017  *                                                                         *
00018  *   You should have received a copy of the GNU General Public License     *
00019  *   along with this program.  If not, see <http://www.gnu.org/licenses/>. *
00020  *                                                                         *
00021  ***************************************************************************/
00022 
00023 #ifndef PSYNTH_OUTPUT_DIRECTOR_ALSA_H
00024 #define PSYNTH_OUTPUT_DIRECTOR_ALSA_H
00025 
00026 #include <psynth/app/defaults_alsa.hpp>
00027 #include <psynth/app/output_director.hpp>
00028 #include <psynth/io/alsa_output.hpp>
00029 #include <psynth/io/buffered_output.hpp>
00030 
00031 namespace psynth
00032 {
00033 
00034 class output_director_alsa : public output_director
00035 {
00036     boost::signals::connection m_on_device_change_slot;
00037     
00038     ~output_director_alsa()
00039     {
00040         if (m_output)
00041             stop();
00042     }
00043     
00044     void on_device_change (base::conf_node& conf)
00045     {
00046         auto old_state = m_output->state ();
00047         build_output (*conf.parent ());
00048         if (old_state == io::async_state::running)
00049             m_output->start ();
00050     }
00051     
00052     virtual graph::audio_async_output_ptr
00053     do_start (base::conf_node& conf)
00054     {
00055         m_on_device_change_slot =
00056             conf.child ("out_device").on_change.connect
00057             (boost::bind (&output_director_alsa::on_device_change, this, _1));
00058 
00059         return build_output (conf);
00060     };
00061 
00062     virtual graph::audio_async_output_ptr
00063     build_output (base::conf_node& conf)
00064     {
00065         auto device = conf.child ("out_device").get<std::string> ();
00066         m_output = io::new_buffered_async_output<
00067             graph::audio_const_range,
00068             io::alsa_output<sound::stereo16sc_range> >(device, 1024, 44100);
00069         return m_output;
00070     }
00071 
00072     virtual void do_stop (base::conf_node& conf)
00073     {
00074         m_on_device_change_slot.disconnect ();
00075         if (m_output)
00076             m_output.reset ();
00077     }
00078 
00079 public:
00080     void defaults (base::conf_node& conf)
00081     {
00082         conf.child ("out_device").def (
00083             std::string (PSYNTH_DEFAULT_ALSA_OUT_DEVICE));
00084     }
00085 };
00086 
00087 class output_director_alsa_factory : public output_director_factory
00088 {
00089 public:
00090     virtual const char* get_name()
00091     { return PSYNTH_DEFAULT_ALSA_NAME; }
00092     
00093     virtual output_director* create_output_director()
00094     { return new output_director_alsa; }
00095 };
00096 
00097 } /* namespace psynth */
00098 
00099 #endif /* PSYNTH_OUTPUT_DIRECTOR_ALSA_H */