-
Notifications
You must be signed in to change notification settings - Fork 4
/
sampler.py
executable file
·232 lines (186 loc) · 7.12 KB
/
sampler.py
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
#!/usr/bin/python
# -*- coding: utf-8 -*-
# Copyright (c) 2010 Hugh Tebby
# Simple Sampler plays a sound when you push its button.
import pygtk
pygtk.require('2.0')
import gtk
import os
from os import *
from signal import SIGTERM
import subprocess
from subprocess import Popen
class SoundPlayer:
def __init__(self, file, time = ''):
self.sound_file = file
self.time = time
def play(self):
if override.get_active():
self.time2 = start_time.get_value()
elif self.time:
self.time2 = float(self.time)/10
else:
self.time2=0
self.freqs_mplayer = ''
for freq in freqs:
self.freqs_mplayer = self.freqs_mplayer + str(freq.get_value())+':'
self.freqs_mplayer = self.freqs_mplayer[0:len(self.freqs_mplayer)-1]
self.sound_process = Popen(['mplayer','-ss',str(self.time2),'-af','equalizer=' + str(self.freqs_mplayer),str(self.sound_file)])
# stop playing
def stop(self):
os.kill(self.sound_process.pid, SIGTERM)
# This class create a gtk.ToggleButton linked to a sound file.
# Send "toggle", it plays the sound. Send "toggle" again, it stops.
class SamplerButton(gtk.ToggleButton):
def __init__(self, file = ''):
filename = os.path.basename(file)
filename = os.path.splitext(filename)
filename_array = filename[0].split('#')
label = filename_array[0]
if len(filename_array) == 2:
time = filename_array[1]
else:
time = ''
label = label.capitalize()
label = label[0:15]
self.sound = SoundPlayer(file, time)
super(SamplerButton,self).__init__(label)
self.connect('toggled', self.toggle)
def play(self):
self.sound.play()
def stop(self):
self.sound.stop()
def toggle(self,widget):
if self.get_active():
self.play()
else:
self.stop()
# 1. Initialize a gtk window
# 2. Add a frame for each subdirectory of 'sounds'
# 3. Add a column for each sub-subdirectory
# 4. Add a button for each sound
class Sampler():
def equalizer_init(self, widget):
for freq in freqs:
freq.set_value(0)
def set_volume(self, widget):
subprocess.Popen( ['aumix','-v',str(self.volume.get_value())] )
def __init__(self):
window = gtk.Window(gtk.WINDOW_TOPLEVEL)
window.set_title("Simple sampler")
window.set_border_width(10)
window.connect('delete_event',gtk.main_quit)
main_panel = gtk.VPaned()
# Add 10 frequency equalizer
equalizer = gtk.HBox(True, 5)
equalizer_init_button = gtk.Button('Init Eq')
equalizer.add(equalizer_init_button)
global freqs
freqs = []
for i in range(0,10):
freqs.append( gtk.VScale(gtk.Adjustment(value=0.0, lower=-10, upper=10, step_incr=0.1)) )
freqs[i].set_inverted(True)
equalizer.add(freqs[i])
equalizer_init_button.connect('clicked',self.equalizer_init)
main_panel.add(equalizer)
bottom_panel = gtk.HPaned()
notebook = gtk.Notebook()
soundsdir = os.listdir('sounds')
soundsdir.sort()
# Add tab for each directory
for directory in soundsdir:
tab_label = gtk.Label(directory)
framebox = gtk.HBox(True, 5)
scrolled_window = gtk.ScrolledWindow(None, None)
scrolled_window.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC)
scrolled_window.shadow_type=(gtk.SHADOW_NONE)
# Add frame for each subdirectory
soundssubdir = os.listdir('sounds/'+directory)
for subdir in soundssubdir:
scrolled_frame = gtk.ScrolledWindow(None, None)
scrolled_frame.set_policy(gtk.POLICY_NEVER, gtk.POLICY_AUTOMATIC)
scrolled_frame.shadow_type=(gtk.SHADOW_NONE)
soundbox = gtk.VButtonBox()
soundbox.set_layout(gtk.BUTTONBOX_START)
soundbox.add(gtk.Label(subdir))
# Add button for each sound in directory
sounds = os.listdir('sounds/'+directory+'/'+subdir)
for sound in sounds:
soundbox.add(SamplerButton('sounds/'+directory+'/'+subdir+'/'+sound))
scrolled_frame.add_with_viewport(soundbox)
framebox.pack_start_defaults(scrolled_frame)
scrolled_window.add_with_viewport(framebox)
notebook.append_page(scrolled_window, tab_label)
bottom_panel.add(notebook)
side_panel = gtk.VBox(True,5)
self.volume = gtk.VScale(gtk.Adjustment(value=75, lower=0, upper=100, step_incr=10))
self.volume.set_inverted(True)
#vol = subprocess.Popen( 'aumix -w'+str(volume.get_value())+' -v'+str(volume.get_value()) )
vol = self.set_volume
side_panel.add(self.volume)
self.volume.connect('value-changed',self.set_volume)
start_time_box = gtk.HBox(False,2)
global start_time
start_time = gtk.SpinButton(gtk.Adjustment(value=0, lower=0, upper=200, step_incr=0.1),digits=1)
global override
override= gtk.CheckButton()
start_time_box.add(start_time)
start_time_box.add(override)
side_panel.add(start_time_box)
bottom_panel.add(side_panel)
main_panel.add(bottom_panel)
window.add(main_panel)
window.show_all()
def main():
gtk.main()
if __name__ == "__main__":
sampler = Sampler()
main()
# setting volume in Windows ?
#import ctypes
#mixerSetControlDetails = (
#ctypes.windll.winmm.mixerSetControlDetails)
#mixerGetControlDetails = (
#ctypes.windll.winmm.mixerGetControlDetailsA)
## Some constants
#MIXER_OBJECTF_MIXER = 0 # mmsystem.h
#VOLUME_CONTROL_ID = 0 # Same on all machines?
#SPEAKER_LINE_FADER_ID = 1 # "Identifier <identifier> in OID value does not resolve to a positive integer"
#MINIMUM_VOLUME = 0 # fader control (MSDN Library)
#MAXIMUM_VOLUME = 65535 # fader control (MSDN Library)
#class MIXERCONTROLDETAILS(ctypes.Structure):
#_pack_ = 1
#_fields_ = [('cbStruct', ctypes.c_ulong),
#('dwControlID', ctypes.c_ulong),
#('cChannels', ctypes.c_ulong),
#('cMultipleItems', ctypes.c_ulong),
#('cbDetails', ctypes.c_ulong),
#('paDetails', ctypes.POINTER(ctypes.c_ulong))]
#def setVolume(volume):
#"""Set the speaker volume on the 'Volume Control' mixer"""
#if not (MINIMUM_VOLUME <= volume <= MAXIMUM_VOLUME):
#raise ValueError, "Volume out of range"
#cd = MIXERCONTROLDETAILS(ctypes.sizeof(MIXERCONTROLDETAILS),
#SPEAKER_LINE_FADER_ID,
#1, 0,
#ctypes.sizeof(ctypes.c_ulong),
#ctypes.pointer(ctypes.c_ulong(volume)))
#ret = mixerSetControlDetails(VOLUME_CONTROL_ID,
#ctypes.byref(cd),
#MIXER_OBJECTF_MIXER)
#if ret != 0:
#print WindowsError, "Error %d while setting volume" % ret
#ret = mixerGetControlDetails(VOLUME_CONTROL_ID,
#ctypes.byref(cd),
#MIXER_OBJECTF_MIXER)
#if ret != 0:
#print WindowsError, "Error %d while setting volume" % ret
#else:
#print 'cbStruct', cd.cbStruct
#print 'dwControlID', cd.dwControlID
#print 'cChannels', cd.cChannels
#print 'cMultipleItems', cd.cMultipleItems
#print 'cbDetails', cd.cbDetails
#print 'paDetails', cd.paDetails.contents
#return
#setVolume((2**16-1)/2)