-
Notifications
You must be signed in to change notification settings - Fork 1
/
stream.py
97 lines (80 loc) · 2.08 KB
/
stream.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
import serial
import pygame
from time import sleep
import pyaudio, sys, socket
import wave
port = 5001
ip = "192.168.43.33"
chunk = 512
FORMAT = pyaudio.paInt16
CHANNELS = 1
RATE = 48000
p = pyaudio.PyAudio()
stream = p.open(format = FORMAT, channels = CHANNELS, rate = RATE, output=True, output_device_index=0, frames_per_buffer = chunk)
#Create a socket connection for connecting to the server:
client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client_socket.connect((ip, port))
# save stream audio
audio_buffer = []
cnt=0
file_order=0
pygame.mixer.init()
def save_wave_file(filename,data):
wf = wave.open(filename,'wb')
wf.setnchannels(CHANNELS)
wf.setsampwidth(2)
wf.setframerate(RATE)
wf.writeframes(b''.join(data))
wf.close()
def press_volume():
global vol
for i in range(5):
if c2 == VOL[i]:
vol = i
m.set_volume(vol/6)
break
#print('vol')
print(vol)
START = bytes.fromhex('F2')
END = bytes.fromhex('2F')
CENTER = b'\x84'
PREV = b'\x81'
FORW = b'\x82'
VOL = [b'\x01', b'\x03',b'\x07',b'\x0f',b'\x1f',b'\x1f']
c = ''
c_prev = ''
vol = 6
is_play = 1
pygame.mixer.init()
m = pygame.mixer.music
m.set_volume(vol/6)
ser = serial.Serial("/dev/ttyUSB0",9600)
ser.reset_input_buffer()
while True:
#Recieve data from the server:
data = client_socket.recv(1024)
#stream.write(data,chunk) #play stream sound
audio_buffer.append(data)
cnt+=1
#print(cnt)
if cnt >= 100:
file_name ='stream_music/' 'stream' + str(file_order) + '.wav'
save_wave_file(file_name, audio_buffer)
audio_buffer = []
cnt=0
print("Now playing: "+file_name)
pygame.mixer.music.load(file_name)
m.play()
if file_order>=10:
file_order=0
else:
file_order+=1
if ser.inWaiting() > 3:
first = ser.read()
if first == START:
temp = ser.read()
c_prev = c
c = temp
c2 = ser.read()
if c != '' and c2 != '':
press_volume()