File: MCLDGetenvUGen.cpp

package info (click to toggle)
supercollider-sc3-plugins 3.7.1~repack-2
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 14,332 kB
  • ctags: 11,704
  • sloc: cpp: 140,180; lisp: 14,746; ansic: 2,133; xml: 86; makefile: 82; haskell: 21; sh: 8
file content (108 lines) | stat: -rw-r--r-- 2,272 bytes parent folder | download
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
/**
Getenv UGen (c) Dan Stowell 2007
Grab environment variables (numeric ones please!) for use in synth graphs.
*/

#include "SC_PlugIn.h"
#include <sys/time.h>
#include <ctime>

static InterfaceTable *ft;

struct Getenv : public Unit
{
	int m_key;
	char* m_key_string;
	float m_value;
};

struct Clockmus : public Unit
{
	timeval tp;
};

extern "C"
{
	void load(InterfaceTable *inTable);

	void Getenv_Ctor(Getenv* unit);
	void Getenv_next(Getenv *unit, int inNumSamples);
	void Getenv_Dtor(Getenv* unit);

	void Clockmus_Ctor(Clockmus* unit);
	void Clockmus_next(Clockmus *unit, int inNumSamples);
};

//////////////////////////////////////////////////////////////////

void Getenv_Ctor(Getenv* unit)
{

	SETCALC(Getenv_next);

	unit->m_key = IN0(0); // number of chars in the key string
	// Now allocate and load the key into a null-terminated string
	unit->m_key_string = (char*)RTAlloc(unit->mWorld, ((int)unit->m_key + 1) * sizeof(char));
	for(int i = 0; i < (int)unit->m_key; i++){
		unit->m_key_string[i] = (char)IN0(2+i);
		};
	unit->m_key_string[(int)unit->m_key] = '\0';

	int verbosity = unit->mWorld->mVerbosity;

	if(verbosity >= 1){
		Print("Getenv: key is %s\n", unit->m_key_string);
	}

	char *envstr = getenv(unit->m_key_string);

	if(envstr==NULL){
		if(verbosity >= 0){
			Print("Getenv warning: string is NULL (env var %s not set)\n", unit->m_key_string);
		}
		unit->m_value = IN0(1); // defaultval
	}else{
		unit->m_value = atof(envstr);
		if(verbosity >= 1){
			Print("Getenv: string value is %s\n", envstr);
			//Print("Getenv: float value is %g\n", unit->m_value);
		}
	}

	// calculate one sample of output.
	Getenv_next(unit, 1);
}

void Getenv_next(Getenv *unit, int inNumSamples)
{
	OUT0(0) = unit->m_value;
}

void Getenv_Dtor(Getenv* unit)
{
	RTFree(unit->mWorld, unit->m_key_string);
}

//////////////////////////////////////////////////////////////////

void Clockmus_Ctor(Clockmus* unit)
{
	SETCALC(Clockmus_next);
	Clockmus_next(unit, 1);
}

void Clockmus_next(Clockmus *unit, int inNumSamples)
{
	gettimeofday(&(unit->tp), NULL);
	OUT0(0) = (float) unit->tp.tv_usec;
}

//////////////////////////////////////////////////////////////////

PluginLoad(MCLDGetEnv)
{
	ft = inTable;

	DefineDtorUnit(Getenv);
	DefineSimpleUnit(Clockmus);
}