//
// MATLAB Compiler: 2.2
// Date: Mon Apr 19 18:03:20 2004
// Arguments: "-B" "macro_default" "-O" "all" "-O" "fold_scalar_mxarrays:on"
// "-O" "fold_non_scalar_mxarrays:on" "-O" "optimize_integer_for_loops:on" "-O"
// "array_indexing:on" "-O" "optimize_conditionals:on" "-p" "-W" "main" "-L"
// "Cpp" "-t" "-T" "link:exe" "-h" "libmmfile.mlib" "-B" "sglcpp" "-p" "-W"
// "main" "-L" "Cpp" "-t" "-T" "link:exe" "-h" "libmmfile.mlib" "-W" "mainhg"
// "libmwsglm.mlib" "hier_community.m" "Default.m" "Stop_Simulation.m"
// "Call_SaveLoad_Fig.m" "SaveLoad_Fig.m" "SaveLoad.m" "Load_Population_Data.m"
// "Save_Population_Data.m" "Start_hier_community.m" "MyInline.m" "Find_Var.m"
// "isnumber.m" "Change_Number_of_Populations.m" "Switch_Populations.m"
// "Store_Parameters.m" 
//
#include "audio_private_playsnd.hpp"
#include "libmatlbm.hpp"
#include "libmmfile.hpp"

static mxChar _array1_[160] = { 'R', 'u', 'n', '-', 't', 'i', 'm', 'e', ' ',
                                'E', 'r', 'r', 'o', 'r', ':', ' ', 'F', 'i',
                                'l', 'e', ':', ' ', 'a', 'u', 'd', 'i', 'o',
                                '/', 'p', 'r', 'i', 'v', 'a', 't', 'e', '/',
                                'p', 'l', 'a', 'y', 's', 'n', 'd', ' ', 'L',
                                'i', 'n', 'e', ':', ' ', '1', ' ', 'C', 'o',
                                'l', 'u', 'm', 'n', ':', ' ', '1', ' ', 'T',
                                'h', 'e', ' ', 'f', 'u', 'n', 'c', 't', 'i',
                                'o', 'n', ' ', '"', 'a', 'u', 'd', 'i', 'o',
                                '/', 'p', 'r', 'i', 'v', 'a', 't', 'e', '/',
                                'p', 'l', 'a', 'y', 's', 'n', 'd', '"', ' ',
                                'w', 'a', 's', ' ', 'c', 'a', 'l', 'l', 'e',
                                'd', ' ', 'w', 'i', 't', 'h', ' ', 'm', 'o',
                                'r', 'e', ' ', 't', 'h', 'a', 'n', ' ', 't',
                                'h', 'e', ' ', 'd', 'e', 'c', 'l', 'a', 'r',
                                'e', 'd', ' ', 'n', 'u', 'm', 'b', 'e', 'r',
                                ' ', 'o', 'f', ' ', 'o', 'u', 't', 'p', 'u',
                                't', 's', ' ', '(', '0', ')', '.' };
static mwArray _mxarray0_ = mclInitializeString(160, _array1_);

static mxChar _array3_[159] = { 'R', 'u', 'n', '-', 't', 'i', 'm', 'e', ' ',
                                'E', 'r', 'r', 'o', 'r', ':', ' ', 'F', 'i',
                                'l', 'e', ':', ' ', 'a', 'u', 'd', 'i', 'o',
                                '/', 'p', 'r', 'i', 'v', 'a', 't', 'e', '/',
                                'p', 'l', 'a', 'y', 's', 'n', 'd', ' ', 'L',
                                'i', 'n', 'e', ':', ' ', '1', ' ', 'C', 'o',
                                'l', 'u', 'm', 'n', ':', ' ', '1', ' ', 'T',
                                'h', 'e', ' ', 'f', 'u', 'n', 'c', 't', 'i',
                                'o', 'n', ' ', '"', 'a', 'u', 'd', 'i', 'o',
                                '/', 'p', 'r', 'i', 'v', 'a', 't', 'e', '/',
                                'p', 'l', 'a', 'y', 's', 'n', 'd', '"', ' ',
                                'w', 'a', 's', ' ', 'c', 'a', 'l', 'l', 'e',
                                'd', ' ', 'w', 'i', 't', 'h', ' ', 'm', 'o',
                                'r', 'e', ' ', 't', 'h', 'a', 'n', ' ', 't',
                                'h', 'e', ' ', 'd', 'e', 'c', 'l', 'a', 'r',
                                'e', 'd', ' ', 'n', 'u', 'm', 'b', 'e', 'r',
                                ' ', 'o', 'f', ' ', 'i', 'n', 'p', 'u', 't',
                                's', ' ', '(', '3', ')', '.' };
static mwArray _mxarray2_ = mclInitializeString(159, _array3_);

static mxChar _array5_[8] = { 'h', 'o', 's', 't', 'n', 'a', 'm', 'e' };
static mwArray _mxarray4_ = mclInitializeString(8, _array5_);

static mxChar _array7_[8] = { 'u', 'n', 'a', 'm', 'e', ' ', '-', 'n' };
static mwArray _mxarray6_ = mclInitializeString(8, _array7_);
static mwArray _mxarray8_ = mclInitializeDouble(1.0);

static mxChar _array10_[4] = { 'H', 'O', 'S', 'T' };
static mwArray _mxarray9_ = mclInitializeString(4, _array10_);

static mxChar _array12_[7] = { 'D', 'I', 'S', 'P', 'L', 'A', 'Y' };
static mwArray _mxarray11_ = mclInitializeString(7, _array12_);

static mxChar _array14_[1] = { ':' };
static mwArray _mxarray13_ = mclInitializeString(1, _array14_);

static mxChar _array16_[4] = { 'u', 'n', 'i', 'x' };
static mwArray _mxarray15_ = mclInitializeString(4, _array16_);

static mxChar _array18_[10] = { '/', 'd', 'e', 'v', '/',
                                'a', 'u', 'd', 'i', 'o' };
static mwArray _mxarray17_ = mclInitializeString(10, _array18_);

static mxChar _array20_[2] = { 'w', 'b' };
static mwArray _mxarray19_ = mclInitializeString(2, _array20_);
static mwArray _mxarray21_ = mclInitializeDouble(-1.0);

static mxChar _array23_[53] = { 'A', 'u', 'd', 'i', 'o', ' ', 'c', 'a', 'p',
                                'a', 'b', 'i', 'l', 'i', 't', 'i', 'e', 's',
                                ' ', 'a', 'r', 'e', ' ', 'n', 'o', 't', ' ',
                                'a', 'v', 'a', 'i', 'l', 'a', 'b', 'l', 'e',
                                ' ', 'o', 'n', ' ', 't', 'h', 'i', 's', ' ',
                                'm', 'a', 'c', 'h', 'i', 'n', 'e', '.' };
static mwArray _mxarray22_ = mclInitializeString(53, _array23_);

static mxChar _array25_[5] = { 'u', 'c', 'h', 'a', 'r' };
static mwArray _mxarray24_ = mclInitializeString(5, _array25_);

static mxChar _array27_[7] = { '/', 't', 'm', 'p', '/', 'a', 'u' };
static mwArray _mxarray26_ = mclInitializeString(7, _array27_);

static mxChar _array29_[18] = { '%', '0', '2', '.', '0', 'f', '%', '0', '2',
                                '.', '0', 'f', '%', '0', '2', '.', '0', 'f' };
static mwArray _mxarray28_ = mclInitializeString(18, _array29_);
static mwArray _mxarray30_ = mclInitializeDouble(4.0);
static mwArray _mxarray31_ = mclInitializeDouble(6.0);

static mxChar _array33_[12] = { 'C', 'a', 'n', 'n', 'o', 't',
                                ' ', 'o', 'p', 'e', 'n', ' ' };
static mwArray _mxarray32_ = mclInitializeString(12, _array33_);

static mxChar _array35_[22] = { ' ', 'w', 'i', 't', 'h', ' ', 'w', 'r',
                                'i', 't', 'e', ' ', 'p', 'r', 'i', 'v',
                                'i', 'l', 'e', 'g', 'e', '.' };
static mwArray _mxarray34_ = mclInitializeString(22, _array35_);

static mxChar _array37_[4] = { 'r', 's', 'h', ' ' };
static mwArray _mxarray36_ = mclInitializeString(4, _array37_);

static mxChar _array39_[1] = { 'H' };
static mwArray _mxarray38_ = mclInitializeString(1, _array39_);

static mxChar _array41_[6] = { 'r', 'e', 'm', 's', 'h', ' ' };
static mwArray _mxarray40_ = mclInitializeString(6, _array41_);

static mxChar _array43_[5] = { '!', 'c', 'a', 't', ' ' };
static mwArray _mxarray42_ = mclInitializeString(5, _array43_);

static mxChar _array45_[3] = { ' ', '|', ' ' };
static mwArray _mxarray44_ = mclInitializeString(3, _array45_);

static mxChar _array47_[20] = { ' ', 0x0027, 'c', 'a', 't', ' ', '>',
                                ' ', '/', 'd', 'e', 'v', '/', 'a',
                                'u', 'd', 'i', 'o', 0x0027, ' ' };
static mwArray _mxarray46_ = mclInitializeString(20, _array47_);

void InitializeModule_audio_private_playsnd() {
}

void TerminateModule_audio_private_playsnd() {
}

static void Maudio_private_playsnd(mwArray y, mwArray fs, mwArray bits);

_mexLocalFunctionTable _local_function_table_audio_private_playsnd
  = { 0, (mexFunctionTableEntry *)NULL };

//
// The function "audio_private_playsnd" contains the normal interface for the
// "audio/private/playsnd" M-function from file
// "C:\MATLAB6p1\toolbox\matlab\audio\private\playsnd.m" (lines 1-55). This
// function processes any input arguments and passes them to the implementation
// version of the function, appearing above.
//
void audio_private_playsnd(mwArray y, mwArray fs, mwArray bits) {
    Maudio_private_playsnd(y, fs, bits);
}

//
// The function "mlxAudio_private_playsnd" contains the feval interface for the
// "audio/private/playsnd" M-function from file
// "C:\MATLAB6p1\toolbox\matlab\audio\private\playsnd.m" (lines 1-55). The
// feval function calls the implementation version of audio/private/playsnd
// through this function. This function processes any input arguments and
// passes them to the implementation version of the function, appearing above.
//
void mlxAudio_private_playsnd(int nlhs,
                              mxArray * plhs[],
                              int nrhs,
                              mxArray * prhs[]) {
    MW_BEGIN_MLX();
    {
        mwArray mprhs[3];
        int i;
        if (nlhs > 0) {
            error(_mxarray0_);
        }
        if (nrhs > 3) {
            error(_mxarray2_);
        }
        for (i = 0; i < 3 && i < nrhs; ++i) {
            mprhs[i] = mwArray(prhs[i], 0);
        }
        for (; i < 3; ++i) {
            mprhs[i].MakeDIN();
        }
        Maudio_private_playsnd(mprhs[0], mprhs[1], mprhs[2]);
    }
    MW_END_MLX();
}

//
// The function "Maudio_private_playsnd" is the implementation version of the
// "audio/private/playsnd" M-function from file
// "C:\MATLAB6p1\toolbox\matlab\audio\private\playsnd.m" (lines 1-55). It
// contains the actual compiled code for that M-function. It is a static
// function and must only be called from one of the interface functions,
// appearing below.
//
//
// function playsnd(y,fs,bits)
//
static void Maudio_private_playsnd(mwArray y, mwArray fs, mwArray bits) {
    mwLocalFunctionTable save_local_function_table_
      (&_local_function_table_audio_private_playsnd);
    mwArray rsh(mclGetUninitializedArray());
    mwArray cp(mclGetUninitializedArray());
    mwArray tmpfile(mclGetUninitializedArray());
    mwArray t(mclGetUninitializedArray());
    mwArray ans(mclGetUninitializedArray());
    mwArray fp(mclGetUninitializedArray());
    mwArray dspname(mclGetUninitializedArray());
    mwArray machinename(mclGetUninitializedArray());
    mwArray hostname(mclGetUninitializedArray());
    mwArray s(mclGetUninitializedArray());
    //
    // %PLAYSND Implementation for SOUND
    // %   PLAYSND(Y,FS,BITS)
    // 
    // %   IF a MEX file version exists, it is called; otherwise, this file is
    // %   used when a MEX file is not available for the platform.  Sound is
    // %   simply written to /dev/audio, either on the local machine, or on
    // %   the remote machine specified by the environment variable DISPLAY.
    // 
    // %   Copyright 1984-2001 The MathWorks, Inc. 
    // %   $Revision: 1.13 $  $Date: 2001/04/15 12:04:33 $
    // 
    // % Is audio device local or remote?
    // if isunix,
    //
    if (tobool(mwVe(isunix()))) {
        //
        // [s,hostname] = unix('hostname');
        //
        s = unix_func(&hostname, _mxarray4_);
        //
        // if isempty(hostname), % Be extra robust
        //
        if (tobool(mwVe(isempty(mwVv(hostname, "hostname"))))) {
            //
            // hostname = unix('uname -n');
            //
            hostname = unix_func(_mxarray6_);
        //
        // end
        //
        }
        //
        // machinename = hostname(1:end-1);
        //
        machinename
          = mclArrayRef(
              mwVsv(hostname, "hostname"),
              colon(
                _mxarray8_,
                end(mwVv(hostname, "hostname"), _mxarray8_, _mxarray8_)
                - _mxarray8_));
    //
    // else
    //
    } else {
        //
        // machinename = getenv('HOST');
        //
        machinename = getenv(_mxarray9_);
    //
    // end
    //
    }
    //
    // dspname = getenv('DISPLAY');
    //
    dspname = getenv(_mxarray11_);
    //
    // if ~isempty(dspname), dspname  = dspname(1:find(dspname == ':')-1); end
    //
    if (mclNotBool(mwVe(isempty(mwVv(dspname, "dspname"))))) {
        dspname
          = mclArrayRef(
              mwVsv(dspname, "dspname"),
              colon(
                _mxarray8_,
                mwVe(find(mwVv(dspname, "dspname") == _mxarray13_))
                - _mxarray8_));
    }
    //
    // 
    // if isempty(dspname) | isempty(machinename) | ...
    //
    {
        mwArray a_(mwVe(isempty(mwVv(dspname, "dspname"))));
        if (tobool(a_)) {
            a_ = 1;
        } else {
            a_ = a_ | mwVe(isempty(mwVv(machinename, "machinename")));
        }
        if (tobool(a_)) {
            a_ = 1;
        } else {
            a_
              = a_
                | mwVe(
                    strcmp(
                      mwVv(dspname, "dspname"),
                      mwVv(machinename, "machinename")));
        }
        if (tobool(a_)
            || tobool(
                 a_
                 | mwVe(
                     strcmp(
                       mwVe(lower(mwVv(dspname, "dspname"))),
                       _mxarray15_)))) {
            //
            // strcmp(dspname,machinename) | strcmp(lower(dspname),'unix');
            // % Audio device is (probably) on this machine.
            // fp = fopen('/dev/audio','wb');
            //
            fp = fopen(_mxarray17_, _mxarray19_);
            //
            // if fp == -1
            //
            if (mclEqBool(mwVv(fp, "fp"), _mxarray21_)) {
                //
                // disp('Audio capabilities are not available on this machine.')
                //
                disp(_mxarray22_);
                //
                // return;
                //
                goto return_;
            //
            // end
            //
            }
            //
            // fwrite(fp,lin2mu(y),'uchar');
            //
            ans.EqAns(
              fwrite(mwVv(fp, "fp"), mwVe(lin2mu(mwVa(y, "y"))), _mxarray24_));
            //
            // fclose(fp);
            //
            ans.EqAns(fclose(mwVv(fp, "fp")));
        //
        // else
        //
        } else {
            //
            // % Audio device is on a remote machine.
            // % Write to a temporary file, then use a shell escape
            // % to send the file to the remote /dev/audio.
            // t = clock;
            //
            t = clock_func();
            //
            // tmpfile = ['/tmp/au' sprintf('%02.0f%02.0f%02.0f', t(4:6))];
            //
            tmpfile
              = horzcat(
                  mwVarargin(
                    _mxarray26_,
                    mwVe(
                      sprintf(
                        _mxarray28_,
                        mwVarargin(
                          mwVe(
                            mclArrayRef(
                              mwVsv(t, "t"),
                              colon(_mxarray30_, _mxarray31_))))))));
            //
            // fp = fopen(tmpfile,'wb');
            //
            fp = fopen(mwVv(tmpfile, "tmpfile"), _mxarray19_);
            //
            // if fp==-1,
            //
            if (mclEqBool(mwVv(fp, "fp"), _mxarray21_)) {
                //
                // error(['Cannot open ' tmpfile ' with write privilege.'])
                //
                error(
                  horzcat(
                    mwVarargin(
                      _mxarray32_, mwVv(tmpfile, "tmpfile"), _mxarray34_)));
            //
            // end
            //
            }
            //
            // fwrite(fp,lin2mu(y),'uchar');
            //
            ans.EqAns(
              fwrite(mwVv(fp, "fp"), mwVe(lin2mu(mwVa(y, "y"))), _mxarray24_));
            //
            // fclose(fp);
            //
            ans.EqAns(fclose(mwVv(fp, "fp")));
            //
            // 
            // cp = computer;
            //
            cp = computer();
            //
            // rsh = 'rsh ';
            //
            rsh = _mxarray36_;
            //
            // if cp(1)=='H', rsh = 'remsh '; end
            //
            if (mclEqBool(
                  mwVe(mclIntArrayRef(mwVsv(cp, "cp"), 1)), _mxarray38_)) {
                rsh = _mxarray40_;
            }
            //
            // eval(['!cat ' tmpfile ' | ' rsh dspname ' ''cat > /dev/audio'' ']);
            //
            ans.EqAns(
              eval(
                mwAnsVarargout(),
                horzcat(
                  mwVarargin(
                    _mxarray42_,
                    mwVv(tmpfile, "tmpfile"),
                    _mxarray44_,
                    mwVv(rsh, "rsh"),
                    mwVv(dspname, "dspname"),
                    _mxarray46_))));
            //
            // delete(tmpfile);
            //
            delete_func(mwVarargin(mwVv(tmpfile, "tmpfile")));
        }
    //
    // end
    //
    }
    return_:;
}
