Skip to content

Instantly share code, notes, and snippets.

@iMostfa
Created May 31, 2022 00:02
Show Gist options
  • Save iMostfa/3e56f6b8ad3997d7a83fd538009a658f to your computer and use it in GitHub Desktop.
Save iMostfa/3e56f6b8ad3997d7a83fd538009a658f to your computer and use it in GitHub Desktop.
import numpy as np
import matplotlib.pyplot as plt
from numpy.fft import fft, ifft
from pydub import AudioSegment
from playsound import playsound
from tkinter import *
from pydub.playback import play
import tkinter as tk
from tkinter import messagebox
songName = "output.mp3"
shiftForwardValue = 2000
shiftBackwardValue = -1000
compressConstant = 1.5
expandConstant = 0.5
lastModifiedSong = AudioSegment.from_mp3(songName)
def updateLastModifiedSong(newSong):
global lastModifiedSong
lastModifiedSong = newSong
def plot2():
soneBefore = AudioSegment.from_mp3(songName)
songAfter = lastModifiedSong
songBeforeSamples = soneBefore.get_array_of_samples()
songAfterSamples = songAfter.get_array_of_samples()
figure, axis = plt.subplots(2, 1)
plt.xlabel("Sample Index")
plt.ylabel("Amplitude")
plt.title("Waveform of Text Audio")
axis[0].plot(songBeforeSamples)
# axis[0].xlabel("Sample Index")
# axis[0].ylabel("Amplitude")
# axis[0].title("Waveform of Text Audio")
axis[1].plot(songAfterSamples)
plt.show()
def plot():
song = AudioSegment.from_mp3(songName)
data= song.get_array_of_samples()
plt.figure()
plt.plot(data)
plt.xlabel("Sample Index")
plt.ylabel("Amplitude")
plt.title("Waveform of Text Audio")
plt.show()
def expandSong():
song = AudioSegment.from_mp3(songName)
newSong = speed_change(song,expandContant)
play(newSong)
updateLastModifiedSong(newSong)
def compressSong():
song = AudioSegment.from_mp3(songName)
newSong = speed_change(song,compressConstant)
play(newSong)
updateLastModifiedSong(newSong)
def speed_change(sound, speed=1.0):
# Manually override the frame_rate. This tells the computer how many
# samples to play per second
sound_with_altered_frame_rate = sound._spawn(sound.raw_data, overrides={
"frame_rate": int(sound.frame_rate * speed)
})
# convert the sound with altered frame rate to a standard frame rate
# so that regular playback programs will work right. They often only
# know how to play audio at standard frame rate (like 44.1k)
return sound_with_altered_frame_rate.set_frame_rate(sound.frame_rate)
def plotFTT(songSample):
spectrum = np.fft.fft(songSample)
frequencies = np.fft.fftfreq(len(spectrum))
plt.plot(frequencies, spectrum)
plt.show()
def changeSongVolume(song, value):
newSong = song + value
updateLastModifiedSong(newSong)
return newSong
def shiftBackward():
song = AudioSegment.from_mp3(songName)
shiftted = song[shiftBackwardValue * -1:]
play(shiftted)
updateLastModifiedSong(shiftted)
def shiftForward():
song = AudioSegment.from_mp3(songName)
silence_duration = shiftForwardValue
silenced_segment = AudioSegment.silent(duration=silence_duration)
combined_segment = silenced_segment + song
play(combined_segment)
updateLastModifiedSong(combined_segment)
return combined_segment
def reverse():
song = AudioSegment.from_mp3(songName)
reversed = song.reverse()
updateLastModifiedSong(reversed)
play(reversed)
def helloCallBack():
messagebox.showinfo( "Hello Python", "Hello World")
def playS():
song = AudioSegment.from_mp3(songName)
play(song)
if __name__ == '__main__':
song = AudioSegment.from_mp3(songName)
# shiftted = shift(song, -2000)
root = Tk()
root.geometry("1000x600")
root.resizable(width=0, height=0)
root.title('Bucy & A')
root.columnconfigure(0, weight=3)
root.columnconfigure(1, weight=3)
root.columnconfigure(2, weight=3)
root.columnconfigure(3, weight=3)
shiftForwardButton = Button(root, text="Shift Forward", command=shiftForward)
shiftForwardButton.grid(column=0, row=0, sticky=tk.W, padx=5, pady=5)
expandButton = Button(root, text="Expand", command=expandSong)
expandButton.grid(column=1, row=3, sticky=tk.W, padx=115, pady=5)
playButton = Button(root, text="play", command= playS)
playButton.grid(column=2, row=3, sticky=tk.W, padx=5, pady=5)
l = Label(root,text= " ")
l.grid(column=2, row=2, sticky=tk.W, padx=5, pady=5)
compressButton = Button(root, text="Compress", command=compressSong)
compressButton.grid(column=3, row=3, sticky=tk.W, padx=115, pady=5)
reverseButton = Button(root, text="Reverse Song", command=reverse)
reverseButton.grid(column=0, row=3, sticky=tk.W, padx=5, pady=5)
shiftBackward = Button(root, text="Shift Backward", command=shiftBackward)
shiftBackward.grid(column=4, row=0, sticky=tk.W)
plotSong = Button(root, text=" Plot Song ", command=plot2)
plotSong.grid(column=4, row=3, sticky=tk.W)
# Code to add widgets will go here...
root.mainloop()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment