-
Notifications
You must be signed in to change notification settings - Fork 0
/
play music21.py
292 lines (224 loc) · 10.2 KB
/
play music21.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
from __future__ import print_function
import Tkinter
import tempfile
import os
from tkFileDialog import askopenfilename , asksaveasfilename
import ttk
import tkMessageBox
from Tkconstants import *
import random
import pyaudio
import wave
#import librosa
import math
import IPython.display as ipd
import pandas as pd
import numpy as np
from Tkinter import *
file='C:/Users/chaitu/Desktop/songs/'
#############################################################################
#This code has been taken from https://gist.github.com/sloria/5693955 by sloria
r=Tkinter.Tk()
song=StringVar()
label=Label(r,text="Hii there! How are you doing ? Wanna listen some music?",fg="black",font='times 36')
label.pack()
search=Entry(r,textvariable=song,bg="beige",fg="purple",font='times 24')
search.pack()
songname=song.get()+'.mp3'
def songs():
class Recorder(object):
'''A recorder class for recording audio to a WAV file.
Records in mono by default.
'''
def __init__(self, channels=1, rate=44100, frames_per_buffer=1024):
self.channels = channels
self.rate = rate
self.frames_per_buffer = frames_per_buffer
def open1(self, fname, mode='wb'):
return RecordingFile(fname, mode, self.channels, self.rate,
self.frames_per_buffer)
class RecordingFile(object):
def __init__(self, fname, mode, channels,
rate, frames_per_buffer):
self.fname = fname
self.mode = mode
self.channels = channels
self.rate = rate
self.frames_per_buffer = frames_per_buffer
self._pa = pyaudio.PyAudio()
self.wavefile = self._prepare_file(self.fname, self.mode)
self._stream = None
def __enter__(self):
return self
def __exit__(self, exception, value, traceback):
self.close()
def record(self, duration):
# Use a stream with no callback function in blocking mode
self._stream = self._pa.open(format=pyaudio.paInt16,
channels=self.channels,
rate=self.rate,
input=True,
frames_per_buffer=self.frames_per_buffer)
for _ in range(int(self.rate / self.frames_per_buffer * duration)):
audio = self._stream.read(self.frames_per_buffer)
self.wavefile.writeframes(audio)
return None
def start_recording(self):
# Use a stream with a callback in non-blocking mode
self._stream = self._pa.open(format=pyaudio.paInt16,
channels=self.channels,
rate=self.rate,
input=True,
frames_per_buffer=self.frames_per_buffer,
stream_callback=self.get_callback())
self._stream.start_stream()
return self
def stop_recording(self):
self._stream.stop_stream()
return self
def get_callback(self):
def callback(in_data, frame_count, time_info, status):
self.wavefile.writeframes(in_data)
return in_data, pyaudio.paContinue
return callback
def close(self):
self._stream.close()
self._pa.terminate()
self.wavefile.close()
def _prepare_file(self, fsongname, mode='wb'):
wavefile = wave.open(fname, mode)
wavefile.setnchannels(self.channels)
wavefile.setsampwidth(self._pa.get_sample_size(pyaudio.paInt16))
wavefile.setframerate(self.rate)
return wavefile
#############################################################################
banglakey=Tkinter.Tk()
banglakey.title('Music player 1.3')
class dummypygame:
def play(self): pass
class mixer:
True
def get_init():
True
class Sound(file):
class play:pass
class quit:
pass
def load_sound(file):
print ('Warning, no sound')
pygame.mixer = None
class init:
pass
class error: False
import pygame
pygame.init()
nam=Tkinter.StringVar()
name=''
sound=''
pause=Tkinter.BooleanVar()
pause.set(False)
def u78(event=None):
global name
name= songname
if name!='':
load_sound(name)
nam.set(name)
pause.set(False)
if pygame.mixer and not pygame.mixer.get_init():
print ('Warning, no sound')
pygame.mixer = None
def helpe(event=None):
tkMessageBox.showinfo("Help','1.To open a file click on open and choose a file.To play a file click on play.Only .wav,.mp3,.ogg and .mid files can be played. Pause feature is available in .mp3,.ogg formats.To record sound go to file menu then Record option then give filename press save then start.Record and the press stop to stop recording")
def About():
tkMessageBox.showinfo('About','This is Music player 1.3')
def Record():
banglakey.record=Tkinter.Toplevel()
banglakey.record.title("Record Sound")
banglakey.record.transient(banglakey)
record=Tkinter.BooleanVar()
rec = Recorder(channels=2)
f = asksaveasfilename(defaultextension=".wav",filetypes=[("sound files", ".wav")])
recfile2 = rec.open1(f, 'wb')
def start(event=None):
p61.start()
recfile2.start_recording()
def stop(event=None):
recfile2.stop_recording()
recfile2.close()
p61.stop()
banglakey.record.destroy()
b61=Tkinter.Button(banglakey.record, text =u'Start', command = start,relief="groove",bg="#0060FF",fg="white",font=("arial",16))
b62=Tkinter.Button(banglakey.record, text =u'Stop', command = stop,relief="groove",bg="#0060FF",fg="white",font=("arial",16))
p61=ttk.Progressbar(banglakey.record,orient='horizontal',mode='indeterminate',length=200)
b61.pack(side=Tkinter.TOP)
b62.pack(side=Tkinter.TOP)
p61.pack(side=Tkinter.TOP)
banglakey.record.mainloop()
menu = Tkinter.Menu(banglakey)
banglakey.config(menu=menu)
filemenu = Tkinter.Menu(menu)
menu.add_cascade(label="File", menu=filemenu)
filemenu.add_command(label="Open...", command=u78)
filemenu.add_command(label="Record...", command=Record)
filemenu.add_separator()
filemenu.add_command(label="Exit",activebackground='red', command=banglakey.destroy)
helpmenu = Tkinter.Menu(menu)
menu.add_cascade(label="Help", menu=helpmenu)
helpmenu.add_command(label="About...", command=About)
helpmenu.add_command(label="Help file",activebackground='purple', command=helpe)
class dummysound:#class object to be used in place of pygame.mixer.Sound if not available
def play(self): pass
def mixer(self): pass
def load_sound(name):
if not pygame.mixer: return dummysound()
try:
name = os.path.join(name)
print (name)
pygame.mixer.music.load(name)
except pygame.error:
print ('Warning, unable to load, %s' % name)
return dummysound()
b51=Tkinter.Button(banglakey, text =u'OPEN', command = u78,relief="groove",bg="#0060FF",fg="white",font=("arial",16))
l12=Tkinter.Label(banglakey, textvariable= nam ,font=("times new roman",11),relief="flat",borderwidth=1,bg="#109070",fg="white")
p51=ttk.Progressbar(orient='horizontal',mode='indeterminate',length=200)
def update_timeText():
if not pygame.mixer.music.get_busy():
p51.stop()
b54.config(state='disabled')
banglakey.after(1000, update_timeText)
def u77(event=None):
#load the sound effects
if pygame.mixer:
pygame.mixer.init()
b54.config(state='normal')
p51.start()
if pause.get()==False:
pygame.mixer.music.play(1)
update_timeText()
elif pause.get()==True:
pygame.mixer.music.unpause()
p51.start()
b52=Tkinter.Button(banglakey, text =u'PLAY', command = u77,relief="groove",bg="#0060FF",fg="white",font=("arial",16))
def u76(event=None):
pygame.mixer.music.stop()
p51.stop()
pause.set(False)
b53=Tkinter.Button(banglakey, text =u'STOP', command = u76,relief="groove",bg="#0060FF",fg="white",font=("arial",16))
def u78(event=None):
pygame.mixer.music.pause()
pause.set(True)
p51.stop()
b54.config(state='disabled')
b54=Tkinter.Button(banglakey, text =u'PAUSE',state='disabled', command = u78,relief="groove",bg="#0060FF",fg="white",font=("arial",16))
#nextsong=Tkinter.Button(banglakey, text=u'NEXT',command=nextsong,relief="groove",bg="#0060FF",fg="white",font=("arial",16))
b51.pack(side=Tkinter.TOP)
b52.pack(side=Tkinter.TOP)
b54.pack(side=Tkinter.TOP)
b53.pack(side=Tkinter.TOP)
p51.pack(side=Tkinter.TOP)
l12.pack(side=Tkinter.TOP)
banglakey.mainloop()
pygame.quit()
searchb=Button(r,text="search",command=songs)
searchb.pack()
r.mainloop()