2015-03-05 09:58:45 -05:00
|
|
|
import sys, io, os
|
2015-03-02 16:47:52 -05:00
|
|
|
from PyQt4 import QtCore, QtGui, uic
|
|
|
|
from os.path import expanduser
|
|
|
|
import subprocess as sp
|
|
|
|
import numpy
|
2017-05-28 14:19:28 -04:00
|
|
|
from PIL import Image
|
2017-05-18 19:14:27 -04:00
|
|
|
import tempfile
|
|
|
|
from shutil import rmtree
|
|
|
|
import atexit
|
2017-06-02 01:30:44 -04:00
|
|
|
import time
|
2017-06-03 15:24:52 -04:00
|
|
|
from collections import OrderedDict
|
2015-03-02 16:47:52 -05:00
|
|
|
|
|
|
|
class Core():
|
|
|
|
|
|
|
|
def __init__(self):
|
2015-03-05 09:58:45 -05:00
|
|
|
self.FFMPEG_BIN = self.findFfmpeg()
|
2017-06-04 13:00:36 -04:00
|
|
|
self.tempDir = os.path.join(tempfile.gettempdir(), 'audio-visualizer-python-data')
|
2017-06-04 20:27:43 -04:00
|
|
|
if not os.path.exists(self.tempDir):
|
|
|
|
os.makedirs(self.tempDir)
|
2017-05-18 19:14:27 -04:00
|
|
|
atexit.register(self.deleteTempDir)
|
2015-03-05 09:58:45 -05:00
|
|
|
|
|
|
|
def findFfmpeg(self):
|
2015-03-02 16:47:52 -05:00
|
|
|
if sys.platform == "win32":
|
2015-03-05 09:58:45 -05:00
|
|
|
return "ffmpeg.exe"
|
2015-03-02 16:47:52 -05:00
|
|
|
else:
|
2015-03-05 09:58:45 -05:00
|
|
|
try:
|
|
|
|
with open(os.devnull, "w") as f:
|
|
|
|
sp.check_call(['ffmpeg', '-version'], stdout=f, stderr=f)
|
|
|
|
return "ffmpeg"
|
|
|
|
except:
|
|
|
|
return "avconv"
|
2015-03-02 16:47:52 -05:00
|
|
|
|
2017-06-02 04:30:51 -04:00
|
|
|
def readAudioFile(self, filename, parent):
|
|
|
|
command = [ self.FFMPEG_BIN,
|
|
|
|
'-i', filename]
|
|
|
|
|
|
|
|
try:
|
|
|
|
fileInfo = sp.check_output(command, stderr=sp.STDOUT, shell=False)
|
|
|
|
except sp.CalledProcessError as ex:
|
|
|
|
fileInfo = ex.output
|
|
|
|
pass
|
|
|
|
|
|
|
|
info = fileInfo.decode("utf-8").split('\n')
|
|
|
|
for line in info:
|
|
|
|
if 'Duration' in line:
|
|
|
|
d = line.split(',')[0]
|
|
|
|
d = d.split(' ')[3]
|
|
|
|
d = d.split(':')
|
|
|
|
duration = float(d[0])*3600 + float(d[1])*60 + float(d[2])
|
|
|
|
|
2015-03-02 16:47:52 -05:00
|
|
|
command = [ self.FFMPEG_BIN,
|
|
|
|
'-i', filename,
|
|
|
|
'-f', 's16le',
|
|
|
|
'-acodec', 'pcm_s16le',
|
|
|
|
'-ar', '44100', # ouput will have 44100 Hz
|
|
|
|
'-ac', '1', # mono (set to '2' for stereo)
|
|
|
|
'-']
|
|
|
|
in_pipe = sp.Popen(command, stdout=sp.PIPE, stderr=sp.DEVNULL, bufsize=10**8)
|
2017-05-18 19:54:48 -04:00
|
|
|
|
2015-03-02 16:47:52 -05:00
|
|
|
completeAudioArray = numpy.empty(0, dtype="int16")
|
|
|
|
|
2017-06-02 04:30:51 -04:00
|
|
|
progress = 0
|
|
|
|
lastPercent = None
|
2015-03-02 16:47:52 -05:00
|
|
|
while True:
|
2017-06-02 01:30:44 -04:00
|
|
|
if self.canceled:
|
|
|
|
break
|
2015-03-02 16:47:52 -05:00
|
|
|
# read 2 seconds of audio
|
2017-06-02 04:30:51 -04:00
|
|
|
progress = progress + 4
|
2015-03-02 16:47:52 -05:00
|
|
|
raw_audio = in_pipe.stdout.read(88200*4)
|
|
|
|
if len(raw_audio) == 0:
|
|
|
|
break
|
|
|
|
audio_array = numpy.fromstring(raw_audio, dtype="int16")
|
|
|
|
completeAudioArray = numpy.append(completeAudioArray, audio_array)
|
2017-06-02 04:30:51 -04:00
|
|
|
|
|
|
|
percent = int(100*(progress/duration))
|
|
|
|
if percent >= 100:
|
|
|
|
percent = 100
|
|
|
|
|
|
|
|
if lastPercent != percent:
|
|
|
|
string = 'Loading audio file: '+str(percent)+'%'
|
|
|
|
parent.progressBarSetText.emit(string)
|
|
|
|
parent.progressBarUpdate.emit(percent)
|
|
|
|
|
|
|
|
lastPercent = percent
|
|
|
|
|
2015-03-02 16:47:52 -05:00
|
|
|
|
|
|
|
in_pipe.kill()
|
|
|
|
in_pipe.wait()
|
|
|
|
|
|
|
|
# add 0s the end
|
|
|
|
completeAudioArrayCopy = numpy.zeros(len(completeAudioArray) + 44100, dtype="int16")
|
|
|
|
completeAudioArrayCopy[:len(completeAudioArray)] = completeAudioArray
|
|
|
|
completeAudioArray = completeAudioArrayCopy
|
|
|
|
|
|
|
|
return completeAudioArray
|
|
|
|
|
2017-05-18 19:14:27 -04:00
|
|
|
def deleteTempDir(self):
|
2017-06-04 13:00:36 -04:00
|
|
|
try:
|
2017-06-04 20:27:43 -04:00
|
|
|
rmtree(self.tempDir)
|
2017-06-04 13:00:36 -04:00
|
|
|
except FileNotFoundError:
|
|
|
|
pass
|
2017-06-02 01:30:44 -04:00
|
|
|
|
|
|
|
def cancel(self):
|
|
|
|
self.canceled = True
|
|
|
|
|
|
|
|
def reset(self):
|
|
|
|
self.canceled = False
|
2017-06-03 15:24:52 -04:00
|
|
|
|
|
|
|
@staticmethod
|
2017-06-04 13:00:36 -04:00
|
|
|
def stringOrderedDict(dictionary):
|
2017-06-03 15:24:52 -04:00
|
|
|
sorted_ = OrderedDict(sorted(dictionary.items(), key=lambda t: t[0]))
|
|
|
|
return repr(sorted_)
|