-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapc_random.py
More file actions
103 lines (85 loc) · 2.75 KB
/
Copy pathapc_random.py
File metadata and controls
103 lines (85 loc) · 2.75 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
99
100
101
102
103
import mido
import time
import pygame
import os
import json
from random import randint
# some variables
config_file = "config.json"
# initialise the midi input and output
inport = mido.open_input("APC MINI")
outport = mido.open_output("APC MINI")
# initialise pygame for audio
pygame.mixer.init(frequency=44100)
pygame.mixer.set_num_channels(99)
# button class for midi io and audio playback
class button:
def __init__(self, note_number, audio_file, state=False):
self.note_number = note_number
if isinstance(state, bool):
self.state = state
else:
self.state = False
self.audio_file = audio_file
self.channel = self.note_number % 8
def set_state(self, state):
if isinstance(state, bool):
self.state = state
self.midi_output()
# self.audio_output()
def toggle_state(self):
self.state = not self.state
self.midi_output()
# self.audio_output()
def get_state(self):
return self.state
def output_state(self):
pass
def midi_output(self):
if self.state:
velocity = 127
else:
velocity = 0
out_msg = mido.Message("note_on", note=self.note_number, velocity=velocity)
outport.send(out_msg)
def audio_output(self):
if self.state:
self.audio = pygame.mixer.Sound(self.audio_file)
pygame.mixer.Channel(self.note_number).play(self.audio)
else:
try:
self.audio.fadeout(500)
except:
pass
# import files from json config file #
config_path = os.path.join(os.getcwd(), 'config', config_file)
with open(config_path, 'r') as f:
config_data = f.read()
# print(config_data)
config_info = json.loads(config_data)
for page in config_info['PageData']:
if page['PageNumber'] == 1:
samples_list = []
for idx in range(8):
for sample in page["Samples"][str(idx)]:
samples_list.append(sample)
# initialise buttons
buttons = []
for idx in range(63):
file_path = os.path.join(os.getcwd(), "audio", samples_list[idx])
buttons.append(button(idx, file_path, False))
for idx in range(63, 99):
file_path = os.path.join(os.getcwd(), "audio", "dummy.wav")
buttons.append(button(idx, file_path, False))
channels = [0 for _ in range(9)]
prev_state = [0 for _ in range(99)]
current_state = [0 for _ in range(99)]
loop_counter = 0
while True:
loop_counter = randint(0, 18446744073709551615)
display_string = bin(loop_counter)[2:]
display_string = "{:0>64}".format(display_string)
# display update loop
for idx in range(64):
buttons[idx].set_state(bool(int(display_string[idx])))
time.sleep(0.5)