Skip to content

Commit

Permalink
add constructors for sndbuf and string
Browse files Browse the repository at this point in the history
  • Loading branch information
gewang committed Nov 26, 2023
1 parent de83a1c commit b8e8e81
Show file tree
Hide file tree
Showing 5 changed files with 113 additions and 29 deletions.
17 changes: 17 additions & 0 deletions VERSIONS
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,23 @@ new Foo(10,11,12) @=> Foo @ f4;
- (added) Shred.parent() and Shred.ancestor()
- (fixed) Type.of("int[][]").isArray() now correctly returns true
- (updated) ChucK API doucmentation for constructors
- (added) overloaded constructors added (more to come)
==================
Gain( float gain );
Construct a Gain with default value.
SndBuf( string path );
Construct a SndBuf with the 'path' to a sound file to read.
SndBuf( string path, float rate );
Construct a SndBuf with the 'path' to a sound file to read, and a default playback 'rate' (1.0 is normal rate)
SndBuf( string path, float rate, int pos );
Construct a SndBuf with the 'path' to a sound file to read, a default playback 'rate' (1.0 is normal rate), and starting at sample position 'pos'
string( string str );
Construct a string as a copy of another string.
Osc( float freq );
Construct a Osc at specified frequency.
Osc( float freq, float phase );
Construct an Osc at specified frequency and phase.
(similarly for SinOsc, TriOsc, SqrOsc, SawOsc, PulseOsc, Phasor)
==================
chugins API update
==================
Expand Down
11 changes: 7 additions & 4 deletions examples/basic/sndbuf.ck
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,13 @@ me.dir() + "../data/snare.wav" => string filename;
// if there is argument, use it as the filename
if( me.args() ) me.arg(0) => filename;

// the patch
SndBuf buf => dac;
// load the file
filename => buf.read;
// the patch
SndBuf buf(filename) => dac;
// can also load the file this way
// filename => buf.read;

// check if file successfully loaded
if( !buf.ready() ) me.exit();

// time loop
while( true )
Expand Down
28 changes: 14 additions & 14 deletions src/core/ugen_osc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,14 +84,14 @@ DLL_QUERY osc_query( Chuck_DL_Query * QUERY )
// overload constructor (float freq)
func = make_new_ctor( oscx_ctor_1 );
func->add_arg( "float", "freq" );
func->doc = "Constructor to initialize a Osc at specified frequency.";
func->doc = "construct Osc at specified frequency.";
if( !type_engine_import_ctor( env, func ) ) goto error;

// overload constructor (float freq, float phase)
func = make_new_ctor( oscx_ctor_2 );
func->add_arg( "float", "freq" );
func->add_arg( "float", "phase" );
func->doc = "Constructor to initialize a Osc at specified frequency and phase.";
func->doc = "construct Osc at specified frequency and phase.";
if( !type_engine_import_ctor( env, func ) ) goto error;

// add ctrl: freq
Expand Down Expand Up @@ -152,14 +152,14 @@ DLL_QUERY osc_query( Chuck_DL_Query * QUERY )
// overload constructor (float freq)
func = make_new_ctor( oscx_ctor_1 );
func->add_arg( "float", "freq" );
func->doc = "Constructor to initialize a Phasor at specified frequency.";
func->doc = "construct Phasor at specified frequency.";
if( !type_engine_import_ctor( env, func ) ) goto error;

// overload constructor (float freq, float phase)
func = make_new_ctor( oscx_ctor_2 );
func->add_arg( "float", "freq" );
func->add_arg( "float", "phase" );
func->doc = "Constructor to initialize a Phasor at specified frequency and phase.";
func->doc = "construct Phasor at specified frequency and phase.";
if( !type_engine_import_ctor( env, func ) ) goto error;

// add examples | 1.5.0.0 added
Expand All @@ -180,14 +180,14 @@ DLL_QUERY osc_query( Chuck_DL_Query * QUERY )
// overload constructor (float freq)
func = make_new_ctor( oscx_ctor_1 );
func->add_arg( "float", "freq" );
func->doc = "Constructor to initialize a SinOsc at specified frequency.";
func->doc = "construct SinOsc at specified frequency.";
if( !type_engine_import_ctor( env, func ) ) goto error;

// overload constructor (float freq, float phase)
func = make_new_ctor( oscx_ctor_2 );
func->add_arg( "float", "freq" );
func->add_arg( "float", "phase" );
func->doc = "Constructor to initialize a SinOsc at specified frequency and phase.";
func->doc = "construct SinOsc at specified frequency and phase.";
if( !type_engine_import_ctor( env, func ) ) goto error;

// add examples | 1.5.0.0 (ge)
Expand Down Expand Up @@ -216,14 +216,14 @@ DLL_QUERY osc_query( Chuck_DL_Query * QUERY )
// overload constructor (float freq)
func = make_new_ctor( oscx_ctor_1 );
func->add_arg( "float", "freq" );
func->doc = "Constructor to initialize a TriOsc at specified frequency.";
func->doc = "construct TriOsc at specified frequency.";
if( !type_engine_import_ctor( env, func ) ) goto error;

// overload constructor (float freq, float phase)
func = make_new_ctor( oscx_ctor_2 );
func->add_arg( "float", "freq" );
func->add_arg( "float", "phase" );
func->doc = "Constructor to initialize a TriOsc at specified frequency and phase.";
func->doc = "construct TriOsc at specified frequency and phase.";
if( !type_engine_import_ctor( env, func ) ) goto error;

func = make_new_mfun( "float", "width", osc_ctrl_width );
Expand Down Expand Up @@ -255,14 +255,14 @@ DLL_QUERY osc_query( Chuck_DL_Query * QUERY )
// overload constructor (float freq)
func = make_new_ctor( oscx_ctor_1 );
func->add_arg( "float", "freq" );
func->doc = "Constructor to initialize a SawOsc at specified frequency.";
func->doc = "construct SawOsc at specified frequency.";
if( !type_engine_import_ctor( env, func ) ) goto error;

// overload constructor (float freq, float phase)
func = make_new_ctor( oscx_ctor_2 );
func->add_arg( "float", "freq" );
func->add_arg( "float", "phase" );
func->doc = "Constructor to initialize a SawOsc at specified frequency and phase.";
func->doc = "construct SawOsc at specified frequency and phase.";
if( !type_engine_import_ctor( env, func ) ) goto error;

func = make_new_mfun( "float", "width", sawosc_ctrl_width );
Expand Down Expand Up @@ -294,14 +294,14 @@ DLL_QUERY osc_query( Chuck_DL_Query * QUERY )
// overload constructor (float freq)
func = make_new_ctor( oscx_ctor_1 );
func->add_arg( "float", "freq" );
func->doc = "Constructor to initialize a PulseOsc at specified frequency.";
func->doc = "construct PulseOsc at specified frequency.";
if( !type_engine_import_ctor( env, func ) ) goto error;

// overload constructor (float freq, float phase)
func = make_new_ctor( oscx_ctor_2 );
func->add_arg( "float", "freq" );
func->add_arg( "float", "phase" );
func->doc = "Constructor to initialize a PulseOsc at specified frequency and phase.";
func->doc = "construct PulseOsc at specified frequency and phase.";
if( !type_engine_import_ctor( env, func ) ) goto error;

func = make_new_mfun( "float", "width", osc_ctrl_width );
Expand Down Expand Up @@ -332,14 +332,14 @@ DLL_QUERY osc_query( Chuck_DL_Query * QUERY )
// overload constructor (float freq)
func = make_new_ctor( oscx_ctor_1 );
func->add_arg( "float", "freq" );
func->doc = "Constructor to initialize a SqrOsc at specified frequency.";
func->doc = "construct SqrOsc at specified frequency.";
if( !type_engine_import_ctor( env, func ) ) goto error;

// overload constructor (float freq, float phase)
func = make_new_ctor( oscx_ctor_2 );
func->add_arg( "float", "freq" );
func->add_arg( "float", "phase" );
func->doc = "Constructor to initialize a SqrOsc at specified frequency and phase.";
func->doc = "construct SqrOsc at specified frequency and phase.";
if( !type_engine_import_ctor( env, func ) ) goto error;

func = make_new_mfun( "float", "width", sqrosc_ctrl_width );
Expand Down
81 changes: 71 additions & 10 deletions src/core/ugen_xxx.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
#include "chuck_compile.h"
#include "chuck_instr.h"
#include "util_math.h"
#include "util_raw.h"

#include <math.h>
#include <stdio.h>
Expand Down Expand Up @@ -364,7 +365,7 @@ DLL_QUERY xxx_query( Chuck_DL_Query * QUERY )

// add ctrl: fprob
func = make_new_mfun( "void", "Gain", gain_ctor_1 );
func->doc = "Create a Gain with default value.";
func->doc = "construct a Gain with default value.";
func->add_arg( "float", "gain" );
if( !type_engine_import_mfun( env, func ) ) goto error;

Expand Down Expand Up @@ -717,14 +718,37 @@ DLL_QUERY xxx_query( Chuck_DL_Query * QUERY )
sndbuf_offset_data = type_engine_import_mvar( env, "int", "@sndbuf_data", FALSE );
if( sndbuf_offset_data == CK_INVALID_OFFSET ) goto error;

// add ctor( string path )
func = make_new_ctor( sndbuf_ctor_path );
func->add_arg( "string", "path" );
func->doc = "construct a SndBuf with the 'path' to a sound file to read.";
if( !type_engine_import_ctor( env, func ) ) goto error;

// add ctor( string path, float rate )
func = make_new_ctor( sndbuf_ctor_path_rate );
func->add_arg( "string", "path" );
func->add_arg( "float", "rate" );
func->doc = "construct a SndBuf with the 'path' to a sound file to read, and a default playback 'rate' (1.0 is normal rate)";
if( !type_engine_import_ctor( env, func ) ) goto error;

// add ctor( string path )
func = make_new_ctor( sndbuf_ctor_path_rate_pos );
func->add_arg( "string", "path" );
func->add_arg( "float", "rate" );
func->add_arg( "int", "pos" );
func->doc = "construct a SndBuf with the 'path' to a sound file to read, a default playback 'rate' (1.0 is normal rate), and starting at sample position 'pos'";
if( !type_engine_import_ctor( env, func ) ) goto error;

// add ctrl: read
func = make_new_mfun( "string", "read", sndbuf_ctrl_read );
func->add_arg( "string", "read" );
func->doc = "read file for reading.";
if( !type_engine_import_mfun( env, func ) ) goto error;
// add cget: read // area
//func = make_new_mfun( "string", "read", sndbuf_cget_read );
//if( !type_engine_import_mfun( env, func ) ) goto error;

// add cget: ready
func = make_new_mfun( "int", "ready", sndbuf_cget_ready );
func->doc = "query whether the SndBuf is ready for use (e.g., sound file successfully loaded).";
if( !type_engine_import_mfun( env, func ) ) goto error;

// add ctrl: write
func = make_new_mfun( "string", "write", sndbuf_ctrl_write );
Expand Down Expand Up @@ -2926,7 +2950,6 @@ void sndbuf_sinc_interpolate( sndbuf_data * d, SAMPLE * out );
CK_DLL_CTOR( sndbuf_ctor )
{
OBJ_MEMBER_UINT(SELF, sndbuf_offset_data) = (t_CKUINT)new sndbuf_data;

}

CK_DLL_DTOR( sndbuf_dtor )
Expand All @@ -2936,6 +2959,41 @@ CK_DLL_DTOR( sndbuf_dtor )
OBJ_MEMBER_UINT(SELF, sndbuf_offset_data) = 0;
}

CK_DLL_CTOR( sndbuf_ctor_path )
{
Chuck_DL_Return RETURN;
// read it
sndbuf_ctrl_read( SELF, ARGS, &RETURN, VM, SHRED, API );
}

CK_DLL_CTOR( sndbuf_ctor_path_rate )
{
Chuck_DL_Return RETURN;
// read it
sndbuf_ctrl_read( SELF, ARGS, &RETURN, VM, SHRED, API );

// advance args, skip over path
GET_NEXT_STRING(ARGS);
// control rate
sndbuf_ctrl_rate( SELF, ARGS, &RETURN, VM, SHRED, API );
}

CK_DLL_CTOR( sndbuf_ctor_path_rate_pos )
{
Chuck_DL_Return RETURN;
// read it
sndbuf_ctrl_read( SELF, ARGS, &RETURN, VM, SHRED, API );

// advance args, skip over path
GET_NEXT_STRING(ARGS);
// control rate
sndbuf_ctrl_rate( SELF, ARGS, &RETURN, VM, SHRED, API );

// advance args, skip over rate
GET_NEXT_FLOAT(ARGS);
sndbuf_ctrl_pos( SELF, ARGS, &RETURN, VM, SHRED, API );
}

inline t_CKUINT sndbuf_read( sndbuf_data * d, t_CKUINT frame, t_CKUINT num_frames )
{
// check
Expand Down Expand Up @@ -3361,10 +3419,6 @@ CK_DLL_TICKF( sndbuf_tickf )
return TRUE;
}


#include "util_raw.h"


CK_DLL_CTRL( sndbuf_ctrl_read )
{
sndbuf_data * d = (sndbuf_data *)OBJ_MEMBER_UINT(SELF, sndbuf_offset_data);
Expand Down Expand Up @@ -3667,10 +3721,17 @@ CK_DLL_CTRL( sndbuf_ctrl_write )
#endif
}

CK_DLL_CGET( sndbuf_cget_ready )
{
// get the internal representation
sndbuf_data * d = ( sndbuf_data * )OBJ_MEMBER_UINT(SELF, sndbuf_offset_data);
// ready if we have a file descriptor
RETURN->v_int = d->fd != NULL;
}

CK_DLL_CTRL( sndbuf_ctrl_rate )
{
sndbuf_data * d = ( sndbuf_data * ) OBJ_MEMBER_UINT(SELF, sndbuf_offset_data);
sndbuf_data * d = ( sndbuf_data * )OBJ_MEMBER_UINT(SELF, sndbuf_offset_data);
t_CKFLOAT rate = GET_CK_FLOAT(ARGS); // rate
d->rate = rate * d->sampleratio;
d->rate_factor = rate;
Expand Down
5 changes: 4 additions & 1 deletion src/core/ugen_xxx.h
Original file line number Diff line number Diff line change
Expand Up @@ -157,11 +157,14 @@ CK_DLL_CGET( delayp_cget_max );

// sndbuf
CK_DLL_CTOR( sndbuf_ctor );
CK_DLL_CTOR( sndbuf_ctor_path );
CK_DLL_CTOR( sndbuf_ctor_path_rate );
CK_DLL_CTOR( sndbuf_ctor_path_rate_pos );
CK_DLL_DTOR( sndbuf_dtor );
CK_DLL_TICK( sndbuf_tick );
CK_DLL_TICKF( sndbuf_tickf );
CK_DLL_CTRL( sndbuf_ctrl_read );
CK_DLL_CGET( sndbuf_cget_read );
CK_DLL_CGET( sndbuf_cget_ready );
CK_DLL_CTRL( sndbuf_ctrl_write );
CK_DLL_CGET( sndbuf_cget_write );
CK_DLL_CTRL( sndbuf_ctrl_pos );
Expand Down

0 comments on commit b8e8e81

Please sign in to comment.