-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFakeFlacChecker7-final-build.py
744 lines (601 loc) · 33.1 KB
/
FakeFlacChecker7-final-build.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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
#This program is used to spot fake lossless audio files through use of spectograms, you can play these and their difference. save the lossy compression if needed. Also you can convert audio into mp3 with different bitrate
#To use this in python u need to install following libraries: Kivy, pydub, scipy, numpy, matplotlib
#
import io
import os
import time
import gc
import atexit
#import multiprocessing as mp
#Graphics:
import tkinter as tk
from tkinter import filedialog
from kivy.app import App
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.label import Label
from kivy.uix.button import Button
from kivy.uix.gridlayout import GridLayout
from kivy.core.window import Window
from kivy.core.image import Image as CoreImage
from kivy.uix.image import Image
from kivy.uix.checkbox import CheckBox
from kivy.uix.dropdown import DropDown
from kivy.clock import Clock
#AudioHandling:
import matplotlib.pyplot as plt
import scipy.io.wavfile as wavfile
from pydub import AudioSegment
import tempfile
import pydub
import numpy as np
#For playing audio by default browser
import subprocess
import platform
import webbrowser
#from memory_profiler import profile (To see memory consumption)
def is_wav_to_memory(file_name: str, mp3_and_back: bool, bitrate:str, limit_to_45sec:bool, save_mp3:bool):
""" Opens selected audio, if needed covnerts to wav in memory.
Parameters
----------
file_name : str
Name of audio file
mp3_and_back : bool
True converts the audio to mp3 (to selected bitrate) and then to wav (fake lossless version)
bitrate: str
sets destination bitrate
limit_to_45sec : bool
takes only 45seconds of audio file
save_mp3 : bool
True saves the mp3 as temp
Returns
-------
Fs : int
Number of samples per second. (common sampling rate for audio signals is 44100 Hz)
aud : NumPy array
Contains the audio data
"""
global temp_file_path
def convert_to_memory(file_name):
""" Opens selected audio, if needed covnerts to wav in memory. """
if file_name.endswith(".wav"):
with open(file_name, "rb") as f:
return f.read()
else:
audio = AudioSegment.from_file(file_name, format=file_name.split(".")[-1])
with io.BytesIO() as f:
audio.export(f, format="wav")
f.seek(0)
return f.read()
def convert_to_memory_45s(file_name):
""" Opens 45 seconds of selected audio, if needed covnerts to wav in memory. """
if file_name.endswith(".wav"):
audio = AudioSegment.from_file(file_name, format="wav")[:45000]
with io.BytesIO() as f:
audio.export(f, format="wav")
f.seek(0)
return f.read()
else:
audio = AudioSegment.from_file(file_name, format=file_name.split(".")[-1])[:45000]
with io.BytesIO() as f:
audio.export(f, format="wav")
f.seek(0)
return f.read()
if limit_to_45sec:
aud_data = convert_to_memory_45s(file_name)
else:
aud_data = convert_to_memory(file_name)
if mp3_and_back:
def convert_to_mp3(aud_data):
""" converts the audio to mp3 (to selected bitrate) and then to wav (fake lossless version) """
audio = AudioSegment.from_file(io.BytesIO(aud_data), format="wav")
with io.BytesIO() as f:
audio.export(f, format="mp3", bitrate=bitrate)
f.seek(0)
return f.read()
def convert_to_wav(aud_data):
audio = AudioSegment.from_file(io.BytesIO(aud_data), format="mp3")
new_name = "Fake-(mp3-to-wav) " + os.path.basename(file_name)
global temp_file_path
if save_mp3:
with tempfile.NamedTemporaryFile(prefix=new_name, suffix=".mp3", delete=False) as d:
d.write(aud_data)
temp_file_path = d.name
print (temp_file_path, "Temporary mp3-to-wav")
with io.BytesIO() as f:
audio.export(f, format="wav")
f.seek(0)
return f.read()
def to_wav_and_mp3_and_back(file_name):
mp3_data = convert_to_mp3(aud_data)
audio_data = convert_to_wav(mp3_data)
return audio_data
aud_data= to_wav_and_mp3_and_back(aud_data)
Fs, aud = wavfile.read(io.BytesIO(aud_data))
# select left channel only
if aud.shape[1] == 2:
aud = aud[:,0]
return Fs, aud
def play_audio_by_default_browser(file_path: str):
""" Opens default media browser for selected audio, if it fails opens in web browser
Parameters
----------
file_path : str
Name of audio file
"""
operating_system = platform.system()
success = False
if operating_system == "Windows":
try:
os.startfile(file_path)
success = True
except:
pass
elif operating_system == "Linux":
try:
subprocess.run(["xdg-open", file_path], check=True)
success = True
except:
pass
elif operating_system == "Darwin":
try:
subprocess.run(["open", "-a", "Music", file_path], check=True)
success = True
except:
pass
if not success:
webbrowser.open(file_path)
def difference_between_audio_files(file_name:str, limt_to_45_sec:bool):
""" Takes audio file and its lossy conversion and combines them, combined result saves as temp file
Parameters
----------
file_name : str
Name of audio file
limt_to_45_sec: bool
True takes only 45s of audio
"""
global temp_file_path
print(temp_file_path)
print(file_name, "File name")
new_name = "Difference between High-Low-" + os.path.basename(file_name)
file_format = file_name.split(".")[-1]
audio = None
if file_format == "wav":
audio = pydub.AudioSegment.from_wav(file_name)
elif file_format == "mp3":
audio = pydub.AudioSegment.from_mp3(file_name)
elif file_format == "flac":
audio = pydub.AudioSegment.from_file(file_name, format="flac")
if limt_to_45_sec:
# Get the length of the audio in milliseconds
duration = audio.duration_seconds * 1000
# Select the first 45 seconds of the audio
audio = audio[:min(45000, duration)]
mp3 = pydub.AudioSegment.from_mp3(temp_file_path)
# Convert the MP3 to a numpy array
mp3_array = np.array(mp3.get_array_of_samples())
# Invert the MP3
mp3_array = -mp3_array
# Convert the numpy array back to an audio segment
mp3_inverted = pydub.AudioSegment(
mp3_array.tobytes(),
frame_rate=mp3.frame_rate,
sample_width=mp3.sample_width,
channels=mp3.channels
)
combined = audio.overlay(mp3_inverted)
with tempfile.NamedTemporaryFile(prefix=new_name,suffix=".mp3", delete=False) as temp:
combined.export(temp.name, format="mp3", bitrate="320k")
play_audio_by_default_browser(temp.name)
print(temp.name)
global temp_file_path_of_difference
temp_file_path_of_difference = temp.name
print(temp_file_path_of_difference)
class MyGridLayout(GridLayout):
""" Devides GUI layout into 3 separate collumns and initializes widgets"""
def __init__(self, **kwargs):
super(MyGridLayout, self).__init__(**kwargs)
self.cols=3
self.insideleft = FloatLayout(size_hint=(.7, 1))
self.insidecenter = FloatLayout(size_hint=(1, 1))
self.insideright = FloatLayout(size_hint=(1, 1))
self.image = Image()
self.image_fake = Image()
self.widgetje = False
self.file_name =""
global temp_file_path_of_difference, temp_file_path
temp_file_path_of_difference = ""
temp_file_path = ""
#Name:
self.insideleft.add_widget(Label(text='Lossless audio checker', size_hint=(1,.1),pos_hint={'x':0, 'y':.9}, bold='text', font_size='30sp'))
#What the program is doing:
self.doing_text = "Choose option"
self.doing_label = Label(text=self.doing_text, size_hint=(.1, .1), pos_hint={'x': .0, 'y': 0}, color=(1,0,1,1))
self.insideright.add_widget(self.doing_label)
#Main audio compare button:
self.Compare = Button(text="Compare audio \nwith its fake high resolution",size_hint=(.6,.1),pos_hint={'x':.1, 'y':.8})
self.Compare.bind(on_press=self.ChooseSong)
self.insideleft.add_widget(self.Compare)
#For faster computation (Limit to 45s of audio data):
self.limit_to_45 = CheckBox(size_hint=(.6, .1), pos_hint={'x': .2, 'y': .7})
self.limit_to_45.bind(active=self.update_limit_to_45_variable)
self.insideleft.add_widget(self.limit_to_45)
self.limit_to_45_variable = False
self.label = Label(text="Limit audio to 45s", size_hint=(.4, .1), pos_hint={'x': .1, 'y': .7})
self.insideleft.add_widget(self.label)
#Choose bitrate for second audio: (Compare different bitrates)
self.bitrate_dropdown = DropDown(size_hint=(.6, .1), pos_hint={'x': .2, 'y': .6})
bitrate_options = ["320k", "196k", "160k", "128k", "96k", "64k"]
self.bitrate= "320k"
for option in bitrate_options:
btn = Button(text=option, size_hint_y=None, height=44)
btn.bind(on_release=lambda btn: self.bitrate_dropdown.select(btn.text))
self.bitrate_dropdown.add_widget(btn)
self.bitrate_dropdown.bind(on_select=self.update_selected_bitrate)
self.dropdown_button = Button(text='Choose Bitrate\n Default=320k', size_hint=(.6, .1), pos_hint={'x': .1, 'y': .6})
self.dropdown_button.bind(on_release=self.bitrate_dropdown.open)
self.bitrate_dropdown.bind(on_select=lambda instance, x: setattr(self.dropdown_button, 'text', "Selected bitrate:" + x))
self.insideleft.add_widget(self.dropdown_button)
self.insideleft.add_widget(Label(text="To check lossless audio do not change\ndefault bitrate", size_hint=(.4,.1), pos_hint={'x': .3, 'y': .5}))
#Spectogram for one file
self.bspecto = Button(text="Open spectogram for one file", size_hint=(.6,.1),pos_hint={'x':.1, 'y':.4})
self.bspecto.bind(on_press=self.OnlySpecButton)
self.insideleft.add_widget(self.bspecto)
#How to use:
self.help = Button(text="How it works", size_hint=(.6,.1),pos_hint={'x':.1, 'y':.3})
self.help.bind(on_press=self.show_help)
self.insideleft.add_widget(self.help)
#For folder, fastest computation, shows only most highest frequencies found form 14kHz:
#self.only_max_freq_button = Button(text="Calculate only\nhighest max frequencies", size_hint=(.6,.1), pos_hint={'x':.1, 'y':.2})
#self.only_max_freq_button.bind(on_press=self.only_max_freq)
#self.insideleft.add_widget(self.only_max_freq_button)
#Play High res audio:
self.playbutton = Button(background_normal='Icons-and-pictures/icons8-play-button-circled-100.png', size_hint=(None, None), pos_hint={'x': 0, 'y': .8})
self.playbutton.bind(on_press=self.play_song)
self.audio_label = Label(text="Your audio file:", size_hint=(None, None), pos_hint={'x': 0.3, 'y': 0.8})
#Play Fake high res audio: (image from https://icons8.com/icon/YJ5CCqdcOBs2/play-button-circled)
self.playbutton_fake = Button(background_normal='Icons-and-pictures/icons8-play-button-circled-100.png', size_hint=(None, None), pos_hint={'x': 0, 'y': .8},)
self.playbutton_fake.bind(on_press=self.play_fake_song)
self.audio_label_fake = Label(text="Converted file:", size_hint=(None, None), pos_hint={'x': 0.2, 'y': 0.8})
#Save icon for low-quality audio
self.save_icon = Button(background_normal='Icons-and-pictures/icons8-save-90.png', size_hint=(None, None), pos_hint={'x': 0.5, 'y': .8})
self.save_icon.bind(on_press=self.save_mp3)
#Play difference between high and low quality aduio:
self.play_difference_button = Button(background_normal='Icons-and-pictures/icons8-play-button-circled-100.png', size_hint=(None, None), pos_hint={'x': 0, 'y': .1},)
self.play_difference_button.bind(on_press=self.play_difference)
self.play_difference_label = Label(text="Play the differnce between High-Low res audio:", size_hint=(None, None), pos_hint={'x': .4, 'y': .1})
self.add_widget(self.insideleft)
self.add_widget(self.insidecenter)
self.add_widget(self.insideright)
def update_doing_text(self, instance):
""" updates doing text (self.doing_text needs to be changed before calling) """
self.doing_label.text=str(self.doing_text)
def play_difference(self, instance):
""" Calls function to calculate difference between audio files and then plays it by default browser """
self.doing_text = "Opening the difference audio, please wait"
self.update_doing_text(self.doing_text)
def calculation_callback(dt):
global temp_file_path_of_difference
if temp_file_path_of_difference != "" and os.path.exists(temp_file_path_of_difference):
play_audio_by_default_browser(temp_file_path_of_difference)
print(temp_file_path_of_difference)
else:
difference_between_audio_files(self.file_name, self.limit_to_45_variable)
Clock.schedule_once(calculation_callback, 0)
def update_selected_bitrate(self, instance, value):
""" Updates bitrate if new is selected """
if value:
self.bitrate = value
if self.bitrate == "320k":
self.doing_text = "Choose audio file for lossles check"
self.update_doing_text(self.doing_text)
else:
self.doing_text = "Selected bitrate:" + self.bitrate + "\nOpen new file, which will be converted to this bitrate"
self.update_doing_text(self.doing_text)
def play_song(self, instance):
"""Button function= Plays selected audio """
if self.file_name != "":
self.doing_text = "Opening original audio"
self.update_doing_text(self.doing_text)
def calculation_callback(dt):
play_audio_by_default_browser(self.file_name)
Clock.schedule_once(calculation_callback, 0)
def update_limit_to_45_variable(self, checkbox, value):
"""Checkbox func = Updates limit_to_45 var to True or False"""
self.limit_to_45_variable = value
if value:
self.doing_text = "Limiting audio to 45seconds"
self.update_doing_text(self.doing_text)
else:
self.doing_text = "Whole audio selected"
self.update_doing_text(self.doing_text)
def play_fake_song(self, instance):
"""Button func = plays the fake lossless version of audio """
global temp_file_path
print(temp_file_path)
if temp_file_path != "":
self.doing_text = "Opening fake \"Lossless\" audio\nSometimes takes a little longer"
self.update_doing_text(self.doing_text)
def calculation_callback(dt):
play_audio_by_default_browser(temp_file_path)
Clock.schedule_once(calculation_callback, 0)
def save_mp3(self, instance):
""" Button func = saves the lossy audio file """
def save_mp3(temp_file_path):
self.doing_text = "Saving lower-quiality audio"
self.update_doing_text(self.doing_text)
def calculation_callback(dt):
root = tk.Tk()
root.withdraw()
file_path = filedialog.asksaveasfilename(defaultextension=".mp3", filetypes=[("MP3 files", "*.mp3")])
if file_path:
audio = AudioSegment.from_mp3(temp_file_path)
audio.export(file_path, format="mp3", bitrate=self.bitrate)
self.doing_text = os.path.basename(file_path) + " Succesfully Saved"
self.update_doing_text(self.doing_text)
else:
self.doing_text = "Error while saving audio"
self.update_doing_text(self.doing_text)
Clock.schedule_once(calculation_callback, 0)
save_mp3(temp_file_path)
def ChooseSong(self, instance):
""" Button func= takes original audio, converts it into fake lossy version and spectograms """
global temp_file_path, temp_file_path_of_difference
root = tk.Tk()
root.withdraw()
file_name = filedialog.askopenfilename(filetypes = (("Audio Files", "*.wav;*.flac;*.mp3;"), ("All Files", "*.*")))
if file_name == "":
self.doing_text = "No file was selected"
self.update_doing_text(self.doing_text)
if file_name != "":
self.file_name = file_name
print(self.file_name)
self.doing_text = "Opening: " + os.path.basename(self.file_name) +" and calculating spectograms\nPlease wait"
self.update_doing_text(self.doing_text)
if self.widgetje:
self.insidecenter.remove_widget(self.image)
self.insideright.remove_widget(self.image_fake)
self.insidecenter.remove_widget(self.max_freq_label)
self.insideright.remove_widget(self.max_freq_label_fake)
if temp_file_path != "" and os.path.exists(temp_file_path):
os.remove(temp_file_path)
if temp_file_path_of_difference != "" and os.path.exists(temp_file_path_of_difference):
os.remove(temp_file_path_of_difference)
if not self.widgetje:
self.insidecenter.clear_widgets()
self.insideright.clear_widgets()
self.insidecenter.add_widget(self.playbutton)
self.insidecenter.add_widget(self.audio_label)
self.insideright.add_widget(self.playbutton_fake)
self.insideright.add_widget(self.save_icon)
self.insideright.add_widget(self.audio_label_fake)
self.insidecenter.add_widget(self.play_difference_button)
self.insideleft.add_widget(self.play_difference_label)
self.insideright.add_widget(self.doing_label)
#Kivy doesnt support piclikng objects, you would need to reconstruct this way differently
""" spectogram_high = mp.Process(target=self.load_spectogram_high, args=(self.file_name,))
spectogram_low = mp.Process(target=self.load_spectogram_low, args=(self.file_name,))
spectogram_high.start()
spectogram_low.start() """
#
def calculation_callback(dt):
start_time = time.time()
self.load_spectogram_high(self.file_name)
self.load_spectogram_low(self.file_name)
end_time = time.time()
duration = end_time - start_time
self.doing_text = "Done, duration: " + str(int(duration)) + " seconds\nChoose an option or open new file"
self.update_doing_text(self.doing_text)
Clock.schedule_once(calculation_callback, 0)
gc.collect()
#@profile
def load_spectogram_high(self, file_name):
"""Calculates the spectogram of selected audio in memory and then displays it, calculates most highest frequencies from 14000freq """
#Load audio to memory:
Fs, aud = is_wav_to_memory(file_name, False, self.bitrate, self.limit_to_45_variable, False)
#Plot spectogram:
powerSpectrum, frequenciesFound, time, imageAxis = plt.specgram(aud, Fs=Fs, cmap='nipy_spectral') #'hot' #
#Show most used highest frequencies:
if max(frequenciesFound) > 14000:
n_frequencies, n_times = powerSpectrum.shape
frequency_range = np.where((frequenciesFound >= 14000))
max_frequencies = [frequenciesFound[np.argmax(powerSpectrum[frequency_range, i])] for i in range(n_times)]
print("Max frequency:", max(max_frequencies) + 14000)
max_freq_text = "Most used highest frequencies (from 14KHz+): " + str(max(max_frequencies) + 14000) + "\nThis value should be higher"
self.max_freq_label = Label(text=max_freq_text,pos_hint={'x':.3, 'y':.23}, size_hint=(.1, .1))
self.insidecenter.add_widget(self.max_freq_label)
else:
self.max_freq_label = Label(text="Too small frequencies found",pos_hint={'x':.3, 'y':.23}, size_hint=(.1, .1))
self.insidecenter.add_widget(self.max_freq_label)
plt.title(os.path.basename(file_name))
plt.xlabel('Time [s]')
plt.ylabel('Frequency [Hz]')
#Save spectogram to memory and refresh memory buffer:
buf=io.BytesIO()
buf.flush()
plt.savefig(buf, format="png", transparent=True)
plt.close()
buf.seek(0)
im = CoreImage(buf, ext='png')
self.image = Image(texture=CoreImage(im).texture, pos_hint={'x':0, 'y':.1}, size_hint=(1, 1))
self.insidecenter.add_widget(self.image)
#Clear buffer: (Otherwise creates plot in older plot)
buf.seek(0)
buf.truncate(0)
self.widgetje = True
def load_spectogram_low(self, file_name):
"""Calculates the spectogram of fake lossless audio in memory and then displays it, calculates most highest frequencies from 14000freq """
#Show most used highest frequencies:
def show_highest_freq(powerSpectrum, frequenciesFound, frequency_range_from):
n_frequencies, n_times = powerSpectrum.shape
frequency_range = np.where((frequenciesFound >= frequency_range_from))
max_frequencies = [frequenciesFound[np.argmax(powerSpectrum[frequency_range, i])] for i in range(n_times)]
print("Max frequency:", max(max_frequencies) + frequency_range_from)
max_freq_text = "Most used highest frequencies (from " + str(frequency_range_from)[0:2]+ "KHz+): " + str(max(max_frequencies) + frequency_range_from) + "\nThis value should be lower"
self.max_freq_label_fake = Label(text=max_freq_text,pos_hint={'x':.35, 'y':.23}, size_hint=(.1, .1))
self.insideright.add_widget(self.max_freq_label_fake)
#Load audio data to memory:
Fs, aud = is_wav_to_memory(file_name, True, self.bitrate, self.limit_to_45_variable, True)
#Plot spectogram and save it to memory:
powerSpectrum, frequenciesFound, time, imageAxis = plt.specgram(aud, Fs=Fs, cmap='nipy_spectral') #'hot' #
#if different bitrate is selected, the spectrum needs to be calculated from lower highest freq.
frequency_range_from = 14000
if max(frequenciesFound) > 14000:
if self.bitrate == "320k":
show_highest_freq(powerSpectrum, frequenciesFound, frequency_range_from)
elif self.bitrate == "196k":
frequency_range_from = 12000
show_highest_freq(powerSpectrum, frequenciesFound, frequency_range_from)
elif self.bitrate =="160k":
frequency_range_from = 10000
show_highest_freq(powerSpectrum, frequenciesFound, frequency_range_from)
else:
pass
else:
self.max_freq_label_fake = Label(text="Too small frequencies found",pos_hint={'x':.35, 'y':.23}, size_hint=(.1, .1))
self.insideright.add_widget(self.max_freq_label_fake)
new_name = "Fake-(mp3-to-wav) " + os.path.basename(file_name)
plt.title(new_name)
plt.xlabel('Time [s]')
plt.ylabel('Frequency [Hz]')
buf=io.BytesIO()
buf.flush()
plt.savefig(buf, format="png", transparent=True)
plt.close()
#Add image on screen:
buf.seek(0)
im = CoreImage(buf, ext='png')
self.image_fake = Image(texture=CoreImage(im).texture, pos_hint={'x':0, 'y':0.1}, size_hint=(1, 1))
self.insideright.add_widget(self.image_fake)
#Clear memory:
buf.seek(0)
buf.truncate(0)
def only_max_freq(self, instance):
""" Takes audio files from a folder and passes it one by one to only_max_frequencies_value """
root = tk.Tk()
root.withdraw()
directory = filedialog.askdirectory()
if directory:
self.file_names = []
for file_name in os.listdir(directory):
if file_name.endswith((".wav", ".flac", ".mp3")):
self.file_names.append(os.path.join(directory, file_name))
print(self.file_names)
for file_name in self.file_names:
self.only_max_frequencies_value(file_name)
def only_max_frequencies_value(self, file_name):
""" Calculates only the highest frequencies (>14Khz) and prints to console """
Fs, aud = is_wav_to_memory(file_name, False, self.bitrate, self.limit_to_45_variable, False)
#Plot spectogram and save it to memory:
powerSpectrum, frequenciesFound, time, imageAxis = plt.specgram(aud, Fs=Fs) #'hot' #
plt.close()
#Show most used highest frequencies:
n_frequencies, n_times = powerSpectrum.shape
frequency_range = np.where((frequenciesFound >= 14000))
max_frequencies = [frequenciesFound[np.argmax(powerSpectrum[frequency_range, i])] for i in range(n_times)]
print("Max frequency of original file", os.path.basename(file_name), ":", max(max_frequencies) + 14000)
Fs, aud = is_wav_to_memory(file_name, True, self.bitrate, self.limit_to_45_variable, False)
#Plot spectogram and save it to memory:
powerSpectrum, frequenciesFound, time, imageAxis = plt.specgram(aud, Fs=Fs) #'hot' #
plt.close()
#Show most used highest frequencies:
n_frequencies, n_times = powerSpectrum.shape
frequency_range = np.where((frequenciesFound >= 14000))
max_frequencies = [frequenciesFound[np.argmax(powerSpectrum[frequency_range, i])] for i in range(n_times)]
print("Max frequency of its fake lossles compresion", os.path.basename(file_name), ":", max(max_frequencies) + 14000)
def show_difference(self, instance):
self.insidecenter.clear_widgets()
self.insideright.clear_widgets()
self.insidecenter.add_widget(Label(text="Example of lossless audio: (Up to 22KHz)", pos_hint={'x':0, 'y':.8}, size_hint=(1,.1)))
lossless = Image(source="Icons-and-pictures/Lossless-audio-example.png", pos_hint={'x':0, 'y':0}, size_hint=(1, 1))
self.insidecenter.add_widget(lossless)
self.insideright.add_widget(Label(text="Example of lossy compression (196k) (Up to 19KHz):", pos_hint={'x':0, 'y':.8}, size_hint=(1,.1)))
lossy = Image(source="Icons-and-pictures/Lossy-audio-example.png", pos_hint={'x':0, 'y':0}, size_hint=(1, 1))
self.insideright.add_widget(lossy)
example_button = Button(text="Show me example of lossles audio check (Real flac)", pos_hint={'x':0.1, 'y':.1}, size_hint=(.8,.1))
example_button.bind(on_press=self.show_difference_lossles)
self.insidecenter.add_widget(example_button)
example_button2 = Button(text="Show me example of lossy audio check (Fake flac)", pos_hint={'x':0.1, 'y':.1}, size_hint=(.8,.1))
example_button2.bind(on_press=self.show_difference_lossy)
self.insideright.add_widget(example_button2)
def show_difference_lossles(self, instance):
self.insidecenter.clear_widgets()
self.insideright.clear_widgets()
self.insidecenter.add_widget(Label(text="Example of lossless audio check:", pos_hint={'x':0.5, 'y':.8}, size_hint=(1,.1)))
lossless = Image(source="Icons-and-pictures/Lossless-audio-example1split1.png", pos_hint={'x':0, 'y':.0}, size_hint=(1, 1))
self.insidecenter.add_widget(lossless)
lossy = Image(source="Icons-and-pictures/Lossless-audio-example1split2.png", pos_hint={'x':0, 'y':0}, size_hint=(1, 1))
self.insideright.add_widget(lossy)
example_button = Button(text="Back", pos_hint={'x':0.8, 'y':0}, size_hint=(.4,.1))
example_button.bind(on_press=self.show_difference)
self.insidecenter.add_widget(example_button)
def show_difference_lossy(self, instance):
self.insidecenter.clear_widgets()
self.insideright.clear_widgets()
self.insidecenter.add_widget(Label(text="Example of lossy (fake flac)audio check:", pos_hint={'x':0.5, 'y':.8}, size_hint=(1,.1)))
lossless = Image(source="Icons-and-pictures/Lossy-audio-example1split1.png", pos_hint={'x':0, 'y':.0}, size_hint=(1, 1))
self.insidecenter.add_widget(lossless)
lossy = Image(source="Icons-and-pictures/Lossy-audio-example1split2.png", pos_hint={'x':0, 'y':0}, size_hint=(1, 1))
self.insideright.add_widget(lossy)
example_button = Button(text="Back", pos_hint={'x':0.8, 'y':0}, size_hint=(.4,.1))
example_button.bind(on_press=self.show_difference)
self.insidecenter.add_widget(example_button)
def OnlySpecButton(self, instance):
root = tk.Tk()
root.withdraw()
self.file_name = filedialog.askopenfilename(filetypes = (("Audio Files", "*.mp3;*.wav;*.flac;"), ("All Files", "*.*")))
print(self.file_name)
if self.file_name != "":
self.doing_text = "Opening spectogram for file: " + os.path.basename(self.file_name)
self.update_doing_text(self.doing_text)
def calculation_callback(dt):
self.only_spectogram(self.file_name)
Clock.schedule_once(calculation_callback, 0)
def only_spectogram(self, file_name):
Fs, aud = is_wav_to_memory(file_name, False, self.bitrate, False, False)
plt.specgram(aud, Fs=Fs, cmap='nipy_spectral') #'hot' #powerSpectrum, frequenciesFound, time, imageAxis =
plt.title(os.path.basename(file_name))
plt.xlabel('Time [s]')
plt.ylabel('Frequency [Hz]')
plt.show()
def show_help(self, instance):
self.insidecenter.clear_widgets()
self.insideright.clear_widgets()
self.insideleft.remove_widget(self.play_difference_label)
help_text = """ There is no way to check if audio is truly lossless if you do not have the original file\n
One way to determine this is by looking at the spectrogram of the file and finding the cutoff.\n\n
Some audio files have a lower cutoff frequency, but this doesn't mean they're not lossless.\n
This program compares the spectrogram of your audio file to a fake "lossless" version.\n
If there's a difference, the audio file is likely lossless.\n
Most used highest friequencies are also displayed, if these values match you are probably dealing with fake lossless compression.\n\n
Use the "Compare audio with its fake resolution" option for this. You can limit the audio to 45 seconds for faster processing.\n\n
!!Please note that changing the bitrate to lower settings won't show you if the audio is truly lossless!!\n\n
You can play both the original and the lower-quality audio and save the lower-quality audio as an MP3 file.\n
You can play the difference between those two (+- What you are missing by compressing the audio)\n\n
You can also view the spectrogram of a single file using the "Open spectrogram for one file" option.\n
\n
\n
"""
example_button = Button(text="Show me example", pos_hint={'x':0.4, 'y':.1}, size_hint=(.6,.1))
example_button.bind(on_press=self.show_difference)
self.insidecenter.add_widget(example_button)
help_label = Label(text=help_text,size_hint=(.9, .9), pos_hint={'x': 0.3, 'y': 0.05} ) #,7 ,6
self.insidecenter.add_widget(help_label)
self.widgetje = False
class LosslessAudioChecker(App):
def build(self):
return MyGridLayout()
def remove_temp_file():
global temp_file_path, temp_file_path_of_difference
if os.path.exists(temp_file_path):
os.remove(temp_file_path)
if os.path.exists(temp_file_path_of_difference):
os.remove(temp_file_path_of_difference)
if __name__ == '__main__':
Window.size = (1366, 768) #1024x640, 1366x768, 1920x1080
Window.top = 30
Window.left = 0
Window.minimum_height = 640
plt.style.use('dark_background')
plt.rcParams['text.color'] = 'white'
LosslessAudioChecker().run()
atexit.register(remove_temp_file)