1 /*----------------------------------------------------------------------------
2 ChucK Concurrent, On-the-fly Audio Programming Language
3 Compiler and Virtual Machine
5 Copyright (c) 2004 Ge Wang and Perry R. Cook. All rights reserved.
6 http://chuck.cs.princeton.edu/
7 http://soundlab.cs.princeton.edu/
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
23 -----------------------------------------------------------------------------*/
25 //-----------------------------------------------------------------------------
29 // author: Ge Wang (gewang@cs.princeton.edu)
30 // Perry R. Cook (prc@cs.princeton.edu)
32 //-----------------------------------------------------------------------------
33 #ifndef __CHUCK_VM_H__
34 #define __CHUCK_VM_H__
37 #include "chuck_ugen.h"
46 //-----------------------------------------------------------------------------
48 //-----------------------------------------------------------------------------
49 #define CVM_MEM_STACK_SIZE (0x1 << 16)
50 #define CVM_REG_STACK_SIZE (0x1 << 16)
57 class Chuck_VM_FTable
;
66 //-----------------------------------------------------------------------------
67 // name: class Chuck_VM_Stack
69 //-----------------------------------------------------------------------------
72 //-----------------------------------------------------------------------------
74 //-----------------------------------------------------------------------------
80 t_CKBOOL
initialize( t_CKUINT size
);
83 //-----------------------------------------------------------------------------
85 //-----------------------------------------------------------------------------
91 public: // linked list
92 Chuck_VM_Stack
* prev
;
93 Chuck_VM_Stack
* next
;
102 //-----------------------------------------------------------------------------
103 // name: class Chuck_VM_Code
105 //-----------------------------------------------------------------------------
113 Chuck_Instr
** instr
;
117 t_CKUINT stack_depth
;
123 //-----------------------------------------------------------------------------
124 // name: class Chuck_VM_Shred
126 //-----------------------------------------------------------------------------
129 //-----------------------------------------------------------------------------
131 //-----------------------------------------------------------------------------
136 t_CKBOOL
initialize( Chuck_VM_Code
* c
,
137 t_CKUINT mem_st_size
= CVM_MEM_STACK_SIZE
,
138 t_CKUINT reg_st_size
= CVM_REG_STACK_SIZE
);
140 t_CKBOOL
run( Chuck_VM
* vm
);
141 t_CKBOOL
add( Chuck_UGen
* ugen
);
142 t_CKBOOL
remove( Chuck_UGen
* ugen
);
144 //-----------------------------------------------------------------------------
146 //-----------------------------------------------------------------------------
147 public: // machine components
149 Chuck_VM_Stack
* mem
;
150 Chuck_VM_Stack
* reg
;
153 Chuck_VM_Code
* code
;
154 Chuck_Instr
** instr
;
155 Chuck_VM_Shred
* parent
;
156 map
<t_CKUINT
, Chuck_VM_Shred
*> children
;
168 map
<Chuck_UGen
*, Chuck_UGen
*> m_ugen_map
;
175 Chuck_VM_Shred
* prev
;
176 Chuck_VM_Shred
* next
;
182 //-----------------------------------------------------------------------------
183 // name: class Chuck_VM_Shreduler
185 //-----------------------------------------------------------------------------
186 class Chuck_VM_Shreduler
188 //-----------------------------------------------------------------------------
190 //-----------------------------------------------------------------------------
192 Chuck_VM_Shreduler();
193 ~Chuck_VM_Shreduler();
196 t_CKBOOL
initialize();
199 public: // shreduling
200 t_CKBOOL
shredule( Chuck_VM_Shred
* shred
);
201 t_CKBOOL
shredule( Chuck_VM_Shred
* shred
, t_CKTIME wake_time
);
202 Chuck_VM_Shred
* get( );
205 public: // high-level shred interface
206 t_CKBOOL
remove( Chuck_VM_Shred
* shred
);
207 t_CKBOOL
replace( Chuck_VM_Shred
* out
, Chuck_VM_Shred
* in
);
208 Chuck_VM_Shred
* lookup( t_CKUINT id
);
211 //-----------------------------------------------------------------------------
213 //-----------------------------------------------------------------------------
220 Chuck_VM_Shred
* shred_list
;
225 Chuck_UGen
* m_bunghole
;
226 t_CKUINT m_num_dac_channels
;
227 t_CKUINT m_num_adc_channels
;
233 //-----------------------------------------------------------------------------
234 // name: class Chuck_VM
236 //-----------------------------------------------------------------------------
239 //-----------------------------------------------------------------------------
241 //-----------------------------------------------------------------------------
247 t_CKBOOL
initialize( t_CKBOOL enable_audio
= TRUE
, t_CKBOOL halt
= TRUE
,
248 t_CKUINT srate
= 44100,
249 t_CKUINT buffer_size
= 512, t_CKUINT num_buffers
= 4,
250 t_CKUINT dac
= 0, t_CKUINT adc
= 0,
251 t_CKINT priority
= 0x7fffffff );
255 Chuck_VM_Shred
* spork( Chuck_VM_Code
* code
, Chuck_VM_Shred
* parent
);
256 Chuck_VM_Shred
* fork( Chuck_VM_Code
* code
);
257 Chuck_VM_Shreduler
* shreduler() const;
262 t_CKUINT
srate() const;
264 public: // running the machine
269 public: // invoke functions
270 t_CKBOOL
invoke_static( Chuck_VM_Shred
* shred
);
272 public: // garbage collection
274 void gc( t_CKUINT amount
);
277 t_CKBOOL
queue_msg( Chuck_Msg
* msg
, int num_msg
);
278 t_CKUINT
process_msg( Chuck_Msg
* msg
);
279 Chuck_Msg
* get_reply( );
281 public: // static/dynamic function table
282 Chuck_DLL
* dll_load( const char * path
, const char * id
= NULL
);
283 t_CKBOOL
dll_unload( Chuck_DLL
* & dll
);
285 void set_env( void * env
) { m_env
= env
; }
286 void * get_env( ) { return m_env
; }
289 const char * last_error() const
290 { return m_last_error
.c_str(); }
292 //-----------------------------------------------------------------------------
294 //-----------------------------------------------------------------------------
299 Chuck_UGen
* m_bunghole
;
300 t_CKUINT m_num_adc_channels
;
301 t_CKUINT m_num_dac_channels
;
307 Chuck_VM_Shred
* spork( Chuck_VM_Shred
* shred
);
308 t_CKBOOL
free( Chuck_VM_Shred
* shred
, t_CKBOOL cascade
,
309 t_CKBOOL dec
= TRUE
);
317 Chuck_VM_Shred
* m_shreds
;
318 t_CKUINT m_num_shreds
;
320 Chuck_VM_Shreduler
* m_shreduler
;
326 Chuck_VM_FTable
* m_func_table
;
330 CBuffer
* m_msg_buffer
;
331 CBuffer
* m_reply_buffer
;
335 map
<string
, Chuck_DLL
*> m_dlls
;
339 static t_CKBOOL
set_priority( t_CKINT priority
, Chuck_VM
* vm
);
340 static t_CKINT our_priority
;
346 //-----------------------------------------------------------------------------
347 // name: enum Chuck_Msg_Type
349 //-----------------------------------------------------------------------------
366 // callback function prototype
367 typedef void (* ck_msg_func
)( const Chuck_Msg
* msg
);
368 //-----------------------------------------------------------------------------
369 // name: struct Chuck_Msg
371 //-----------------------------------------------------------------------------
376 Chuck_VM_Code
* code
;
377 Chuck_VM_Shred
* shred
;
386 Chuck_Msg() { memset( this, 0, sizeof(*this) ); }