Skip to content

Commit

Permalink
--wip-- [skip ci]
Browse files Browse the repository at this point in the history
  • Loading branch information
Paul R. Adams committed Jan 2, 2018
1 parent 8c3d7a8 commit ac98570
Show file tree
Hide file tree
Showing 4 changed files with 208 additions and 8 deletions.
93 changes: 93 additions & 0 deletions analysis.ipynb

Large diffs are not rendered by default.

105 changes: 105 additions & 0 deletions clamm/plot_big_stft.py
@@ -0,0 +1,105 @@
"""
Convert a large audio wav file (album length, i.e. > 30 minutes typically)
into a series of videos consisting of the audio synchronized with images of the
spectrogram.
"""
import os
import sys
import glob

import tqdm
import numpy as np
import librosa.core
import librosa.display
import matplotlib
import matplotlib.pyplot as plt

SAMPLERATE = 44.1e3 # samples/sec
WAVPATH = sys.argv[1]


def image_to_cursor_frames(ax, x, duration, savebase):
""" image_to_cursor_frames
"""
frame_root = "frames"
if not os.path.exists(frame_root):
os.mkdir(frame_root)
[os.remove(p) for p in glob.glob(frame_root + "/*.png")]

fft_freqs = librosa.fft_frequencies(sr=SAMPLERATE)
f_max = np.max(fft_freqs)

with tqdm.tqdm(total=duration) as pbar:
for i_second in range(duration):
pbar.update(1)
lcs = [child
for child in ax.get_children()
if isinstance(child, matplotlib.collections.LineCollection)]
[lc.remove() for lc in lcs]

plt.vlines(
i_second, 0, f_max,
linestyles='dashed',
colors='w', alpha=0.8)
plt.savefig(
os.path.join(
frame_root,
savepath(savebase, ('frame', i_second))))


def savepath(base, appendages, ext=".png"):
""" savepath
"""
basestr = "%s" % (
os.path.splitext(os.path.basename(base))[0].replace(" ", "_").lower())
basestr += "-%s%0.4d" % (appendages[0], appendages[1])
return basestr + ext


def main():
""" main
"""
file_time = librosa.core.get_duration(filename=WAVPATH)

time_remain = file_time
duration = 90 #
n_fft = 2048
hop_length = int(3.0 / 4 * n_fft)

pbar = tqdm.tqdm(total=file_time)
while time_remain > 0:
# loop updates
pbar.update(duration)
duration = duration if time_remain > duration else time_remain

# load the audio
x, sr = librosa.core.load(
WAVPATH, sr=SAMPLERATE,
offset=file_time - time_remain, duration=duration)
time_remain -= duration

# compute the spectrogram
x = librosa.stft(
x, hop_length=hop_length, n_fft=n_fft, win_length=n_fft)

# display the spectrogram
plt.figure(figsize=(16, 6))
spec = librosa.display.specshow(
librosa.amplitude_to_db(x, ref=np.max),
x_axis='time', y_axis='log', sr=sr, hop_length=hop_length)
plt.tight_layout()

# save at static image of result
savename = savepath(
WAVPATH, ("offset", file_time - time_remain - duration))
plt.title(savename.replace(".png", ""))
plt.savefig(savename)

image_to_cursor_frames(spec, x, duration, savename)

plt.close()
pbar.close()


if __name__ == '__main__':
main()
3 changes: 1 addition & 2 deletions clamm/streams.py
Expand Up @@ -20,7 +20,6 @@

from clamm import config
from clamm import util
from clamm import audiolib

# constants, globals
plt.switch_backend("agg")
Expand Down Expand Up @@ -58,7 +57,7 @@ def __init__(self, streampath):
def pcm2wav(self):
""" pcm2wav """
if not os.path.exists(self.wavpath):
audiolib.pcm2wav(self.pcmpath, self.wavpath)
util.pcm2wav(self.pcmpath, self.wavpath)

def decode_path(self):
"""artist/album names from stream name
Expand Down
15 changes: 9 additions & 6 deletions clamm/util.py
Expand Up @@ -102,16 +102,19 @@ def start_shairport(filepath):

time.sleep(1)

p1 = subprocess.Popen(
['shairport-sync', '-o=stdout'], stdout=subprocess.PIPE)

subprocess.Popen(
["ffmpeg", "-hide_banner", "-y", "-f",
"s16le", "-ar", "44.1k", "-ac", "2", filepath], stdin=p1.stdout)
with open(filepath, "w") as fptr:
subprocess.Popen(
['shairport-sync', '-o=stdout'], stdout=fptr)

printr("shairport up and running.")


def pcm2wav(pcm_name, wav_name):
subprocess.call(
["ffmpeg", "-hide_banner", "-y", "-f",
"s16le", "-ar", "44.1k", "-ac", "2", "-i", pcm_name, wav_name])


def wav2flac(wav_name):
"""utility for using ``ffmpeg`` to convert a wav file to a flac file
"""
Expand Down

0 comments on commit ac98570

Please sign in to comment.