//
// 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 "sound.hpp"
#include "audio_private_playsnd.hpp"
#include "libmatlbm.hpp"

static mxChar _array1_[128] = { 'R', 'u', 'n', '-', 't', 'i', 'm', 'e', ' ',
                                'E', 'r', 'r', 'o', 'r', ':', ' ', 'F', 'i',
                                'l', 'e', ':', ' ', 's', 'o', 'u', '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', ' ', '"', 's', 'o', 'u',
                                '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(128, _array1_);

static mxChar _array3_[127] = { 'R', 'u', 'n', '-', 't', 'i', 'm', 'e', ' ',
                                'E', 'r', 'r', 'o', 'r', ':', ' ', 'F', 'i',
                                'l', 'e', ':', ' ', 's', 'o', 'u', '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', ' ', '"', 's', 'o', 'u',
                                '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(127, _array3_);

static mxChar _array5_[27] = { 'N', 'o', 't', ' ', 'e', 'n', 'o', 'u', 'g',
                               'h', ' ', 'i', 'n', 'p', 'u', 't', ' ', 'a',
                               'r', 'g', 'u', 'm', 'e', 'n', 't', 's', '.' };
static mwArray _mxarray4_ = mclInitializeString(27, _array5_);
static mwArray _mxarray6_ = mclInitializeDouble(8192.0);
static mwArray _mxarray7_ = mclInitializeDouble(16.0);
static mwArray _mxarray8_ = mclInitializeDouble(-1.0);
static mwArray _mxarray9_ = mclInitializeDouble(1.0);
static mwArray _mxarray10_ = mclInitializeDouble(2.0);

static mxChar _array12_[25] = { 'R', 'e', 'q', 'u', 'i', 'r', 'e', 's', ' ',
                                '2', '-', 'D', ' ', 'v', 'a', 'l', 'u', 'e',
                                's', ' ', 'o', 'n', 'l', 'y', '.' };
static mwArray _mxarray11_ = mclInitializeString(25, _array12_);

void InitializeModule_sound() {
}

void TerminateModule_sound() {
}

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

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

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

//
// The function "mlxSound" contains the feval interface for the "sound"
// M-function from file "C:\MATLAB6p1\toolbox\matlab\audio\sound.m" (lines
// 1-37). The feval function calls the implementation version of sound through
// this function. This function processes any input arguments and passes them
// to the implementation version of the function, appearing above.
//
void mlxSound(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();
        }
        Msound(mprhs[0], mprhs[1], mprhs[2]);
    }
    MW_END_MLX();
}

//
// The function "Msound" is the implementation version of the "sound"
// M-function from file "C:\MATLAB6p1\toolbox\matlab\audio\sound.m" (lines
// 1-37). 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 sound(y,fs,bits)
//
static void Msound(mwArray y, mwArray fs, mwArray bits) {
    mwLocalFunctionTable save_local_function_table_
      (&_local_function_table_sound);
    int nargin_(nargin(3, mwVarargin(y, fs, bits)));
    mwArray ans(mclGetUninitializedArray());
    //
    // %SOUND Play vector as sound.
    // %   SOUND(Y,FS) sends the signal in vector Y (with sample frequency
    // %   FS) out to the speaker on platforms that support sound. Values in
    // %   Y are assumed to be in the range -1.0 <= y <= 1.0. Values outside
    // %   that range are clipped.  Stereo sounds are played, on platforms
    // %   that support it, when Y is an N-by-2 matrix.
    // %
    // %   SOUND(Y) plays the sound at the default sample rate of 8192 Hz.
    // %
    // %   SOUND(Y,FS,BITS) plays the sound using BITS bits/sample if
    // %   possible.  Most platforms support BITS=8 or 16.
    // %
    // %   Example:
    // %     load handel
    // %     sound(y,Fs)
    // %   You should hear a snippet of Handel's Hallelujah Chorus.
    // %
    // %   See also SOUNDSC, WAVPLAY.
    // 
    // %   Copyright 1984-2001 The MathWorks, Inc. 
    // %   $Revision: 5.22 $  $Date: 2001/04/06 19:05:48 $
    // 
    // if nargin<1, error('Not enough input arguments.'); end
    //
    if (nargin_ < 1) {
        error(_mxarray4_);
    }
    //
    // if nargin<2, fs = 8192; end
    //
    if (nargin_ < 2) {
        fs = _mxarray6_;
    }
    //
    // if nargin<3, bits = 16; end
    //
    if (nargin_ < 3) {
        bits = _mxarray7_;
    }
    //
    // 
    // % Make sure y is in the range +/- 1
    // y = max(-1,min(y,1));
    //
    y = max(_mxarray8_, mwVe(min(mwVa(y, "y"), _mxarray9_)));
    //
    // 
    // % Make sure that there's one column
    // % per channel.
    // if ndims(y)>2, error('Requires 2-D values only.'); end
    //
    if (mclGtBool(mwVe(ndims(mwVa(y, "y"))), _mxarray10_)) {
        error(_mxarray11_);
    }
    //
    // if size(y,1)==1, y = y.'; end
    //
    if (mclEqBool(
          mwVe(size(mwValueVarargout(), mwVa(y, "y"), _mxarray9_)),
          _mxarray9_)) {
        y = transpose(mwVa(y, "y"));
    }
    //
    // 
    // playsnd(y,fs,bits);
    //
    audio_private_playsnd(mwVa(y, "y"), mwVa(fs, "fs"), mwVa(bits, "bits"));
}
