<commit_before>/**
 * Copyright (C) 2009-2010 Michael 'Mickey' Lauer <<EMAIL>>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.

 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.

 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA
 *
 */

using Gee;

const string FSO_ALSA_CONF_PATH = "/etc/freesmartphone/alsa/default.conf";
const string FSO_ALSA_DATA_PATH = "/etc/freesmartphone/alsa/default/";

namespace Router
{

/**
 * Alsa Scenario Router
 **/
class LibAlsa : FsoDevice.BaseAudioRouter
{
    private const string MODULE_NAME = "fsodevice.router_alsa";

    private FsoFramework.SoundDevice device;
    private HashMap<string,FsoFramework.BunchOfMixerControls> allscenarios;
    private string currentscenario;
    private GLib.Queue<string> scenarios;

    construct
    {
        initScenarios();
        if ( currentscenario != "unknown" )
        {
            device.setAllMixerControls( allscenarios[currentscenario].controls );
        }
    }

    private void addScenario( string scenario, File file )
    {
        FsoFramework.MixerControl[] controls = {};

        try
        {
            // Open file for reading and wrap returned FileInputStream into a
            // DataInputStream, so we can read line by line
            var in_stream = new DataInputStream( file.read( null ) );
            string line;
            // Read lines until end of file (null) is reached
            while ( ( line = in_stream.read_line( null, null ) ) != null )
            {
                var control = device.controlForString( line );
                controls += control;
            }
#if DEBUG
            debug( "Scenario %s successfully read from file %s".printf( scenario, file.get_path() ) );
#endif
            var bunch = new FsoFramework.BunchOfMixerControls( controls );
            allscenarios[scenario] = bunch;
        }
        catch ( IOError e )
        {
            FsoFramework.theLogger.warning( "%s".printf( e.message ) );
        }
    }

    private void initScenarios()
    {
        scenarios = new GLib.Queue<string>();

        allscenarios = new HashMap<string,FsoFramework.BunchOfMixerControls>( str_hash, str_equal );
        currentscenario = "unknown";

        // init scenarios
        FsoFramework.SmartKeyFile alsaconf = new FsoFramework.SmartKeyFile();
        if ( alsaconf.loadFromFile( FSO_ALSA_CONF_PATH ) )
        {
            device = FsoFramework.SoundDevice.create( alsaconf.stringValue( "alsa", "cardname", "default" ) );
            var defaultscenario = alsaconf.stringValue( "alsa", "default_scenario", "stereoout" );

            var sections = alsaconf.sectionsWithPrefix( "scenario." );
            foreach ( var section in sections )
            {
                var scenario = section.split( "." )[1];
                if ( scenario != "" )
                {
                    FsoFramework.theLogger.debug( "Found scenario '%s'".printf( scenario ) );

                    var file = File.new_for_path( Path.build_filename( FSO_ALSA_DATA_PATH, scenario ) );
                    if ( !file.query_exists(null) )
                    {
                        FsoFramework.theLogger.warning( "Scenario file %s doesn't exist. Ignoring.".printf( file.get_path() ) );
                    }<commit_msg>fsodeviced: router_alsa: fail gracefully, if soundcard not existing; thanks JaMa<commit_after>/**
 * Copyright (C) 2009-2010 Michael 'Mickey' Lauer <<EMAIL>>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.

 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.

 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA
 *
 */

using Gee;

const string FSO_ALSA_CONF_PATH = "/etc/freesmartphone/alsa/default.conf";
const string FSO_ALSA_DATA_PATH = "/etc/freesmartphone/alsa/default/";

namespace Router
{

/**
 * Alsa Scenario Router
 **/
class LibAlsa : FsoDevice.BaseAudioRouter
{
    private const string MODULE_NAME = "fsodevice.router_alsa";

    private FsoFramework.SoundDevice device;
    private HashMap<string,FsoFramework.BunchOfMixerControls> allscenarios;
    private string currentscenario;
    private GLib.Queue<string> scenarios;

    construct
    {
        initScenarios();
        if ( currentscenario != "unknown" )
        {
            device.setAllMixerControls( allscenarios[currentscenario].controls );
        }
    }

    private void addScenario( string scenario, File file )
    {
        FsoFramework.MixerControl[] controls = {};

        try
        {
            // Open file for reading and wrap returned FileInputStream into a
            // DataInputStream, so we can read line by line
            var in_stream = new DataInputStream( file.read( null ) );
            string line;
            // Read lines until end of file (null) is reached
            while ( ( line = in_stream.read_line( null, null ) ) != null )
            {
                var control = device.controlForString( line );
                controls += control;
            }
#if DEBUG
            debug( "Scenario %s successfully read from file %s".printf( scenario, file.get_path() ) );
#endif
            var bunch = new FsoFramework.BunchOfMixerControls( controls );
            allscenarios[scenario] = bunch;
        }
        catch ( IOError e )
        {
            FsoFramework.theLogger.warning( "%s".printf( e.message ) );
        }
    }

    private void initScenarios()
    {
        scenarios = new GLib.Queue<string>();
        allscenarios = new HashMap<string,FsoFramework.BunchOfMixerControls>( str_hash, str_equal );
        currentscenario = "unknown";

        // init scenarios
        FsoFramework.SmartKeyFile alsaconf = new FsoFramework.SmartKeyFile();
        if ( alsaconf.loadFromFile( FSO_ALSA_CONF_PATH ) )
        {
            try
            {
                device = FsoFramework.SoundDevice.create( alsaconf.stringValue( "alsa", "cardname", "default" ) );
            }
            catch ( FsoFramework.SoundError e )
            {
                FsoFramework.theLogger.warning( @"Sound card problem: $(e.message)" );
                return;
            }
            var defaultscenario = alsaconf.stringValue( "alsa", "default_scenario", "stereoout" );

            var sections = alsaconf.sectionsWithPrefix( "scenario." );
            foreach ( var section in sections )
            {
                var scenario = section.split( "." )[1];
                if ( scenario != "" )
                {
                    FsoFramework.theLogger.debug( "Found scenario '%s'".printf( scenario ) );

                    var file = File.new_for_path( Path.build_filename( FSO_ALSA_DATA_PATH, scenario ) );
                    if ( !file.query_exists(null) )
                    {
                        FsoFramework.theLogger.warning( @"Scenario file $(file.get_path()) doesn't exist. Ignoring." );
                    }
{'fraction_non_alphanumeric': 0.09176264396594892, 'fraction_numerical': 0.005508262393590386, 'mean_word_length': 2.286301933360757, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7549682', 'n_tokens_mistral': 2217, 'n_tokens_neox': 2118, 'n_words': 656}