aboutsummaryrefslogtreecommitdiff
path: root/vst~.cpp
blob: 15c26e52a0bc7c5e6496e87dc490efa3ca6d6e3b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
#include "stdafx.h"
#include "m_pd.h"
#include <io.h>
#include <stdlib.h>
#include <direct.h>
#include<time.h>
#include "EditorThread.h"
#include "VstHost.h"

#include "vst~.h"

#if defined(_LANGUAGE_C_PLUS_PLUS) || defined(__cplusplus)
extern "C" {
#endif

static const char * findFilePath( const char * path , const char * dllname );

/* ------------------------ vst_tilde~ ----------------------------- */

static t_class *vst_tilde_class;

/**
*	the perform routine unpacks its parameters 
*	looks to see if time is zero (do channel prob 
*	everytime) if it is to chooses a channel.
*	the routine then copies everything in the input
*	to the choosen output
*/

t_int *vst_tilde_perform_replace(t_int *w)
{
     t_dsp_args *args = (t_dsp_args *)(w[1]);
    int n = (t_int)(w[2]);	
	args->plug->processReplacing( args->inbufs , args->outbufs , args->num_samples );
   return w+3;
}

t_int *vst_tilde_perform_acc(t_int *w)
{
     t_dsp_args *args = (t_dsp_args *)(w[1]);
    int n = (t_int)(w[2]);
	args->plug->process( args->inbufs , args->outbufs , args->num_samples );
   return w+3;
}

/**
*	set up our dsp perform routine - it takes parameters
*	the input channel, the output channels ( left and right), 
*	the pin object and the number of samples in the array
*/

static void vst_tilde_dsp(t_vst_tilde *x, t_signal **sp)
{
	int i;	
	if ( x->plug != NULL )
	{
		x->d_args = (t_dsp_args *) malloc( sizeof( t_dsp_args ));
		x->d_args->num_in = x->num_audio_inputs;
		x->d_args->num_out = x->num_audio_outputs;
		x->d_args->inbufs = (float**) malloc( x->d_args->num_in * sizeof( float *));
		for( i = 0 ; i< x->d_args->num_in;i++ )
		{
			x->d_args->inbufs[i] = sp[ i ]->s_vec;
		}
		x->d_args->outbufs = (float**) malloc( x->d_args->num_out * sizeof( float *));
		for( i = 0 ; i< x->d_args->num_out;i++ )
		{	
			x->d_args->outbufs[i] = sp[ i + x->d_args->num_in + 1]->s_vec;
		}
		x->d_args->num_samples = sp[0]->s_n;
		x->d_args->sample_rate = sp[0]->s_sr;
		x->d_args->plug = x->plug;
		//post("*");
		x->plug->Init( sp[0]->s_sr  , sp[0]->s_n );	
		//post("*");
		if ( x->plug->replace() )
		{
			dsp_add(vst_tilde_perform_replace, 2 , x->d_args , sp[0]->s_n );
		}
		else
		{
			dsp_add(vst_tilde_perform_acc, 2 , x->d_args , sp[0]->s_n );
		}
	}

}

/**
*	free up the tilde object - for now we only need 
*	to get rid of the clock
*/

static void vst_tilde_free(t_vst_tilde *x)
{	
	int i;
  /* Destroy inlets */
    if (x->audio_inlets != NULL) 
	{	
		for (i = 0; i < x->num_audio_inputs; i++) 
		{
			inlet_free (x->audio_inlets[i]);
		}
		free (x->audio_inlets);
		x->audio_inlets = NULL;
    }
    /* Destroy outlets */
    if (x->control_outlet != NULL) 
	{
		outlet_free (x->control_outlet);
		x->control_outlet = NULL;
	}
    if (x->audio_outlets != NULL) 
	{
		for (i = 0; i < x->num_audio_outputs; i++) 
		{
			outlet_free (x->audio_outlets[i]);
		}
		free (x->audio_outlets);
		x->audio_outlets = NULL;
    }

	if( x->d_args != NULL)
	{
		free( x->d_args->inbufs );
		free( x->d_args->outbufs);
		free( x->d_args );
	}
	if ( x->plug != NULL )
	{
		delete x->plug ;
	}
}

/**
*	make a new object - set up out internal variables 
*	and add our inlets and outlets
*/

static void *vst_tilde_new( t_symbol *s, int argc, t_atom *argv)
{
	post("In vst~ new");
	t_vst_tilde *x = (t_vst_tilde *)pd_new(vst_tilde_class);
	x->d_args = NULL;
	x->plug = new VSTPlugin();
	 x->audio_inlets = NULL;
	  x->audio_outlets = NULL;
	  x->control_outlet = NULL;

	// to help deal with spaces we assume ALL of the args make 
	// up the filename 
	char buf[255];	
	CString str;
	for( int i = 0 ; i < argc ; i++ )
	{
		atom_string( &argv[i] , buf, 255 );
		if ( i == 0 )
		{
			str += buf;
		}
		else
		{
			str += ' ';
			str += buf;
		}
	}
	bool lf = FALSE;

	// try loading the dll from the raw filename 
	if ( x->plug->Instance(  str ) == VSTINSTANCE_NO_ERROR )
	{
		//post( "it loaded fine ");
		lf = TRUE;
	}
	else // try finding it on the VST PAth
	{
		
		char* vst_path = getenv ("VST_PATH");
		char* tok_path = (char *) malloc( strlen( vst_path) * sizeof( char ));

		CString dllname;
		if ( str.MakeLower().Find( ".dll" ) == -1 )
		{
			dllname = str + ".dll";			
		}
		else
		{
			dllname = str;
		}
		strcpy( tok_path , vst_path );
		if ( vst_path != NULL )
		{
			char *tok = strtok( tok_path , ";" );
			 while( tok != NULL )
			 {
				 CString abpath( tok );
				 if( abpath.Right( 1 ) != _T("\\") )
				 {
					abpath += "\\";
				 }				 
				 const char * realpath = findFilePath( abpath , dllname );				
				 //post( "findFilePath( %s , %s ) = %s\n" , abpath , dllname , realpath );
				 if ( realpath != NULL )
				 {
					 CString rpath( realpath );
					rpath += _T("\\") + str;
					 post( "trying %s " , rpath );
					if ( x->plug->Instance( rpath ) == VSTINSTANCE_NO_ERROR )
					{
						post( "  %s loaded " , x->plug->GetName());
						lf = TRUE;
						break;
					}
				 }
				tok = strtok( NULL , ";" );
				if ( tok == NULL )
				{
					post( "couldn't find dll");
				}
			 }
		}
	}
	if ( !lf ) // failed - don't make any ins or outs
	{
		post("Unable to load %s" , str );
		delete x->plug;
		x->plug = NULL;
		return( x);
	}

	// make our inlets and outlets next

	x->num_audio_inputs = x->plug->getNumInputs();
	x->num_audio_outputs = x->plug->getNumOutputs();

  /* Allocate memory for in- and outlet pointers */
    x->audio_inlets = (t_inlet**)calloc (x->num_audio_inputs, sizeof (t_inlet*));
    x->audio_outlets = (t_outlet**)calloc (x->num_audio_outputs, sizeof (t_outlet*));


    /* The first inlet is always there (needn't be created), and is
       used for control messages.  Now, create the rest of the
       inlets for audio signal input. */ 
    for (i = 0; i < x->num_audio_inputs; i++) 
	{
		x->audio_inlets[i] = inlet_new (&x->x_obj,&x->x_obj.ob_pd,gensym ("signal"),gensym ("signal"));
    }

	 // set up our inlets for midi notes
	inlet_new(&x->x_obj, &x->x_obj.ob_pd, gensym("float"), gensym("midinote"));	
	floatinlet_new(&x->x_obj, &x->x_vel);

    /* We use the first outlet always for VST parameter control  messages */
    x->control_outlet = outlet_new (&x->x_obj, gensym ("control"));

    /* The rest of the outlets are used for audio signal output */
    for (i = 0; i < x->num_audio_outputs; i++) 
	{
		x->audio_outlets[i] = outlet_new (&x->x_obj, gensym ("signal"));
    }
	
	return (x);
}

static const char * findFilePath( const char * path , const char * dllname )
{
	CFileFind finder;
	_chdir( path );
	if ( finder.FindFile( dllname ) == TRUE )
	{
		return path;
	}
	else
	{
		finder.FindFile();
		while( finder.FindNextFile() )
		{
			if ( finder.IsDirectory() )
			{
				if ( !finder.IsDots() )
				{
					CString *npath = new CString( finder.GetFilePath()); 
					const char * ret = findFilePath( *npath , dllname );
					if ( ret != NULL)
					{
						CString *retstr = new CString( ret );
						return *retstr;
					}
				}
			}
		}
	}
	return NULL;
}


/**
*	setup - add our methods 
*/

 void vst_tilde_setup(void)
{	
	 srand( (unsigned) time( NULL ) );
    vst_tilde_class = class_new(gensym("vst~"), (t_newmethod) vst_tilde_new, (t_method) vst_tilde_free,
    	sizeof(t_vst_tilde), 0, A_GIMME , 0);    
    CLASS_MAINSIGNALIN( vst_tilde_class, t_vst_tilde, x_f);
    class_addmethod(vst_tilde_class, (t_method) vst_tilde_dsp, gensym("dsp"), (t_atomtype)0);    	
	class_addmethod (vst_tilde_class,(t_method)vst_tilde_control,gensym ("control"),A_DEFSYM, A_DEFFLOAT, 0);
	class_addmethod (vst_tilde_class,(t_method)vst_tilde_pitchbend,gensym ("pitchbend"),A_DEFFLOAT, 0);
	class_addmethod (vst_tilde_class,(t_method)vst_tilde_program,gensym ("program"),A_DEFFLOAT, 0);
	class_addmethod (vst_tilde_class,(t_method)vst_tilde_programchange,gensym ("programchange"),A_DEFFLOAT, 0);
	class_addmethod (vst_tilde_class,(t_method)vst_tilde_ctrlchange,gensym ("ctrlchange"),A_DEFFLOAT ,A_DEFFLOAT, 0);
    class_addmethod (vst_tilde_class,(t_method)vst_tilde_print,gensym ("print"),A_GIMME,(t_atomtype) 0);
	class_addmethod (vst_tilde_class,(t_method)vst_tilde_edit,gensym ("edit"),(t_atomtype) 0);
	class_addmethod (vst_tilde_class,(t_method)vst_tilde_showparams,gensym ("showparams"),(t_atomtype) 0);
	class_addmethod (vst_tilde_class,(t_method)vst_tilde_noshowparams,gensym ("noshowparams"),(t_atomtype) 0);
	class_addmethod (vst_tilde_class,(t_method)vst_tilde_param,gensym ("param"), A_DEFFLOAT , A_DEFFLOAT , (t_atomtype) 0);
    class_addmethod (vst_tilde_class,(t_method)vst_tilde_reset,gensym ("reset"),(t_atomtype) 0);
	class_addmethod (vst_tilde_class,(t_method)vst_tilde_midinote,gensym ("midinote"),A_DEFFLOAT,(t_atomtype) 0);	
	//class_sethelpsymbol(vst_tilde_class, gensym("vst/vsthelp"));
}

 static void vst_tilde_control (t_vst_tilde* x,  t_symbol* ctrl_name,t_float ctrl_value)     
{
    unsigned parm_num = 0;
    
    if (ctrl_name->s_name == NULL || strlen (ctrl_name->s_name) == 0) 
	{
		error ("plugin~: control messages must have a name and a value");
		return;
    }
    //parm_num = vst_tilde_get_parm_number (x, ctrl_name->s_name);
    //if (parm_num) 
	//{
		//vst_tilde_set_control_input_by_index (x, parm_num - 1, ctrl_value);
    //}
    //else 
	//{
		//vst_tilde_set_control_input_by_name (x, ctrl_name->s_name, ctrl_value);
    //}
}

 static void vst_tilde_pitchbend (t_vst_tilde* x,  t_float ctrl_value)     
{
	x->plug->AddPitchBend( (int) ctrl_value );    
}

  static void vst_tilde_programchange (t_vst_tilde* x,  t_float ctrl_value)     
{
	x->plug->AddProgramChange( (int) ctrl_value );    
}

static void vst_tilde_program (t_vst_tilde* x,  t_float ctrl_value)     
{
	x->plug->SetCurrentProgram( (int) ctrl_value );    
}

   static void vst_tilde_ctrlchange (t_vst_tilde* x, t_float control , t_float ctrl_value)     
{
	x->plug->AddControlChange( (int) control , (int) ctrl_value );    
}


 /**
 *	display the parameters names and values and some other bits and pieces that 
 *	may be of use
 */

static void vst_tilde_print (t_vst_tilde* x, t_symbol *s, int ac, t_atom *av ) 
{
     int i;
	bool params = false;
	bool header = true;
	bool programs = false;
	bool parameters = true;
	int specific = -1;
	 if ( ac > 0 )
	 {
		for( i = 0 ; i < ac ; i++)
		{
			if ( av[i].a_type == A_SYMBOL )
			{
				char buf[255];	
				atom_string( &av[i] , buf, 255 );
				if ( strcmp( buf , "-params" ) == 0 )
				{
					params = true;
				}
				else if ( strcmp( buf , "-noheader" ) == 0 )
				{
					header = false;
				}
				else if ( strcmp( buf , "-programs" ) == 0 )
				{
					programs = true;
					parameters = false;
				}
				else if ( strcmp( buf , "-parameters" ) == 0 )
				{				
					parameters = false;
				}
				else if ( strcmp( buf , "-help" ) == 0 )
				{
					post("print options:");
					post("-help \t\tprint this");
					post("-params \tshow the parameter display values ");
					post("-noheader \tdo not display the header");
					return;
				}
			}
			else if ( av[i].a_type == A_FLOAT )
			{
				int p = (int) atom_getfloat( &av[i] );
				if (( p > 0 ) && ( p <=  x->plug->GetNumParams()))
				{
					specific = p - 1;
				}
			}
		}
	 }
	 if ( header )
	 {
		post("VST~ plugin: %s " , x->plug->GetName() );
		post("made by: %s " , x->plug->GetVendorName() );
		post("parameterss %d\naudio: %d in(s)/%d out(s) \nLoaded from library \"%s\".\n",
			x->plug->GetNumParams(),
			x->num_audio_inputs,
			x->num_audio_outputs,
			x->plug->GetDllName());

		post("Flags");
		if ( x->plug->_pEffect->flags & effFlagsHasEditor )
		{
			post("Has editor");
		}
		if ( x->plug->_pEffect->flags & effFlagsCanReplacing )
		{
			post("Can do replacing");
		}
	 }
	 if ( parameters )
	 {
		if ( specific == -1)
		{
			for (i = 0; i < x->plug->GetNumParams(); i++) 
			{
				display_parameter( x , i , params );	
			}
		}
		else
		{
			display_parameter( x , specific , params);	
		}
	 }
	 if( programs )
	 {
		 
		for( int j = 0; j < x->plug->GetNumCategories() ; j++ )
		{
			for( i = 0 ; i < x->plug->GetNumParams() ; i++ )
			{
				char buf[64];
				x->plug->GetProgramName( j , i , buf );
				post("Program %d: %s ", i , buf );
			}
		}
	 }
}

static void display_parameter( t_vst_tilde* x,  int param , bool showparams )
{
	int j = param;
	/* the Steinberg(tm) way... */
	char name[109];
	char display[164];
	float val;
	if (j == 0) 
	{
		post ("Control input/output(s):");
	}
	memset (name, 0, 108);
	memset( display, 0 ,163);
	x->plug->GetParamName( j , name );
	if ( name[0] != NULL )
	{
		if ( showparams )
		{
			x->plug->DescribeValue( j , display );		
			val = x->plug->GetParamValue( j );
			post ("parameter[#%d], \"%s\" value=%f (%s) ", j + 1, name,  val,display);			
		}
		else
		{
			val = x->plug->GetParamValue( j );
			post ("parameter[#%d], \"%s\" value=%f ", j + 1, name,  val);			
		}
	}
}


/**
*	display an editor - currently not implemented 
*/


static void vst_tilde_edit(t_vst_tilde* x)     
{
	AFX_MANAGE_STATE(AfxGetStaticModuleState());	
	if ( x->plug != NULL )
	{
		x->plug->edit();    
	}
	else
	{
		post("No plugin to edit");
	}
}

static void vst_tilde_showparams(t_vst_tilde* x)     
{
	x->plug->SetShowParameters( true);    
}

static void vst_tilde_noshowparams(t_vst_tilde* x)     
{
	x->plug->SetShowParameters( false);    
}



/**
*	set the value of a parameter
*/

static void vst_tilde_param(t_vst_tilde* x , t_float pnum , t_float val)     
{
	if ( ( pnum > 0 ) && ( pnum <= x->plug->GetNumParams() ))
	{		
		int i = (int) pnum - 1;
		char name[9];
		char display[64];
		float xval;
		memset (name, 0, 9);
		memset( display, 0 ,64);
		x->plug->GetParamName( i , name );
		if ( name[0] != NULL )
		{		
			xval = x->plug->GetParamValue( i );
			if ( xval <= 1.0f)
			{
				x->plug->SetParameter( i , val );
				if ( x->plug->ShowParams() )
				{
					display_parameter( x , i , true );
				}
			}		
		}
	}
}

static void vst_tilde_reset (t_vst_tilde* x)
{
    
}

static void vst_tilde_midinote(t_vst_tilde* x , t_float note )     
{
	if ( x->x_vel > 0 )
	{
		x->plug->AddNoteOn( note , x->x_vel );
	}
	else
	{
		x->plug->AddNoteOff( note );
	}
}

#if defined(_LANGUAGE_C_PLUS_PLUS) || defined(__cplusplus)
}
#endif