-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnew.py
150 lines (136 loc) · 5.24 KB
/
new.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
import os
import subprocess as sp
import wave
import numpy as np
import pygame
import time
from time import sleep
from pygame.locals import *
from scipy.fftpack import dct
from sense_hat import SenseHat
import random
import wave
import json
import simpleaudio
def formatwave(music_path):
head_tail = os.path.split(music_path)
file_name = head_tail[1]
filename_raw, extension = os.path.splitext(file_name)
# check if the file is in wav form
if extension != ".wav":
try:
fnull = open(os.devnull, "w")
pieq_tmp = os.path.expanduser("~") + "/.pieq_tmp/"
wav_path = pieq_tmp + filename_raw + ".wav"
if not os.path.isfile(wav_path):
print("Decompressing...")
sp.call(["mkdir", "-p", pieq_tmp])
sp.call(["ffmpeg", "-i", music_path, wav_path], stdout=fnull, stderr=sp.STDOUT)
except FileExistsError:
print('failed to convert to wav file')
else:
wav_path = music_path
file_name = '.'.join(file_name.split('.')[:-1])
return wav_path, file_name
x_axis_color_dict_parcels = {6:tuple((218, 217, 218)),
7:tuple((218, 217, 218)),
0:tuple((42, 75, 114)),
1:tuple((42, 75, 114)),
2:tuple((114, 85, 62)),
3:tuple((114, 85, 62)),
4:tuple((133, 152, 172)),
5:tuple((133, 152, 172))}
with open(os.path.join('.','playlist_utils','cache_jsons','pathuri_mapping.json')) as file:
pathuri_mapping = json.load(file)
music_path = '/home/pi/Desktop/PARCELS_MUSICBOX_SY/raspberrypi_sensehat_music/Music/Parcels/Tieduprightnow.wav'
wav_path, file_name = formatwave(music_path)
track_uri = pathuri_mapping.get(file_name+'.wav',None)
if track_uri is not None:
with open ("/home/pi/Desktop/PARCELS_MUSICBOX_SY/raspberrypi_sensehat_music/playlist_utils/cache_jsons/audio_features/{}.json".format(track_uri)) as f:
audio_features = json.load(f)
print('filename', file_name+'.wav')
print('track_uri',track_uri)
with open('tmp_features.json', 'w') as file:
json.dump(audio_features,file)
background = (0,0,0)
R = (198, 30, 74) #raspberrytips red
W = (255, 255, 255)
def wait(delay):
end_time = time.time() + delay
while end_time > time.time():
continue
def get_features(wav_path):
feature_dict = {}
f = wave.open(wav_path, 'rb')
params = f.getparams()
nchannels, sampwidth, framerate, nframes = params[:4]
feature_dict['nchannels'] = nchannels
feature_dict['sampwidth'] = sampwidth
feature_dict['framerate'] = framerate
feature_dict['nframes'] = nframes
str_data = f.readframes(nframes)
f.close()
wave_data = np.frombuffer(str_data, dtype=np.short)
wave_data.shape = -1, 2
feature_dict['wave_data'] = wave_data.T
return feature_dict
def play_metronome(wav_path):
volume=0.3
sensehat.clear()
feature_dict = get_features(wav_path)
nframes = feature_dict.get('nframes')
track_uri = pathuri_mapping.get(file_name+'.wav',None)
if audio_features:
bpm = round(audio_features.get('track').get('tempo'))
metronome_on = True
print(float(bpm), "bpm")
delay = 60.0/float(bpm)
count = 0
beat = 0
mode = 4
multiple = 8
eo_fadein = audio_features.get('track').get('end_of_fade_in')
else:
eo_fadein = 0.0
pygame.mixer.init(frequency=feature_dict['framerate'], buffer=64)
pygame.mixer.set_num_channels(5)
pygame.mixer.music.load(wav_path)
pygame.mixer.music.set_volume(0.5)
sensehat.show_message("Playing: {}".format(file_name), 0.05, W, background)
pygame.mixer.music.play()
status = 'playing'
start_time = 0.0
if eo_fadein!= 0.0:
# print('fade in',eo_fadein)
wait(audio_features.get('beats')[0].get('start')+0.005)
else:
wait(0.005)
#metronome_sound = pygame.mixer.Sound('./metronome/metronome.wav')
#metronome_up = pygame.mixer.Sound('./metronome/metronomeup.wav')
while metronome_on:
start = time.time()
events = sensehat.stick.get_events()
if len(events)!=0 and events[-1].action == 'released':
print('stopped')
metronome_on = False
break
#if len(events)!=0 and events[-1].direction == 'middle' and events[-1].action == 'released':
# metronome_on = False
# increment count after every wait and beat after ever 4 counts
count += 1
if count > mode:
count = 1
beat += 1
# set metronome audio according to beat count
#wave_obj = simpleaudio.WaveObject.from_wave_file('./metronome/metronome.wav')
#channel1.play(metronome_sound)
#if count == 1:
# channel2.play(metronome_up)
wave_obj = simpleaudio.WaveObject.from_wave_file('./metronome/metronome.wav')
if count == 1:
wave_obj = simpleaudio.WaveObject.from_wave_file('./metronome/metronomeup.wav')
exec_time = time.time()-start
play_obj = wave_obj.play()
wait(delay-exec_time-0.006)
sensehat = SenseHat()
play_metronome(wav_path=music_path)