DEV Community

Cover image for Simple AI Sound Mixer in Python
MyExamCloud
MyExamCloud

Posted on

Simple AI Sound Mixer in Python

If you’re looking for a fun and practical Raspberry Pi project, creating an AI-powered sound mixer using Python, Tkinter, and Pygame is a great choice. This project replaces the traditional soundpad with an AI-based approach that generates random sounds. It’s a perfect tool for experimenting with unique soundscapes, adding AI-generated effects to your music, or just having fun with sound synthesis. Best of all, it’s simple to set up and run.

What Are We Using?

We’re using a Raspberry Pi (or any Linux-based system) with Python and a few essential libraries. The interface is built with Tkinter, and sound generation is handled by a simple AI model using NumPy to create random waveforms, which are then played back using Pygame.

How Does It Work?

Instead of loading pre-existing sound files, this project generates synthetic audio using AI. The AI model uses random waveform synthesis to create unique, unpredictable soundscapes.

Required Libraries

To get started, install the required libraries if they are not already installed:

pip install pygame numpy tkinter
Enter fullscreen mode Exit fullscreen mode

Python Script for AI Sound Mixer

import pygame
import tkinter as tk
from tkinter import Button, messagebox
import numpy as np
import wave
import os

# Initialize pygame mixer
pygame.mixer.init(frequency=44100, size=-16, channels=2, buffer=512)
pygame.mixer.set_num_channels(12)

# Function to generate a random sound

def generate_random_sound(duration=1.0, sample_rate=44100):
    t = np.linspace(0, duration, int(sample_rate * duration), False)
    wave_data = np.sin(2 * np.pi * np.random.uniform(100, 1000) * t) * 32767
    wave_data = wave_data.astype(np.int16)

    file_name = "temp_sound.wav"
    with wave.open(file_name, "w") as wf:
        wf.setnchannels(1)
        wf.setsampwidth(2)
        wf.setframerate(sample_rate)
        wf.writeframes(wave_data.tobytes())

    return file_name

# Function to play a generated sound
def play_sound():
    sound_file = generate_random_sound()
    sound = pygame.mixer.Sound(sound_file)
    channel = pygame.mixer.find_channel()
    if channel:
        channel.play(sound)
    os.remove(sound_file)  # Clean up after playing

# Function to confirm exit
def confirm_exit():
    if messagebox.askyesno("Exit", "Are you sure you want to exit?"):
        root.destroy()

# Create GUI
root = tk.Tk()
root.title("AI Sound Mixer")
root.geometry("600x400")

# Create button for generating sounds
play_btn = Button(root, text="Generate Sound", font=("Arial", 18), width=20, height=5, command=play_sound)
play_btn.pack(pady=20)

# Exit button
exit_btn = Button(root, text="Exit", font=("Arial", 18), width=20, height=5, command=confirm_exit)
exit_btn.pack(pady=20)

# Run the application
root.mainloop()
Enter fullscreen mode Exit fullscreen mode

How It Works

  • The AI model generates a random waveform with a frequency between 100Hz and 1000Hz.
  • The waveform is saved as a temporary WAV file.
  • The sound is played using Pygame’s mixer.
  • The temporary sound file is deleted after playback to keep things clean.

This project is an exciting way to experiment with AI-generated sounds while building a fun and interactive Python application. Try modifying the waveform generation to create different types of sounds!

Top comments (0)