1
2
3global {
4  outchannels 2;
5}
6
7
8//
9// instr otone
10// plays a sine wave
11// and its octave
12//
13// uses two user-defined
14// opcodes
15
16
17instr otone (freq)
18
19{
20
21  // variable declaration
22
23  ivar a;
24  asig init;
25  asig s[2], out[2];
26
27  //***************
28  // runs at i-rate
29  //***************
30
31  a = coeff(freq);
32
33  //***************
34  // runs at a-rate
35  //***************
36
37  if (init == 0)
38    {
39      init = 1;
40      s[0] = 0.5;
41    }
42
43  out = update(s,a);
44
45  output(out);
46
47}
48
49//
50// opcode definition
51//
52// name: update
53// rate: a-rate
54// width: 2
55//
56// expects to be passed,
57// BY REFERENCE, the
58// state array for the
59// oscillator. opcode
60// updates state array,
61// and returns a stereo
62// signal, one channel
63// is the fundamental
64// tone, the other is
65// the first harmonic
66//
67
68aopcode update(asig s[2],
69	       ivar a)
70
71{
72  // scales harmonic
73
74  asig w;
75
76  s[0] = s[0] - a*s[1];
77  s[1] = s[1] + a*s[0];
78
79  w = 2.0;
80
81  return(s[1], w*s[1]*s[0]);
82
83}
84
85
86//
87// opcode definition
88//
89// name: coeff
90// rate: polymorphic
91// width: 1
92//
93//
94// parameter hertz is the
95// desired frequency of the
96// sine wave oscillator.
97// returns the coefficient
98// value that produces that
99// frequency
100//
101
102opcode coeff(xsig hertz)
103
104{
105  xsig rval;
106
107  rval = 2*sin(3.1415927*hertz/s_rate);
108  return(rval);
109}
110
111