Ge Wangwill it compile?bb2ec149 years agoHistory
919 lines · 27.1 KB · C++
RawDownload
/*---------------------------------------------------------------------------- ChucK Concurrent, On-the-fly Audio Programming Language   Compiler and Virtual Machine  Copyright (c) 2003 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: chuck.h// desc: chuck engine header; VM + compiler + state; no audio I/O//       REFACTOR-2017//// author: Ge Wang (http://www.gewang.com/)// date: fall 2017//// additional authors://         Jack Atherton (lja@ccrma.stanford.edu)//         Spencer Salazar (spencer@ccrma.stanford.edu)//-----------------------------------------------------------------------------#include "chuck.h"#include "chuck_errmsg.h"#include "chuck_otf.h"#include "ulib_machine.h"#include "util_network.h"#include "util_string.h" #ifndef __PLATFORM_WIN32__#include <unistd.h>#include <netinet/in.h>#include <arpa/inet.h>#include <sys/param.h>   // added 1.3.0.0#else#include <direct.h>      // added 1.3.0.0#define MAXPATHLEN (255) // addec 1.3.0.0#endif // #ifndef __PLATFORM_WIN32__    // chuck param defaults#define CHUCK_PARAM_SAMPLE_RATE_DEFAULT          "44100"#define CHUCK_PARAM_INPUT_CHANNELS_DEFAULT       "2"#define CHUCK_PARAM_OUTPUT_CHANNELS_DEFAULT      "2"#define CHUCK_PARAM_VM_ADAPTIVE_DEFAULT          "0"#define CHUCK_PARAM_VM_HALT_DEFAULT              "0"#define CHUCK_PARAM_OTF_ENABLE_DEFAULT           "0"#define CHUCK_PARAM_OTF_PORT_DEFAULT             "8888"#define CHUCK_PARAM_DUMP_INSTRUCTIONS_DEFAULT    "0"#define CHUCK_PARAM_DEPRECATE_LEVEL_DEFAULT      "1"#define CHUCK_PARAM_WORKING_DIRECTORY_DEFAULT    ""#define CHUCK_PARAM_CHUGIN_ENABLE_DEFAULT        "1"#define CHUCK_PARAM_CHUGIN_DIRECTORY_DEFAULT     ""   // chuck statics// versionconst char ChucK::VERSION[] = "1.3.6.0-rc1 (numchuck)";t_CKUINT ChucK::o_numVMs = 0;    //-----------------------------------------------------------------------------// name: version()// desc: get chuck version//-----------------------------------------------------------------------------const char * ChucK::version(){    return VERSION;}    //-----------------------------------------------------------------------------// name: intSize()// desc: get chuck int size (in bits)//-----------------------------------------------------------------------------t_CKUINT ChucK::intSize(){    return machine_intsize();}    //-----------------------------------------------------------------------------// name: ChucK()// desc: constructor//-----------------------------------------------------------------------------ChucK::ChucK(){    // instantiate the carrier!    m_carrier = new Chuck_Carrier();    // increment the numChucKs    o_numVMs++;    // initialize default params    initDefaultParams();    // did user init?    m_init = FALSE;}    //-----------------------------------------------------------------------------// name: ~ChucK()// desc: destructor//-----------------------------------------------------------------------------ChucK::~ChucK(){    // shutdown    shutdown();    // decrement the numChucKs    o_numVMs--;}    // REFACTOR-2017: TODO Ge: Implementation of this?//-----------------------------------------------------------------------------// name: ck_param_type// desc: enum for type of param (int, float, string)//-----------------------------------------------------------------------------enum ck_param_type{    ck_param_int, ck_param_float, ck_param_string};   //-----------------------------------------------------------------------------// name: ck_param_types// desc: storage for types of param (int, float, string)//-----------------------------------------------------------------------------std::map< std::string, ck_param_type> ck_param_types;    //-----------------------------------------------------------------------------// name: initDefaultParams()// desc: initialize default params//-----------------------------------------------------------------------------void ChucK::initDefaultParams(){    m_params[CHUCK_PARAM_SAMPLE_RATE] = CHUCK_PARAM_SAMPLE_RATE_DEFAULT;    m_params[CHUCK_PARAM_INPUT_CHANNELS] = CHUCK_PARAM_INPUT_CHANNELS_DEFAULT;    m_params[CHUCK_PARAM_OUTPUT_CHANNELS] = CHUCK_PARAM_OUTPUT_CHANNELS_DEFAULT;    m_params[CHUCK_PARAM_VM_ADAPTIVE] = CHUCK_PARAM_VM_ADAPTIVE_DEFAULT;    m_params[CHUCK_PARAM_VM_HALT] = CHUCK_PARAM_VM_HALT_DEFAULT;    m_params[CHUCK_PARAM_OTF_ENABLE] = CHUCK_PARAM_OTF_ENABLE_DEFAULT;    m_params[CHUCK_PARAM_OTF_PORT] = CHUCK_PARAM_OTF_PORT_DEFAULT;    m_params[CHUCK_PARAM_DUMP_INSTRUCTIONS] = CHUCK_PARAM_DUMP_INSTRUCTIONS_DEFAULT;    m_params[CHUCK_PARAM_DEPRECATE_LEVEL] = CHUCK_PARAM_DEPRECATE_LEVEL_DEFAULT;    m_params[CHUCK_PARAM_WORKING_DIRECTORY] = CHUCK_PARAM_WORKING_DIRECTORY_DEFAULT;    m_params[CHUCK_PARAM_CHUGIN_DIRECTORY] = CHUCK_PARAM_CHUGIN_DIRECTORY_DEFAULT;    m_params[CHUCK_PARAM_CHUGIN_ENABLE] = CHUCK_PARAM_CHUGIN_ENABLE_DEFAULT;        ck_param_types[CHUCK_PARAM_SAMPLE_RATE] =       ck_param_int;    ck_param_types[CHUCK_PARAM_INPUT_CHANNELS] =    ck_param_int;    ck_param_types[CHUCK_PARAM_OUTPUT_CHANNELS] =   ck_param_int;    ck_param_types[CHUCK_PARAM_VM_ADAPTIVE] =       ck_param_int;    ck_param_types[CHUCK_PARAM_VM_HALT] =           ck_param_int;    ck_param_types[CHUCK_PARAM_OTF_ENABLE] =        ck_param_int;    ck_param_types[CHUCK_PARAM_OTF_PORT] =          ck_param_int;    ck_param_types[CHUCK_PARAM_DUMP_INSTRUCTIONS] = ck_param_int;    ck_param_types[CHUCK_PARAM_DEPRECATE_LEVEL] =   ck_param_int;    ck_param_types[CHUCK_PARAM_WORKING_DIRECTORY] = ck_param_string;    ck_param_types[CHUCK_PARAM_CHUGIN_DIRECTORY] =  ck_param_string;    ck_param_types[CHUCK_PARAM_CHUGIN_ENABLE] =     ck_param_int;}    //-----------------------------------------------------------------------------// name: setParam()// desc: set an int param by name//-----------------------------------------------------------------------------bool ChucK::setParam( const std::string & name, t_CKINT value ){    if( m_params.count( name ) > 0 && ck_param_types[name] == ck_param_int )    {        std::ostringstream s;        s << value;        m_params[name] = s.str();        return TRUE;    }    else    {        return FALSE;    }}    //-----------------------------------------------------------------------------// name: setParam()// desc: set a float param by name//-----------------------------------------------------------------------------bool ChucK::setParam( const std::string & name, t_CKFLOAT value ){    if( m_params.count( name ) > 0 && ck_param_types[name] == ck_param_float )    {        std::ostringstream s;        s << value;        m_params[name] = s.str();        return TRUE;    }    else    {        return FALSE;    }}    //-----------------------------------------------------------------------------// name: setParam()// desc: set a string param by name//-----------------------------------------------------------------------------bool ChucK::setParam( const std::string & name, const std::string & value ){    if( m_params.count( name ) > 0 && ck_param_types[name] == ck_param_string )    {        m_params[name] = value;        return TRUE;    }    else    {        return FALSE;    }}    //-----------------------------------------------------------------------------// name: getParamInt()// desc: get an int param//-----------------------------------------------------------------------------t_CKINT ChucK::getParamInt( const std::string & key ){    t_CKINT result = 0;    if( m_params.count( key ) > 0 && ck_param_types[key] == ck_param_int )    {        istringstream s( m_params[key] );        s >> result;    }    return result;}    //-----------------------------------------------------------------------------// name: getParamInt()// desc: get a float param//-----------------------------------------------------------------------------t_CKFLOAT ChucK::getParamFloat( const std::string & key ){    t_CKFLOAT result = 0;    if( m_params.count( key ) > 0 && ck_param_types[key] == ck_param_float )    {        istringstream s( m_params[key] );        s >> result;    }    return result;}    //-----------------------------------------------------------------------------// name: getParamString()// desc: get a string param//-----------------------------------------------------------------------------std::string ChucK::getParamString( const std::string & key ){    if( m_params.count( key ) > 0 && ck_param_types[key] == ck_param_string )    {        return m_params[key];    }    else    {        return "";    }}    //-----------------------------------------------------------------------------// initialize ChucK (using params)//-----------------------------------------------------------------------------bool ChucK::init(){    Chuck_VM_Code * code = NULL;    Chuck_VM_Shred * shred = NULL;    std::string cwd;    char cstr_cwd[MAXPATHLEN];        // sanity check    if( m_carrier->vm != NULL )    {        CK_FPRINTF_STDERR( "[chuck]: VM already initialized...\n" );        return false;    }        // get VM params    t_CKUINT srate = getParamInt( CHUCK_PARAM_SAMPLE_RATE );    t_CKUINT outs = getParamInt( CHUCK_PARAM_OUTPUT_CHANNELS );    t_CKUINT ins = getParamInt( CHUCK_PARAM_INPUT_CHANNELS );    t_CKUINT adaptiveSize = getParamInt( CHUCK_PARAM_VM_ADAPTIVE );    t_CKBOOL halt = getParamInt( CHUCK_PARAM_VM_HALT ) != 0;        // get compiler params    t_CKBOOL dump = getParamInt( CHUCK_PARAM_DUMP_INSTRUCTIONS ) != 0;    t_CKBOOL auto_depend = getParamInt( CHUCK_PARAM_AUTO_DEPEND ) != 0;    string workingDir = getParamString( CHUCK_PARAM_WORKING_DIRECTORY );    string chuginDir = getParamString( CHUCK_PARAM_CHUGIN_DIRECTORY );    t_CKUINT deprecate = getParamInt( CHUCK_PARAM_DEPRECATE_LEVEL );     // list of search pathes (added 1.3.0.0)    std::list<std::string> dl_search_path;    if( workingDir != std::string("") )    {        dl_search_path.push_back( workingDir );    }    if( chuginDir != std::string("") )    {        dl_search_path.push_back( chuginDir );    }    // list of individually named chug-ins (added 1.3.0.0)    std::list<std::string> named_dls;     // instantiate VM    m_carrier->vm = new Chuck_VM();    // reference back to carrier    m_carrier->vm->setCarrier( m_carrier );    // initialize VM    if( !m_carrier->vm->initialize( srate, outs, ins, adaptiveSize, halt ) )    {        CK_FPRINTF_STDERR( "[chuck]: %s\n", m_carrier->vm->last_error() );        goto cleanup;    }     // if chugin load is off, then clear the lists (added 1.3.0.0 -- TODO: refactor)    if( getParamInt( CHUCK_PARAM_CHUGIN_ENABLE ) == 0 )    {        // turn off chugin load        dl_search_path.clear();        named_dls.clear();    }        // instantiate compiler    m_carrier->compiler = new Chuck_Compiler();    // reference back to carrier    m_carrier->compiler->setCarrier( m_carrier );        // initialize compiler    if( !m_carrier->compiler->initialize( dl_search_path, named_dls ) )    {        CK_FPRINTF_STDERR( "[chuck]: compiler failed to initialize...\n" );        goto cleanup;    }    // set dump flag    m_carrier->compiler->emitter->dump = dump;    // set auto depend flag (for type checker) | currently must be FALSE    m_carrier->compiler->set_auto_depend( auto_depend );    // set deprecation level    m_carrier->env->deprecate_level = deprecate;        // VM + type system integration    if( !m_carrier->vm->initialize_synthesis() )    {        CK_FPRINTF_STDERR( "[chuck]: %s\n", m_carrier->vm->last_error() );        goto cleanup;    }        // figure out current working directory (added 1.3.0.0)    // is this needed for current path to work correctly?!    if( getcwd(cstr_cwd, MAXPATHLEN) == NULL )    {        // uh...        EM_log( CK_LOG_SEVERE, "error: unable to determine current working directory!" );    }    else    {        cwd = std::string(cstr_cwd);        cwd = normalize_directory_separator(cwd) + "/";    }        // whether or not chug should be enabled (added 1.3.0.0)    if( getParamInt( CHUCK_PARAM_CHUGIN_ENABLE ) != 0 )    {        // log        EM_log( CK_LOG_SEVERE, "pre-loading ChucK libs..." );        EM_pushlog();                // iterate over list of ck files that the compiler found        for( std::list<std::string>::iterator j =            compiler()->m_cklibs_to_preload.begin();            j != compiler()->m_cklibs_to_preload.end(); j++)        {            // the filename            std::string filename = *j;                        // log            EM_log( CK_LOG_SEVERE, "preloading '%s'...", filename.c_str() );            // push indent            EM_pushlog();                        // SPENCERTODO: what to do for full path            std::string full_path = filename;                        // parse, type-check, and emit            if( compiler()->go( filename, NULL, NULL, full_path ) )            {                // TODO: how to compilation handle?                //return 1;                                // get the code                code = compiler()->output();                // name it - TODO?                // code->name += string(argv[i]);                                // spork it                shred = vm()->spork( code, NULL, TRUE );            }                        // pop indent            EM_poplog();        }                // clear the list of chuck files to preload        compiler()->m_cklibs_to_preload.clear();                // pop log        EM_poplog();    }        // load user namespace    m_carrier->env->load_user_namespace();        // server    if( getParamInt( CHUCK_PARAM_OTF_ENABLE ) != 0 )    {        m_carrier->otf_port = getParamInt( CHUCK_PARAM_OTF_PORT );        // log        EM_log( CK_LOG_SYSTEM, "starting listener on port: %d...",                m_carrier->otf_port );                // start tcp server        m_carrier->otf_socket = ck_tcp_create( 1 );        if( !m_carrier->otf_socket ||            !ck_bind( m_carrier->otf_socket, m_carrier->otf_port ) ||            !ck_listen( m_carrier->otf_socket, 10 ) )        {            CK_FPRINTF_STDERR( "[chuck]: cannot bind to tcp port %li...\n", m_carrier->otf_port );            ck_close( m_carrier->otf_socket );            m_carrier->otf_socket = NULL;        }        else        {#if !defined(__PLATFORM_WIN32__) || defined(__WINDOWS_PTHREAD__)            pthread_create( &m_carrier->otf_thread, NULL, otf_cb, NULL );#else            m_carrier->otf_thread = CreateThread( NULL, 0,                (LPTHREAD_START_ROUTINE)otf_cb, NULL, 0, 0 );#endif        }    }    else    {        // log        EM_log( CK_LOG_SYSTEM, "OTF server/listener: OFF" );    }

Lines 1–500 of 919.

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