AI MUSIC WORLD
🎯 Goal of the Project
Build a Python software that:
- Uses AI (speech recognition) to receive voice commands like “play”, “pause”, “next”, or “stop”.
- Controls music playback using a media library.
- Can be remotely controlled over a network (via socket or HTTP).
🧰 Technologies Used
speech_recognition
: for voice commands (AI input).pygame
: for music playback.socket
orFlask
: for network control (we’ll start with Flask for simplicity).threading
: to run music and server simultaneously.
🧠 AI-Powered Music Controller with Network Access (Python Code)
Let’s write a simple version using Flask + Speech Recognition + Pygame:
pythonCopyEditimport os
import threading
from flask import Flask, request, jsonify
import pygame
import speech_recognition as sr
# Initialize Flask app
app = Flask(__name__)
# Initialize Pygame Mixer
pygame.mixer.init()
# Load your music folder
MUSIC_FOLDER = "music"
music_files = [os.path.join(MUSIC_FOLDER, f) for f in os.listdir(MUSIC_FOLDER) if f.endswith('.mp3')]
current_track = 0
# Play music
def play_music(index):
pygame.mixer.music.load(music_files[index])
pygame.mixer.music.play()
# Speech recognition function
def listen_voice_command():
recognizer = sr.Recognizer()
with sr.Microphone() as source:
print("Say a command (play/pause/stop/next):")
audio = recognizer.listen(source)
try:
command = recognizer.recognize_google(audio).lower()
print("You said:", command)
process_command(command)
except sr.UnknownValueError:
print("Could not understand audio.")
except sr.RequestError:
print("Error with the recognition service.")
# Command processor
def process_command(cmd):
global current_track
if "play" in cmd:
play_music(current_track)
elif "pause" in cmd:
pygame.mixer.music.pause()
elif "unpause" in cmd or "resume" in cmd:
pygame.mixer.music.unpause()
elif "stop" in cmd:
pygame.mixer.music.stop()
elif "next" in cmd:
current_track = (current_track + 1) % len(music_files)
play_music(current_track)
# Flask endpoints for network control
@app.route("/control", methods=["POST"])
def control():
data = request.json
command = data.get("command", "").lower()
process_command(command)
return jsonify({"status": "Command executed", "command": command})
@app.route("/listen", methods=["GET"])
def voice_control():
threading.Thread(target=listen_voice_command).start()
return jsonify({"status": "Listening started"})
# Run both Flask server and music loop
def start_server():
app.run(port=5000, debug=False)
if __name__ == "__main__":
print("Starting AI Music Controller...")
threading.Thread(target=start_server).start()
print("You can control music using voice (type /listen in browser) or network (POST to /control).")
📁 Folder Structure
bashCopyEditproject_folder/
│
├── app.py # <-- (This code)
├── music/
│ ├── song1.mp3
│ ├── song2.mp3
🧪 How to Use It
- Put your MP3 files in the
music/
folder. - Run the script:
python app.py
- Open browser at
http://localhost:5000/listen
→ Speak commands like “play”, “pause”, “next” - Use API for remote control (example with
curl
or Postman):
bashCopyEditcurl -X POST http://localhost:5000/control -H "Content-Type: application/json" -d '{"command": "ne