-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcode.py
More file actions
261 lines (200 loc) · 8.36 KB
/
code.py
File metadata and controls
261 lines (200 loc) · 8.36 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
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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
import os
import tkinter as tk
from tkinter import messagebox, filedialog
from pygame import mixer
import wave
import threading
import pyaudio
import cv2
import numpy as np
# Initialize mixer for sound playback
mixer.init()
drum_clap = mixer.Sound('batterrm.wav') # Replace with your sound file
drum_snare = mixer.Sound('button-2.ogg') # Replace with your sound file
# Audio recording parameters
CHUNK = 1024
FORMAT = pyaudio.paInt16
CHANNELS = 1
RATE = 44100
# Global variables
recording = False
frames = []
recorded_sounds = []
audio_thread = None
camera = None
downloads_folder = os.path.expanduser("~/Downloads")
audio_directory = os.path.join(downloads_folder, "recorded_audios")
# Ensure the directory exists
if not os.path.exists(audio_directory):
os.makedirs(audio_directory)
def state_machine(sumation, sound):
if sumation > Hatt_thickness[0] * Hatt_thickness[1] * 0.8:
if sound == 1:
drum_clap.play()
if recording:
recorded_sounds.append('drum_clap')
elif sound == 2:
drum_snare.play()
if recording:
recorded_sounds.append('drum_snare')
def ROI_analysis(frame, sound):
hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
mask = cv2.inRange(hsv, blueLower, blueUpper)
sumation = np.sum(mask)
state_machine(sumation, sound)
return mask
# Define the HSV range for blue color (you can adjust these values based on your needs)
blueLower = np.array([100, 150, 0], dtype="uint8") # Lower range of blue
blueUpper = np.array([140, 255, 255], dtype="uint8") # Upper range of blue
def run_digital_drums():
global camera, Hatt, Snare, Hatt_thickness, Snare_thickness
camera = cv2.VideoCapture(0)
ret, frame = camera.read()
H, W = frame.shape[:2]
# Define ROI positions and dimensions
Hatt = cv2.resize(cv2.imread('Hatt.png'), (200, 100), interpolation=cv2.INTER_CUBIC)
Snare = cv2.resize(cv2.imread('Snare.png'), (200, 100), interpolation=cv2.INTER_CUBIC)
Hatt_center = [W * 2 // 8, H * 6 // 8]
Snare_center = [W * 6 // 8, H * 6 // 8]
Hatt_thickness = [200, 100]
Snare_thickness = [200, 100]
Hatt_top = [Hatt_center[0] - Hatt_thickness[0] // 2, Hatt_center[1] - Hatt_thickness[1] // 2]
Hatt_btm = [Hatt_center[0] + Hatt_thickness[0] // 2, Hatt_center[1] + Hatt_thickness[1] // 2]
Snare_top = [Snare_center[0] - Snare_thickness[0] // 2, Snare_center[1] - Snare_thickness[1] // 2]
Snare_btm = [Snare_center[0] + Snare_thickness[0] // 2, Snare_center[1] + Snare_thickness[1] // 2]
while True:
ret, frame = camera.read()
frame = cv2.flip(frame, 1)
if not ret:
break
snare_ROI = frame[Snare_top[1]:Snare_btm[1], Snare_top[0]:Snare_btm[0]]
ROI_analysis(snare_ROI, 1)
hatt_ROI = frame[Hatt_top[1]:Hatt_btm[1], Hatt_top[0]:Hatt_btm[0]]
ROI_analysis(hatt_ROI, 2)
# Display drums and labels
frame[Snare_top[1]:Snare_btm[1], Snare_top[0]:Snare_btm[0]] = cv2.addWeighted(
Snare, 1, frame[Snare_top[1]:Snare_btm[1], Snare_top[0]:Snare_btm[0]], 1, 0
)
frame[Hatt_top[1]:Hatt_btm[1], Hatt_top[0]:Hatt_btm[0]] = cv2.addWeighted(
Hatt, 1, frame[Hatt_top[1]:Hatt_btm[1], Hatt_top[0]:Hatt_btm[0]], 1, 0
)
cv2.putText(frame, 'Digital Drums', (10, 30), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 2)
cv2.imshow('Output', frame)
if cv2.waitKey(1) & 0xFF == ord("q"):
break
camera.release()
cv2.destroyAllWindows()
def audio_recording():
global frames, recording
p = pyaudio.PyAudio()
stream = p.open(format=FORMAT, channels=CHANNELS, rate=RATE, input=True, frames_per_buffer=CHUNK)
while recording:
data = stream.read(CHUNK)
frames.append(data)
stream.stop_stream()
stream.close()
p.terminate()
def start_digital_drums():
threading.Thread(target=run_digital_drums).start()
def record_digital_drums():
global recording, audio_thread, frames
recording = True
frames = []
audio_thread = threading.Thread(target=audio_recording)
audio_thread.start()
start_digital_drums()
def stop_recording():
global recording
recording = False
if audio_thread:
audio_thread.join()
messagebox.showinfo("Recording Stopped", "Recorded sounds: " + ", ".join(recorded_sounds))
def save_audio():
if not frames:
messagebox.showwarning("No Audio", "No audio to save.")
return
filename = filedialog.asksaveasfilename(defaultextension=".wav", filetypes=[("WAV files", "*.wav")])
if filename:
with wave.open(filename, 'wb') as wf:
wf.setnchannels(CHANNELS)
wf.setsampwidth(pyaudio.PyAudio().get_sample_size(FORMAT))
wf.setframerate(RATE)
wf.writeframes(b''.join(frames))
# Save the audio in the designated directory
saved_filename = os.path.basename(filename)
os.rename(filename, os.path.join(audio_directory, saved_filename))
messagebox.showinfo("Audio Saved", f"Audio saved as {saved_filename}")
def list_saved_files():
# List all saved files in the directory
saved_files = os.listdir(audio_directory)
saved_files = [f for f in saved_files if f.endswith(".wav")]
if not saved_files:
messagebox.showinfo("No Files", "No recorded files found.")
return
# Clear the listbox and populate it with the saved files
listbox.delete(0, tk.END) # Clear the listbox
for file in saved_files:
listbox.insert(tk.END, file)
def delete_selected_files():
selected_files = listbox.curselection() # Get the indices of selected files
if not selected_files:
messagebox.showinfo("No Files Selected", "Please select a file to delete.")
return
# Ask the user for confirmation
confirmation = messagebox.askyesno("Delete Files", f"Are you sure you want to delete the selected file(s)?")
if confirmation:
for index in selected_files:
file_name = listbox.get(index) # Get file name from listbox
file_path = os.path.join(audio_directory, file_name)
try:
os.remove(file_path)
listbox.delete(index) # Remove from listbox
messagebox.showinfo("File Deleted", f"File {file_name} deleted successfully.")
except Exception as e:
messagebox.showerror("Error Deleting File", f"Failed to delete {file_name}: {e}")
def play_audio():
selected_file = listbox.curselection()
if not selected_file:
messagebox.showinfo("No File Selected", "Please select a file to play.")
return
filename = listbox.get(selected_file[0]) # Get the selected file name
file_path = os.path.join(audio_directory, filename)
try:
mixer.music.load(file_path)
mixer.music.play()
except Exception as e:
messagebox.showerror("Error Playing Audio", f"An error occurred: {e}")
def exit_application():
global recording, audio_thread, camera
recording = False # Stop the recording
if audio_thread and audio_thread.is_alive():
audio_thread.join() # Wait for audio thread to finish
if camera is not None:
camera.release() # Release camera resources
cv2.destroyAllWindows() # Close any OpenCV windows
root.quit() # Exit the Tkinter application immediately
# Set up Tkinter window
root = tk.Tk()
root.title("Digital Drums")
root.geometry("600x400")
# Buttons
start_button = tk.Button(root, text="Start Code", command=start_digital_drums)
start_button.pack(pady=5)
record_button = tk.Button(root, text="Start Recording", command=record_digital_drums)
record_button.pack(pady=5)
stop_button = tk.Button(root, text="Stop Recording", command=stop_recording)
stop_button.pack(pady=5)
save_button = tk.Button(root, text="Save Recording", command=save_audio)
save_button.pack(pady=5)
list_button = tk.Button(root, text="List Saved Files", command=list_saved_files)
list_button.pack(pady=5)
delete_button = tk.Button(root, text="Delete Selected Files", command=delete_selected_files)
delete_button.pack(pady=5)
play_button = tk.Button(root, text="Play Selected Audio", command=play_audio)
play_button.pack(pady=5)
exit_button = tk.Button(root, text="Exit", command=exit_application)
exit_button.pack(pady=5)
# Listbox to display saved audio files
listbox = tk.Listbox(root, height=10, width=50)
listbox.pack(pady=10)
root.mainloop()