Last active
July 30, 2024 12:29
-
-
Save AstroEngineeer/a4ed69c4062d9f137e7ee810b2f7ce9b to your computer and use it in GitHub Desktop.
Stream songs to multiple clients from a central server.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import pyaudio | |
import socket | |
import pickle | |
audio = pyaudio.PyAudio() | |
stream = audio.open(format=pyaudio.paInt16, channels=2, rate=44100, output=True, frames_per_buffer=1024) | |
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) | |
s.connect(("192.168.48.128", 5000)) | |
data = s.recv(1024) | |
listofmp3 = pickle.loads(data) | |
def playSong(): | |
try: | |
while True: | |
data = s.recv(1024) | |
stream.write(data) | |
except KeyboardInterrupt: | |
print("Paused!!!") | |
option = input('Press "R" to Resume / Press "Q" to Exit: ') | |
if "r" == option.lower(): | |
playSong() | |
elif "q" == option.lower(): | |
s.close() | |
return | |
else: | |
print("Enter a valid option!!") | |
playSong() | |
print("List of songs:") | |
i = 0 | |
for mp3 in listofmp3: | |
print("\t{} --> {}".format(i+1,mp3)) | |
i = i + 1 | |
s.send(bytes(listofmp3[((int)(input("Enter the song number want to play: ")))-1],"utf-8")) | |
playSong() | |
print('Disconnected') | |
stream.close() | |
audio.terminate() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import socket | |
import wave | |
from pydub import AudioSegment | |
import os | |
import fnmatch | |
import pickle | |
import _thread as trd | |
# reading list of mp3 files | |
listofmp3 = [] | |
listOfFiles = os.listdir('songs') | |
pattern = "*.mp3" | |
for entry in listOfFiles: | |
if fnmatch.fnmatch(entry, pattern): | |
listofmp3.append(entry) | |
# setting up socket | |
serversocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) | |
serversocket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) | |
serversocket.bind(("192.168.48.128", 5000)) | |
serversocket.listen(5) | |
def playMusic(clientsocket): | |
clientsocket.send(pickle.dumps(listofmp3)) | |
req_song = clientsocket.recv(1024).decode("utf-8") | |
# converting mp3 to wav | |
sound = AudioSegment.from_mp3("songs//"+req_song) | |
sound.export("songs//{}.wav".format(req_song), format="wav") | |
# opening wav file | |
wf = wave.open("songs//{}.wav".format(req_song), 'rb') | |
print("Playing {} for {}".format(req_song,address)) | |
data = wf.readframes(1024) | |
try: | |
while data != '': | |
data = wf.readframes(1024) | |
clientsocket.send(data) | |
except ConnectionResetError: | |
print("{} disconnected.".format(address)) | |
clientsocket.close() | |
trd.exit | |
try: | |
while True: | |
(clientsocket, address) = serversocket.accept() | |
print("Connected to ", address) | |
trd.start_new_thread(playMusic, (clientsocket,)) | |
except KeyboardInterrupt: | |
serversocket.close() | |
pass |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment