forked from csound/csoundAPI_examples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample12.py
More file actions
100 lines (78 loc) · 2.77 KB
/
example12.py
File metadata and controls
100 lines (78 loc) · 2.77 KB
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
# Example 12 - Graphical User Interfaces
# Author: Steven Yi <stevenyi@gmail.com>
# 2013.10.28
#
# This example demonstrates a slightly more advanced GUI example.
# It uses a slider to allow setting the value of the frequency that
# the notes initiated by the button will play at.
#
# Note: the actual use of update() here is not thread-safe. In
# real-world usage, we would need to drive Csound from a loop calling
# PerformKsmps to ensure thread-safety. For this example, the updating
# generally works as there are few things demanding computation.
from Tkinter import *
import csnd6
from random import randint, random
###############################
# Our Orchestra for our project
orc = """
sr=44100
ksmps=32
nchnls=2
0dbfs=1
gkpch chnexport "freq", 1
instr 1
kpch port gkpch, 0.01, i(gkpch)
printk .5, gkpch
kenv linsegr 0, .05, 1, .05, .9, .8, 0
aout vco2 p4 * kenv, kpch
aout moogladder aout, 2000, .25
outs aout, aout
endin"""
c = csnd6.Csound() # create an instance of Csound
c.SetOption("-odac") # Set option for Csound
c.SetOption("-m7") # Set option for Csound
c.CompileOrc(orc) # Compile Orchestra from String
c.Start() # When compiling from strings, this call is necessary before doing any performing
perfThread = csnd6.CsoundPerformanceThread(c)
perfThread.Play()
def createChannel(csound, channelName):
chn = csnd6.CsoundMYFLTArray(1)
csound.GetChannelPtr(chn.GetPtr(), channelName,
csnd6.CSOUND_CONTROL_CHANNEL | csnd6.CSOUND_INPUT_CHANNEL)
return chn
class SliderWrapper(object):
def __init__(self, csound, channelName, slider):
self.slider = slider
self.channel = createChannel(csound, channelName)
def update(self):
self.channel.SetValue(0, self.slider.get())
class Application(Frame):
def __init__(self,master=None):
master.title("Csound API GUI Example")
self.items = []
self.notes = []
Frame.__init__(self,master)
self.pack()
self.createUI()
self.master.protocol("WM_DELETE_WINDOW", self.quit)
def createUI(self):
self.size = 600
self.canvas = Canvas(self,height=self.size,width=self.size)
self.canvas.pack()
self.button = Button(self.canvas, text='Play Note', command=self.playNote)
self.button.pack()
self.freqSlider = Scale(self.canvas,from_=80.0, to=600.0,command=self.setFreq,label="Freq")
self.freqSlider.pack()
self.freqUpdater = SliderWrapper(c, "freq", self.freqSlider)
def playNote(self):
perfThread.InputMessage("i1 0 2 .5")
def setFreq(self, val):
print val
self.freqUpdater.update()
def quit(self):
self.master.destroy()
perfThread.Stop()
perfThread.Join()
app = Application(Tk())
app.mainloop()