summaryrefslogtreecommitdiff
path: root/image.py
diff options
context:
space:
mode:
authorBen Connors <benconnors@outlook.com>2019-01-22 17:02:20 -0500
committerBen Connors <benconnors@outlook.com>2019-01-22 17:02:20 -0500
commitf43793940ccd0fd07b2459e4abc9d8e7d2c7d3c4 (patch)
treedde1b0424a2cba25c27298a924346b1ae062c5fa /image.py
parentca498eba3d9eaa2c25e281f9f8e6b5c3c8646ba6 (diff)
Fixes for image
- Implement audio cues that start after 0
Diffstat (limited to 'image.py')
-rwxr-xr-ximage.py72
1 files changed, 46 insertions, 26 deletions
diff --git a/image.py b/image.py
index d1b1fc0..c6d9dad 100755
--- a/image.py
+++ b/image.py
@@ -1,9 +1,16 @@
#!/usr/bin/env python3
+"""This module contains functions for visualizing QLC+ functions as images.
+
+For audio support,
+"""
+
import subprocess as subp
import tempfile
import wave
import os
+import warnings
+from shutil import which
from PIL import Image, ImageDraw, ImageFont
from .workspace import Show, Function, SHOW
@@ -24,7 +31,7 @@ def get_wave(fname):
"""Load an audio file into a wave."""
with tempfile.NamedTemporaryFile(delete=False,suffix=".wav") as f:
tname = f.name
- subp.call(["ffmpeg", "-i", fname, "-acodec", "pcm_s16le", "-y", tname], stdout=subp.DEVNULL, stderr=subp.DEVNULL)
+ subp.call(["ffmpeg", "-i", fname, "-acodec", "pcm_s16le", "-ar", "44100", "-y", tname], stdout=subp.DEVNULL, stderr=subp.DEVNULL)
return tname,wave.open(tname,mode="rb")
def chunks(l, n):
@@ -90,31 +97,44 @@ def render_image_show(s:Show):
draw.line(((0,numheight-1), (width-1, numheight-1)), fill=GRAY, width=3)
atime = []
- for a in acues:
- tname, wave = get_wave(a[1])
- nchannels, sampwidth, framerate, nframes, *_ = wave.getparams()
- if sampwidth != 2:
- raise ValueError("Only 16-bit wave is supported")
- skip = framerate//100
- for n,t in enumerate(range(0,nframes, skip)):
- asum = 0
- count = 0
- for i in range(skip//10):
- for c in chunks(wave.readframes(10)[:nchannels*sampwidth],sampwidth):
- asum += abs(s16letoi(*c))
- count += 1
- if skip%10 > 0:
- wave.readframes(skip%10)
- if not count:
- break
- if len(atime) > n:
- atime[n] = min(atime[n]+asum/count, 32767)
- else:
- atime.append(asum/count)
-
- wave.close()
- os.remove(tname)
- break
+
+ if which("ffmpeg") is None:
+ warnings.warn("Cannot find ffmpeg, audio is disabled", RuntimeWarning)
+ else:
+ for a in acues:
+ stime = a[0]
+
+ skipperiods = stime//10 + (stime%10 > 0)
+
+ ## Add 0 entries as necessary to the audio list
+ atime += [0]*max(0,skipperiods-len(atime))
+
+ tname, wave = get_wave(a[1])
+ nchannels, sampwidth, framerate, nframes, *_ = wave.getparams()
+ if sampwidth != 2:
+ raise ValueError("Only 16-bit wave is supported")
+ skip = framerate//100
+ if stime%10 > 0:
+ wave.readframes((stime%10)*(skip//10))
+ for n,t in enumerate(range(0,nframes, skip), skipperiods):
+ asum = 0
+ count = 0
+ for i in range(skip//10):
+ for c in chunks(wave.readframes(10)[:nchannels*sampwidth],sampwidth):
+ asum += abs(s16letoi(*c))
+ count += 1
+ if skip%10 > 0:
+ wave.readframes(skip%10)
+ if not count:
+ break
+ if len(atime) > n:
+ atime[n] = min(atime[n]+asum/count, 32767)
+ else:
+ atime.append(asum/count)
+
+ wave.close()
+ os.remove(tname)
+ break
t = 0
while vals: