2044 lines · 51.4 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 "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" #include <stdlib.h>#include <time.h>#include <math.h> #ifdef __PLATFORM_WIN32__  #ifndef __CHUNREAL_ENGINE__    #include <windows.h>  #else    // 1.5.0.0 (ge) | #chunreal    // unreal engine on windows disallows including windows.h    #include "Windows/MinWindows.h"  #endif // #ifndef __CHUNREAL_ENGINE__int setenv( const char *n, const char *v, int i ){  #ifndef __CHUNREAL_ENGINE__    return !SetEnvironmentVariable(n, v);  #else    // #chunreal explicitly call ASCII version    return !SetEnvironmentVariableA(n, v);  #endif}#else  #include <unistd.h>#endif // #ifdef __PLATFORM_WIN32__ // 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  // variable to random maxstatic t_CKINT g_randMax = CK_RANDOM_MAX;  //-----------------------------------------------------------------------------// 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, "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 max    QUERY->doc_func( QUERY, "generate a random integer between 0 and Std.RAND_MAX. (NOTE: this is deprecated; use Math.random())." );     // 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, "Generate a random integer in range [min, max]. (NOTE: this is deprecated; use Math.random2())." );     // add rand    QUERY->add_sfun( QUERY, randf_impl, "float", "randf" ); //! rand between -1.0,1.0    QUERY->doc_func( QUERY, "Generate random floating point number in the range [-1, 1]. (Note: this is 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, "Generate random floating point number in the range [min, max]. (NOTE: this is deprecated; use Math.random2f())" );     // add srand    QUERY->add_sfun( QUERY, srand_impl, "void", "srand" );    QUERY->add_arg( QUERY, "int", "seed" );    QUERY->doc_func( QUERY, "seed the random number generator. Different seeds will likely generate 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. (NOTE: this is deprecated; use Math.srandom())" );     // random max    QUERY->add_svar( QUERY, "int", "RAND_MAX", TRUE, &g_randMax );    QUERY->doc_var( QUERY, "The largest possible value returned by Std.rand()." );     // 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, "get sign of value as -1 (negative), 0, or 1 (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 flag to be set)." );     // add atoi    QUERY->add_sfun( QUERY, atoi_impl, "int", "atoi" ); //! string to integer    QUERY->add_arg( QUERY, "string", "value" );    QUERY->doc_func( QUERY, "convert 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 integer. " );     // add getenv    QUERY->add_sfun( QUERY, getenv_impl, "string", "getenv" ); //! fetch environment variable    QUERY->add_arg( QUERY, "string", "value" );    QUERY->doc_func( QUERY, "get the value of an environment variable (e.g., 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 the value of environment variable named 'key'" );     // 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, "Convert a MIDI note number to frequency (Hz). Note the input value is of type float (supports fractional note number). For reference, MIDI note number 60 is Middle C; each whole number is one semitone." );     // add ftom    QUERY->add_sfun( QUERY, ftom_impl, "float", "ftom" ); //! frequency to midi note    QUERY->add_arg( QUERY, "float", "value" );    QUERY->doc_func( QUERY, "Convert 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, "Convert 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, "Convert 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, "Convert 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 decibels (dB) to rms." );     // add dbtolin    QUERY->add_sfun( QUERY, dbtolin_impl, "float", "dbtolin" ); //! decibel to linear    QUERY->add_arg( QUERY, "float", "value" );    QUERY->doc_func( QUERY, "Convert decibels (dB) to linear amplitude." );     // add lintodb    QUERY->add_sfun( QUERY, lintodb_impl, "float", "lintodb" ); //! linear to decibel    QUERY->add_arg( QUERY, "float", "value" );    QUERY->doc_func( QUERY, "Convert linear amplitude to decibels (dB).");     // 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, "Clamp integer to range [min, max]." );     // 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, "Clamp float to range [min, max]." );     // 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, "Scale a float from source range to destination range." );     // finish class    QUERY->end_class( QUERY );     // seed rand()    // (NOTE: ck_srandom(time(NULL)) is invoked in ulib_math.cpp)    // srand( (unsigned)time( NULL ) );     Chuck_DL_Func * func = NULL; #ifndef __DISABLE_KBHIT__    // doc string    string doc = "KBHit (terminal only) is a simple mechanism for capturing keyboard input; for a more flexible mechanism, see HID. (On Linux, KBHit does not require granting device permissions; it works out of the box.)";     // begin class (KBHit)    if( !type_engine_import_class_begin( env, "KBHit", "Event",                                         env->global(), KBHit_ctor,                                         KBHit_dtor, doc.c_str() ) )        return FALSE;     // add examples    if( !type_engine_import_add_ex( env, "hid/kbhit/kbhit.ck" ) ) goto error;    if( !type_engine_import_add_ex( env, "hid/kbhit/kbhit2.ck" ) ) goto error;    if( !type_engine_import_add_ex( env, "hid/kbhit/clix.ck" ) ) goto error;    if( !type_engine_import_add_ex( env, "hid/kbhit/clix2.ck" ) ) goto error;    if( !type_engine_import_add_ex( env, "hid/kbhit/clix3.ck" ) ) goto error;     // 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 );    func->doc = "Enable the KBHit.";    if( !type_engine_import_mfun( env, func ) ) goto error;     // add off()    func = make_new_mfun( "void", "off", KBHit_off );    func->doc = "Disable the KBHit.";    if( !type_engine_import_mfun( env, func ) ) goto error;     // add state()    func = make_new_mfun( "void", "state", KBHit_state );    func->doc = "Get whether the KBHit is currently enabled.";    if( !type_engine_import_mfun( env, func ) ) goto error;     // add hit()    func = make_new_mfun( "Event", "hit", KBHit_hit );    func->doc = "Return itself as an Event to wait on; this is largely unnecessary as the KBHit instance can be directly => to 'now'.";    if( !type_engine_import_mfun( env, func ) ) goto error;     // add more()    func = make_new_mfun( "int", "more", KBHit_more );    func->doc = "Return whether there are unprocessed KBHit events (e.g., if a user presses multiple keys at once).";    if( !type_engine_import_mfun( env, func ) ) goto error;     // add getchar()    func = make_new_mfun( "int", "getchar", KBHit_getchar );    func->doc = "Get the ASCII value of the last keyboard press.";    if( !type_engine_import_mfun( env, func ) ) goto error;     // add can_wait()    func = make_new_mfun( "int", "can_wait", KBHit_can_wait );    func->doc = "(internal) used by virtual machine for synthronization.";    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,                                         "(Terminal only) a utility for prompting user input on the command line." ) )        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 );    func->doc = "Return an Event to wait on.";    if( !type_engine_import_mfun( env, func ) ) goto error;     // add prompt()    func = make_new_mfun( "Event", "prompt", Skot_prompt2 );    func->add_arg( "string", "what" );    func->doc = "Print a prompt text and return an Event to wait on.";    if( !type_engine_import_mfun( env, func ) ) goto error;     // add more()    func = make_new_mfun( "int", "more", Skot_more );    func->doc = "Return whether there is more input to read.";    if( !type_engine_import_mfun( env, func ) ) goto error;     // add getString()    func = make_new_mfun( "string", "getLine", Skot_getLine );    func->doc = "Return the next line of input as a string.";    if( !type_engine_import_mfun( env, func ) ) goto error;     // add can_wait()    func = make_new_mfun( "int", "can_wait", Skot_can_wait );    func->doc = "(internal) used by virtual machine for synthronization.";    if( !type_engine_import_mfun( env, func ) ) goto error;     // add examples    if( !type_engine_import_add_ex( env, "string/readline.ck" ) ) goto error;    if( !type_engine_import_add_ex( env, "ai/word2vec/word2vec-prompt.ck" ) ) goto error;    if( !type_engine_import_add_ex( env, "ai/word2vec/poem-ungenerate.ck" ) ) 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,                                         "Break a string into tokens. This uses whitespace as the delimiter." ) )        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" );    func->doc = "Set the string to be tokenized.";    if( !type_engine_import_mfun( env, func ) ) goto error;     // add reset()    func = make_new_mfun( "void", "reset", StrTok_reset );    func->doc = "Reset token iteration back to the beginning of the set string.";    if( !type_engine_import_mfun( env, func ) ) goto error;     // add more()    func = make_new_mfun( "int", "more", StrTok_more );    func->doc = "Return true (1) if there are still more tokens, false (0) if no more tokens.";    if( !type_engine_import_mfun( env, func ) ) goto error;     // add next()    func = make_new_mfun( "string", "next", StrTok_next );    func->doc = "Return the next token string.";    if( !type_engine_import_mfun( env, func ) ) goto error;     // add get()    func = make_new_mfun( "string", "next", StrTok_next2 );    func->add_arg( "string", "out" );    func->doc = "Return the next token string. Additionally, write the token string to the 'out' string variable.";    if( !type_engine_import_mfun( env, func ) ) goto error;     // add get()    func = make_new_mfun( "string", "get", StrTok_get );    func->add_arg( "int", "index" );    func->doc = "Return the i-th token in the set string.";    if( !type_engine_import_mfun( env, func ) ) goto error; 

Lines 1–500 of 2044.

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