2001 lines · 47.3 KB · C++
RawDownload
/*----------------------------------------------------------------------------  ChucK Concurrent, On-the-fly Audio Programming Language    Compiler and Virtual Machine   Copyright (c) 2004 Ge Wang and Perry R. Cook.  All rights reserved.    http://chuck.stanford.edu/    http://chuck.cs.princeton.edu/   This program is free software; you can redistribute it and/or modify  it under the terms of the GNU General Public License as published by  the Free Software Foundation; either version 2 of the License, or  (at your option) any later version.   This program 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 General Public License for more details.   You should have received a copy of the GNU General Public License  along with this program; if not, write to the Free Software  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  U.S.A.-----------------------------------------------------------------------------*/ //-----------------------------------------------------------------------------// file: ulib_std.cpp// desc: standard class library//// author: Ge Wang (ge@ccrma.stanford.edu | gewang@cs.princeton.edu)// date: Spring 2004//-----------------------------------------------------------------------------#include "ulib_std.h"#include <stdlib.h>#include <time.h>#include <math.h>#include "util_buffers.h"#ifndef __DISABLE_PROMPTER__#include "util_console.h"#endif#include "util_math.h"#include "util_string.h"#ifndef __DISABLE_THREADS__#include "util_thread.h"#endif#include "chuck.h"#include "chuck_type.h"#include "chuck_compile.h"#include "chuck_instr.h" #if defined(__PLATFORM_WIN32__) #include <windows.h> int setenv( const char *n, const char *v, int i ){    return !SetEnvironmentVariable(n, v);} #else #include <unistd.h> #endif // for ConsoleInput and StringTokenizer#include <sstream>#include <iostream>#ifndef __DISABLE_THREADS__#include "util_thread.h"#endifusing namespace std;  #ifndef __DISABLE_KBHIT__// KBHitCK_DLL_CTOR( KBHit_ctor );CK_DLL_DTOR( KBHit_dtor );CK_DLL_MFUN( KBHit_on );CK_DLL_MFUN( KBHit_off );CK_DLL_MFUN( KBHit_state );CK_DLL_MFUN( KBHit_hit );CK_DLL_MFUN( KBHit_more );CK_DLL_MFUN( KBHit_getchar );CK_DLL_MFUN( KBHit_can_wait ); static t_CKUINT KBHit_offset_data = 0;#endif // __DISABLE_KBHIT__ #ifndef __DISABLE_PROMPTER__// Skot functionsCK_DLL_CTOR( Skot_ctor );CK_DLL_DTOR( Skot_dtor );CK_DLL_MFUN( Skot_prompt );CK_DLL_MFUN( Skot_prompt2 );CK_DLL_MFUN( Skot_more );CK_DLL_MFUN( Skot_getLine );CK_DLL_MFUN( Skot_can_wait ); static t_CKUINT Skot_offset_data = 0;#endif // __DISABLE_PROMPTER__ // StrTok functionsCK_DLL_CTOR( StrTok_ctor );CK_DLL_DTOR( StrTok_dtor );CK_DLL_MFUN( StrTok_set );CK_DLL_MFUN( StrTok_reset );CK_DLL_MFUN( StrTok_more );CK_DLL_MFUN( StrTok_next );CK_DLL_MFUN( StrTok_next2 );CK_DLL_MFUN( StrTok_get );CK_DLL_MFUN( StrTok_get2 );CK_DLL_MFUN( StrTok_size ); static t_CKUINT StrTok_offset_data = 0; #ifdef AJAY #include <fstream> // VCR functionsCK_DLL_CTOR( VCR_ctor );CK_DLL_MFUN( VCR_load );CK_DLL_MFUN( VCR_reset );CK_DLL_MFUN( VCR_seek );CK_DLL_MFUN( VCR_more );CK_DLL_MFUN( VCR_curr );CK_DLL_MFUN( VCR_next );CK_DLL_MFUN( VCR_pos );CK_DLL_MFUN( VCR_size );CK_DLL_MFUN( VCR_name ); static t_CKUINT VCR_offset_data = 0; // Cereal functionsCK_DLL_CTOR( Cereal_ctor );CK_DLL_MFUN( Cereal_open );CK_DLL_MFUN( Cereal_close );CK_DLL_MFUN( Cereal_send );CK_DLL_MFUN( Cereal_recv );CK_DLL_MFUN( Cereal_more ); static t_CKUINT Cereal_offset_data = 0; #endif    //-----------------------------------------------------------------------------// name: libstd_query()// desc: query entry point//-----------------------------------------------------------------------------DLL_QUERY libstd_query( Chuck_DL_Query * QUERY ){    // get global    Chuck_Env * env = QUERY->env();    // set name    QUERY->setname( QUERY, "Std" );     /*! \example    std.rand2f( 100.0, 1000.0 ) => stdout;    */     // register deprecate    type_engine_register_deprecate( env, "std", "Std" );     // begin class    QUERY->begin_class( QUERY, "Std", "Object" );    QUERY->doc_class( QUERY, "Std is a standard library in ChucK, which includes utility functions for random number generation, unit conversions, and absolute value." );     // add abs    QUERY->add_sfun( QUERY, abs_impl, "int", "abs" );    QUERY->add_arg( QUERY, "int", "value" );    QUERY->doc_func( QUERY, "Return absolute value of integer. " );        // add fabs    QUERY->add_sfun( QUERY, fabs_impl, "float", "fabs" );    QUERY->add_arg( QUERY, "float", "value" );    QUERY->doc_func( QUERY, "Return absolute value of float. " );     // add rand    QUERY->add_sfun( QUERY, rand_impl, "int", "rand"); //! return int between 0 and RAND_MAX    QUERY->doc_func( QUERY, "Generates random integer between 0 and Std.RAND_MAX. (NOTE: soon-to-be-deprecated; use Math.random2())." );     // add rand2    QUERY->add_sfun( QUERY, rand2_impl, "int", "rand2" ); //! integer between [min,max]    QUERY->add_arg( QUERY, "int", "min" );     QUERY->add_arg( QUERY, "int", "max" );    QUERY->doc_func( QUERY, "Generates random integer in range [min, max]. (NOTE: soon-to-be-deprecated; use Math.random2())." );     // add rand    QUERY->add_sfun( QUERY, randf_impl, "float", "randf" ); //! rand between -1.0,1.0    QUERY->doc_func( QUERY, "Generates random floating point number in the range [-1, 1]. (Note: soon-to-be-deprecated; use Math.randomf())" );     // add rand2    QUERY->add_sfun( QUERY, rand2f_impl, "float", "rand2f" ); //! rand between min and max    QUERY->add_arg( QUERY, "float", "min" );    QUERY->add_arg( QUERY, "float", "max" );    QUERY->doc_func( QUERY, "Generates random floating point number in the range [min, max]. (NOTE: soon-to-be-deprecated; use Math.random2f())" );     // add srand    QUERY->add_sfun( QUERY, srand_impl, "void", "srand" );    QUERY->add_arg( QUERY, "int", "seed" );    QUERY->doc_func( QUERY, "Provide a seed to the random function. Different seeds will generate very different sequences of random numbers even if the seeds are close together. "                     "Alternatively, a sequence of random numbers can be repeated by setting the same seed. " );     // add sgn    QUERY->add_sfun( QUERY, sgn_impl, "float", "sgn" ); //! return sign of value (-1, 0, 1)    QUERY->add_arg( QUERY, "float", "value" );    QUERY->doc_func( QUERY, "Computes the sign of the input as -1.0 (negative), 0 (zero), or 1.0 (positive)." );     // add system    //! see \example std.ck    QUERY->add_sfun( QUERY, system_impl, "int", "system" ); //! issue system command    QUERY->add_arg( QUERY, "string", "cmd" );    QUERY->doc_func( QUERY, "Pass a command to be executed in the shell (requires --caution-to-the-wind command-line flag)." );     // add atoi    QUERY->add_sfun( QUERY, atoi_impl, "int", "atoi" ); //! string to integer    QUERY->add_arg( QUERY, "string", "value" );    QUERY->doc_func( QUERY, "Converts ascii (string) to integer (int)." );     // add atof    QUERY->add_sfun( QUERY, atof_impl, "float", "atof" ); //! string to float    QUERY->add_arg( QUERY, "string", "value" );    QUERY->doc_func( QUERY, "Converts ascii (string) to floating point value (float)." );     // add itoa    QUERY->add_sfun( QUERY, itoa_impl, "string", "itoa" ); //! int to string    QUERY->add_arg( QUERY, "int", "i" );    QUERY->doc_func( QUERY, "Converts integer (int) to ascii (string)." );     // add ftoa    QUERY->add_sfun( QUERY, ftoa_impl, "string", "ftoa" ); //! float to string    QUERY->add_arg( QUERY, "float", "f" );    QUERY->add_arg( QUERY, "int", "precision" );    QUERY->doc_func( QUERY, "Converts floating point value (float) to ascii (string) with specified precision (number of decimal digits)." );     // add ftoi    QUERY->add_sfun( QUERY, ftoi_impl, "int", "ftoi" ); //! float to int    QUERY->add_arg( QUERY, "float", "f" );    QUERY->doc_func( QUERY, "Convert float to int. " );     // add getenv    QUERY->add_sfun( QUERY, getenv_impl, "string", "getenv" ); //! fetch environment variable    QUERY->add_arg( QUERY, "string", "value" );    QUERY->doc_func( QUERY, "Returns the value of an environment variable, such as of PATH." );     // add setenv    QUERY->add_sfun( QUERY, setenv_impl, "int", "setenv" ); //! set environment variable    QUERY->add_arg( QUERY, "string", "key" );    QUERY->add_arg( QUERY, "string", "value" );    QUERY->doc_func( QUERY, "Set value of environment variable named key to value." );     // add mtof    //! see \example mand-o-matic.ck    QUERY->add_sfun( QUERY, mtof_impl, "float", "mtof" ); //! midi note to frequency    QUERY->add_arg( QUERY, "float", "value" );    QUERY->doc_func( QUERY, "Converts a MIDI note number to frequency (Hz). Note the input value is of type float (supports fractional note number)." );     // add ftom    QUERY->add_sfun( QUERY, ftom_impl, "float", "ftom" ); //! frequency to midi note    QUERY->add_arg( QUERY, "float", "value" );    QUERY->doc_func( QUERY, "Converts frequency (Hz) to MIDI note number space." );     // add powtodb    QUERY->add_sfun( QUERY, powtodb_impl, "float", "powtodb" ); //! linear power to decibel     QUERY->add_arg( QUERY, "float", "value" );    QUERY->doc_func( QUERY, "Converts signal power ratio to decibels (dB)." );     // add rmstodb    QUERY->add_sfun( QUERY, rmstodb_impl, "float", "rmstodb" ); //! rms to decibel    QUERY->add_arg( QUERY, "float", "value" );    QUERY->doc_func( QUERY, "Converts rms to decibels (dB)." );     // add dbtopow    QUERY->add_sfun( QUERY, dbtopow_impl, "float", "dbtopow" ); //! decibel to linear    QUERY->add_arg( QUERY, "float", "value" );    QUERY->doc_func( QUERY, "Converts decibels (dB) to signal power ratio." );     // add dbtorms    QUERY->add_sfun( QUERY, dbtorms_impl, "float", "dbtorms" ); //! decibel to rms    QUERY->add_arg( QUERY, "float", "value" );    QUERY->doc_func( QUERY, "Convert " );     // add dbtolin    QUERY->add_sfun( QUERY, dbtolin_impl, "float", "dbtolin" ); //! decibel to linear    QUERY->add_arg( QUERY, "float", "value" );    QUERY->doc_func( QUERY, "" );     // add lintodb    QUERY->add_sfun( QUERY, lintodb_impl, "float", "lintodb" ); //! linear to decibel    QUERY->add_arg( QUERY, "float", "value" );    QUERY->doc_func( QUERY, "" );     // add clamp    QUERY->add_sfun( QUERY, clamp_impl, "int", "clamp" ); //! clamp to range (int)    QUERY->add_arg( QUERY, "int", "value" );    QUERY->add_arg( QUERY, "int", "min" );    QUERY->add_arg( QUERY, "int", "max" );    QUERY->doc_func( QUERY, "" );     // add clampf    QUERY->add_sfun( QUERY, clampf_impl, "float", "clampf" ); //! clamp to range (float)    QUERY->add_arg( QUERY, "float", "value" );    QUERY->add_arg( QUERY, "float", "min" );    QUERY->add_arg( QUERY, "float", "max" );    QUERY->doc_func( QUERY, "" );     // add scalef    QUERY->add_sfun( QUERY, scalef_impl, "float", "scalef" ); //! scale from source range to dest range (float)    QUERY->add_arg( QUERY, "float", "value" );    QUERY->add_arg( QUERY, "float", "srcmin" );    QUERY->add_arg( QUERY, "float", "srcmax" );    QUERY->add_arg( QUERY, "float", "dstmin" );    QUERY->add_arg( QUERY, "float", "dstmax" );    QUERY->doc_func( QUERY, "" );     // finish class    QUERY->end_class( QUERY );     // seed the rand    srand( time( NULL ) );     Chuck_DL_Func * func = NULL;    #ifndef __DISABLE_KBHIT__    // KBHit    // begin class (KBHit)    if( !type_engine_import_class_begin( env, "KBHit", "Event",                                         env->global(), KBHit_ctor,                                         KBHit_dtor ) )        return FALSE;     // add member variable    KBHit_offset_data = type_engine_import_mvar( env, "int", "@KBHit_data", FALSE );    if( KBHit_offset_data == CK_INVALID_OFFSET ) goto error;     // add on()    func = make_new_mfun( "void", "on", KBHit_on );    if( !type_engine_import_mfun( env, func ) ) goto error;     // add off()    func = make_new_mfun( "void", "off", KBHit_off );    if( !type_engine_import_mfun( env, func ) ) goto error;     // add state()    func = make_new_mfun( "void", "state", KBHit_state );    if( !type_engine_import_mfun( env, func ) ) goto error;     // add hit()    func = make_new_mfun( "Event", "hit", KBHit_hit );    if( !type_engine_import_mfun( env, func ) ) goto error;     // add more()    func = make_new_mfun( "int", "more", KBHit_more );    if( !type_engine_import_mfun( env, func ) ) goto error;     // add getchar()    func = make_new_mfun( "int", "getchar", KBHit_getchar );    if( !type_engine_import_mfun( env, func ) ) goto error;     // add can_wait()    func = make_new_mfun( "int", "can_wait", KBHit_can_wait );    if( !type_engine_import_mfun( env, func ) ) goto error;     // end class    type_engine_import_class_end( env );     // start it    KBHitManager::init();#endif // __DISABLE_KBHIT__      // register deprecate    type_engine_register_deprecate( env, "Skot", "ConsoleInput" ); #ifndef __DISABLE_PROMPTER__    // begin class (Skot)    if( !type_engine_import_class_begin( env, "ConsoleInput", "Event",                                         env->global(), Skot_ctor,                                         Skot_dtor ) )        return FALSE;     // add member variable    Skot_offset_data = type_engine_import_mvar( env, "int", "@Skot_data", FALSE );    if( Skot_offset_data == CK_INVALID_OFFSET ) goto error;     // add prompt()    func = make_new_mfun( "Event", "prompt", Skot_prompt );    if( !type_engine_import_mfun( env, func ) ) goto error;     // add prompt()    func = make_new_mfun( "Event", "prompt", Skot_prompt2 );    func->add_arg( "string", "what" );    if( !type_engine_import_mfun( env, func ) ) goto error;     // add ready()    func = make_new_mfun( "int", "more", Skot_more );    if( !type_engine_import_mfun( env, func ) ) goto error;     // add getString()    func = make_new_mfun( "string", "getLine", Skot_getLine );    if( !type_engine_import_mfun( env, func ) ) goto error;     // add can_wait()    func = make_new_mfun( "int", "can_wait", Skot_can_wait );    if( !type_engine_import_mfun( env, func ) ) goto error;     // end class    type_engine_import_class_end( env );#endif // __DISABLE_PROMPTER__      // register deprecate    type_engine_register_deprecate( env, "PRC", "StringTokenizer" );     // begin class (StrTok)    if( !type_engine_import_class_begin( env, "StringTokenizer", "Object",                                         env->global(), StrTok_ctor,                                         StrTok_dtor ) )        return FALSE;     // add member variable    StrTok_offset_data = type_engine_import_mvar( env, "int", "@StrTok_data", FALSE );    if( StrTok_offset_data == CK_INVALID_OFFSET ) goto error;     // add set()    func = make_new_mfun( "void", "set", StrTok_set );    func->add_arg( "string", "line" );    if( !type_engine_import_mfun( env, func ) ) goto error;     // add reset()    func = make_new_mfun( "void", "reset", StrTok_reset );    if( !type_engine_import_mfun( env, func ) ) goto error;     // add more()    func = make_new_mfun( "int", "more", StrTok_more );    if( !type_engine_import_mfun( env, func ) ) goto error;     // add next()    func = make_new_mfun( "string", "next", StrTok_next );    if( !type_engine_import_mfun( env, func ) ) goto error;     // add get()    func = make_new_mfun( "string", "next", StrTok_next2 );    func->add_arg( "string", "out" );    if( !type_engine_import_mfun( env, func ) ) goto error;     // add get()    func = make_new_mfun( "string", "get", StrTok_get );    func->add_arg( "int", "index" );    if( !type_engine_import_mfun( env, func ) ) goto error;     // add get()    func = make_new_mfun( "string", "get", StrTok_get2 );    func->add_arg( "int", "index" );    func->add_arg( "string", "out" );    if( !type_engine_import_mfun( env, func ) ) goto error;     // add size()    func = make_new_mfun( "int", "size", StrTok_size );    if( !type_engine_import_mfun( env, func ) ) goto error;     // end class    type_engine_import_class_end( env );  #ifdef AJAY     // begin class    // init base class    if( !type_engine_import_class_begin( env, "VCR", "Object",                                         env->global(), VCR_ctor ) )        return FALSE;     // add member variable    VCR_offset_data = type_engine_import_mvar( env, "int", "@me", FALSE );    if( VCR_offset_data == CK_INVALID_OFFSET ) goto error;     // add load()    func = make_new_mfun( "int", "load", VCR_load );    func->add_arg( "string", "filename" );    func->add_arg( "int", "column" );    if( !type_engine_import_mfun( env, func ) ) goto error;     // add reset()    func = make_new_mfun( "int", "reset", VCR_reset );    if( !type_engine_import_mfun( env, func ) ) goto error;     // add seek()    func = make_new_mfun( "int", "seek", VCR_seek );    func->add_arg( "int", "where" );    if( !type_engine_import_mfun( env, func ) ) goto error;     // add more()    func = make_new_mfun( "int", "more", VCR_more );    if( !type_engine_import_mfun( env, func ) ) goto error;     // add curr()

Lines 1–500 of 2001.

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t