-
Notifications
You must be signed in to change notification settings - Fork 1
/
functions.py
160 lines (135 loc) · 5.94 KB
/
functions.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
import pygame
from pybass import *
import math
pygame.font.init()
font = pygame.font.Font('coders_crux.ttf', 16)
class AudioItem(object):
def __init__(self, screen, x, y, w, h, color, filename, volume):
self.file = filename
self.volume = volume
self.x = int(x)
self.y = int(y)
self.w = int(w)
self.h = int(h)
self.volume_changed = False
self.name = filename[8:]
self.color = color
self.block = pygame.Rect([self.x,self.y,self.w,self.h])
self.filePlayerHandle = BASS_StreamCreateFile(False, bytes(filename,'utf-8'), 0, 0, 0)
self.set_volume(self.volume)
self.screen = screen
# BASS_ChannelSetFX(self.filePlayerHandle,BASS_FX_DX8_GARGLE,1)
# BASS_ChannelSetFX(self.filePlayerHandle,BASS_FX_DX8_FLANGER,1)
def draw(self, x, y, w, h):
self.x = int(math.floor(x))
self.y = int(math.floor(y))
self.w = int(math.floor(w))
self.h = int(math.floor(h))
self.block = pygame.Rect([self.x,self.y,self.w,self.h])
# if self.block.collidepoint(mpos[0], mpos[1]):
# self.color = (43,43,43)
# else:
# self.color = (33,33,33)
# self.block2 = pygame.Rect([self.x,self.y,self.w + 10,self.h + 10])
if self.volume_changed is True:
drawText(self.screen, self.x + self.w / 2 - 12, self.y - 10, str(float("{0:.2f}".format(self.volume))), 8)
pygame.draw.rect(self.screen, self.color, self.block, 0)
pygame.draw.rect(self.screen, (55, 55, 55), self.block, 1)
drawText(self.screen, self.x + self.w / 2 - 22, self.y + self.h / 2 - 4, self.name, 8)
def play(self, playback):
if playback is False:
pass
else:
# filePlayerHandle = BASS_StreamCreateFile(False, b'kick.wav', 0, 0, 0)
# BASS_ChannelSetAttribute(self.filePlayerHandle, BASS_ATTRIB_VOL, self.volume)
BASS_ChannelPlay(self.filePlayerHandle, False)
# BASS_ChannelSlideAttribute(self.filePlayerHandle, BASS_ATTRIB_VOL, 1, 50)
# BASS_ChannelSlideAttribute(self.filePlayerHandle, BASS_ATTRIB_VOL, -1, 60)
# print(self.get_length_seconds())
def get_length_seconds(self):
len = BASS_ChannelGetLength(self.filePlayerHandle, BASS_POS_BYTE)
return BASS_ChannelBytes2Seconds(self.filePlayerHandle, len)
def set_sample(self, sample_path):
self.filePlayerHandle = BASS_StreamCreateFile(False, bytes(sample_path,'utf-8'), 0, 0, 0)
def set_volume(self, volume):
if volume < 0.1:
self.volume = 0.0
if volume > 1.0:
self.volume = 1.0
BASS_ChannelSetAttribute(self.filePlayerHandle, BASS_ATTRIB_VOL, self.volume)
self.volume_changed = True
# ---------------------------------------------------------------------
def drawText(screen, x, y, text="text", size=16):
# mpos = pygame.mouse.get_pos()
# text = str(mpos)
size = font.size(text)
fg = 255, 255, 255
wincolor = 40, 40, 90
ren = font.render(text, 1, fg, 255)
screen.blit(ren, (x, y))
# ---------------------------------------------------------------------
# depends on line spacing -1
def checkSnap(audioitems, main_lines, selected_item, n=0):
if not len(audioitems) == 0:
# print('snap')
try:
for j in range(0, len(main_lines.lines)):
if audioitems[selected_item].x >= main_lines.lines[j][0] and audioitems[selected_item].x <= main_lines.lines[j][0]+main_lines.snap_thresh :
audioitems[selected_item].x = main_lines.lines[j][0]
except IndexError as e:
print("index error (checksnap)", e)
# ---------------------------------------------------------------------
def loadproject(audioitems,screen,project):
f = open(project, 'r')
data = f.readlines()
if len(data) == 0:
pass
else:
for x in range(0, len(data)):
d = data[x].rstrip('\n').split(';')
#print(d[0])
audioitems.append(AudioItem(screen, int(d[0]),int(d[1]),int(d[2]),int(d[3]),(33,33,33),d[4],float(d[5])))
# ---------------------------------------------------------------------
def saveproject(audioitems):
f = open('project.txt', 'w')
for x in range(0, len(audioitems)):
ai = audioitems
f.write(str(ai[x].x) + ';')
f.write(str(ai[x].y) + ';')
f.write(str(ai[x].w) + ';')
f.write(str(ai[x].h) + ';')
f.write(str(ai[x].file) + ';')
f.write(str(ai[x].volume) + '\n')
# ---------------------------------------------------------------------
def keypress_manager(current_instrument):
if pygame.key.get_pressed()[pygame.K_1] != 0:
current_instrument = "./samples/kick.wav"
if pygame.key.get_pressed()[pygame.K_2] is not 0:
current_instrument = "samples\\sd1.wav"
if pygame.key.get_pressed()[pygame.K_3] is not 0:
current_instrument = "samples\\sd2.wav"
if pygame.key.get_pressed()[pygame.K_4] is not 0:
current_instrument = "samples\\hh.wav"
if pygame.key.get_pressed()[pygame.K_5] is not 0:
current_instrument = "samples\\ohh.wav"
if pygame.key.get_pressed()[pygame.K_6] is not 0:
current_instrument = "samples\\clap.wav"
if pygame.key.get_pressed()[pygame.K_7] is not 0:
current_instrument = "samples\\cymbal.wav"
if pygame.key.get_pressed()[pygame.K_8] is not 0:
current_instrument = "samples\\cb.wav"
if pygame.key.get_pressed()[pygame.K_9] is not 0:
current_instrument = "samples\\rs.wav"
if pygame.key.get_pressed()[pygame.K_0:] is not 0:
current_instrument = "samples\\hc.wav"
if pygame.key.get_pressed()[pygame.K_EQUALS] is not 0: # + speed
if bpm >= 999:
bpm=999
else:
bpm+=1
if pygame.key.get_pressed()[pygame.K_MINUS] is not 0: # - speed
if bpm <= 10:
bpm=10
else:
bpm-=1
return current_instrument