Skip to content

Commit

Permalink
Initial conversion to Python 3
Browse files Browse the repository at this point in the history
  • Loading branch information
wvengen committed Jun 14, 2020
1 parent a95f694 commit aa71fe5
Show file tree
Hide file tree
Showing 19 changed files with 132 additions and 136 deletions.
26 changes: 13 additions & 13 deletions lib/audio_helpers.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import time
import numpy
from utils import note_name, percent_to_db
from record import record
from constants import CLIPPING_THRESHOLD, \
from .utils import note_name, percent_to_db
from .record import record
from .constants import CLIPPING_THRESHOLD, \
CLIPPING_CHECK_NOTE, \
EXIT_ON_CLIPPING, \
SAMPLE_RATE
from midi_helpers import all_notes_off, CHANNEL_OFFSET
from .midi_helpers import all_notes_off, CHANNEL_OFFSET


def generate_sample(
Expand Down Expand Up @@ -46,7 +46,7 @@ def on_time_up():

def sample_threshold_from_noise_floor(bit_depth, audio_interface_name):
time.sleep(1)
print "Sampling noise floor..."
print("Sampling noise floor...")
sample_width, data, release_time = record(
limit=2.0,
after_start=None,
Expand All @@ -60,9 +60,9 @@ def sample_threshold_from_noise_floor(bit_depth, audio_interface_name):
numpy.amax(numpy.absolute(data)) /
float(2 ** (bit_depth - 1))
)
print "Noise floor has volume %8.8f dBFS" % percent_to_db(noise_floor)
print("Noise floor has volume %8.8f dBFS" % percent_to_db(noise_floor))
threshold = noise_floor * 1.1
print "Setting threshold to %8.8f dBFS" % percent_to_db(threshold)
print("Setting threshold to %8.8f dBFS" % percent_to_db(threshold))
return threshold


Expand All @@ -74,9 +74,9 @@ def check_for_clipping(
audio_interface_name,
):
time.sleep(1)
print "Checking for clipping and balance on note %s..." % (
print("Checking for clipping and balance on note %s..." % (
note_name(CLIPPING_CHECK_NOTE)
)
))

sample_width, data, release_time = generate_sample(
limit=2.0,
Expand All @@ -99,14 +99,14 @@ def check_for_clipping(
)

# All notes off, but like, a lot, again
for _ in xrange(0, 2):
for _ in range(0, 2):
all_notes_off(midiout, midi_channel)

print "Maximum volume is around %8.8f dBFS" % percent_to_db(max_volume)
print("Maximum volume is around %8.8f dBFS" % percent_to_db(max_volume))
if max_volume >= CLIPPING_THRESHOLD:
print "Clipping detected (%2.2f dBFS >= %2.2f dBFS) at max volume!" % (
print("Clipping detected (%2.2f dBFS >= %2.2f dBFS) at max volume!" % (
percent_to_db(max_volume), percent_to_db(CLIPPING_THRESHOLD)
)
))
if EXIT_ON_CLIPPING:
raise ValueError("Clipping detected at max volume!")

Expand Down
34 changes: 15 additions & 19 deletions lib/compare.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
from tqdm import tqdm
from tabulate import tabulate

from utils import normalized, trim_mono_data
from audio_helpers import fundamental_frequency
from wavio import read_wave_file
from .utils import normalized, trim_mono_data
from .audio_helpers import fundamental_frequency
from .wavio import read_wave_file

import matplotlib.pyplot as plt

Expand Down Expand Up @@ -106,21 +106,18 @@ def process_all(aifs):
)
results = sorted(
results,
key=lambda (dl, dr,
pl, pr,
freqd,
fa, fb): dl + dr + abs(freqd - 1))
key=lambda dl_dr_pl_pr_freqd_fa_fb: dl_dr_pl_pr_freqd_fa_fb[0] + dl_dr_pl_pr_freqd_fa_fb[1] + abs(dl_dr_pl_pr_freqd_fa_fb[4] - 1))
with open('results.csv', 'wb') as f:
writer = csv.writer(f)
writer.writerows([headers])
writer.writerows(results)

print "%d results" % len(results)
print tabulate(
print("%d results" % len(results))
print(tabulate(
results,
headers=headers,
floatfmt='.4f'
)
))


def graph_ffts():
Expand All @@ -139,10 +136,9 @@ def graph_ffts():
idx = numpy.argmax(numpy.abs(w))
freq = freqs[idx]
plt.plot(w)
print freq
print \
fundamental_frequency(normalized(list)), \
fundamental_frequency(normalized(left + right))
print(freq)
print(fundamental_frequency(normalized(list)), \
fundamental_frequency(normalized(left + right)))
# plt.show()


Expand Down Expand Up @@ -189,11 +185,11 @@ def freq_shift():
shifted_diffr = normalized_difference(*aligned_sublists(wavea[1],
waveb_shifted[1]))

print files
print 'diffs\t\t', diffl, diffr
print 'shifted diffs\t', shifted_diffl, shifted_diffr
print 'freqs', freqd
print 'shifted freqs', shifted_freqd
print(files)
print('diffs\t\t', diffl, diffr)
print('shifted diffs\t', shifted_diffl, shifted_diffr)
print('freqs', freqd)
print('shifted freqs', shifted_freqd)


if __name__ == "__main__":
Expand Down
10 changes: 5 additions & 5 deletions lib/deflac.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@
import argparse
import subprocess
from tqdm import tqdm
from sfzparser import SFZFile
from wavio import read_wave_file
from utils import normalized
from record import RATE, save_to_file
from constants import bit_depth
from .sfzparser import SFZFile
from .wavio import read_wave_file
from .utils import normalized
from .record import RATE, save_to_file
from .constants import bit_depth


def full_path(sfzfile, filename):
Expand Down
18 changes: 9 additions & 9 deletions lib/flacize.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
import argparse
import subprocess
from tqdm import tqdm
from sfzparser import SFZFile, Group
from wavio import read_wave_file
from utils import group_by_attr, note_name
from .sfzparser import SFZFile, Group
from .wavio import read_wave_file
from .utils import group_by_attr, note_name


def full_path(sfzfile, filename):
Expand Down Expand Up @@ -66,7 +66,7 @@ def flacize_after_sampling(
for key, key_regions in
group_by_attr(group.regions, [
'key', 'pitch_keycenter'
]).iteritems()], [])
]).items()], [])
new_groups.append(Group(group.attributes, output))

with open(sfzfile + '.flac.sfz', 'w') as file:
Expand All @@ -77,7 +77,7 @@ def flacize_after_sampling(
try:
os.unlink(path)
except OSError as e:
print "Could not unlink path: %s: %s" % (path, e)
print("Could not unlink path: %s: %s" % (path, e))


ANTI_CLICK_OFFSET = 3
Expand Down Expand Up @@ -128,8 +128,8 @@ def concat_samples(regions, path, name=None):
filename,
note_name(key)))
for key, regions in
tqdm(group_by_attr(group.regions,
'key').iteritems())], [])
print group.just_group()
tqdm(iter(group_by_attr(group.regions,
'key').items()))], [])
print(group.just_group())
for region in output:
print region
print(region)
2 changes: 1 addition & 1 deletion lib/graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from tqdm import tqdm
from tabulate import tabulate

from wavio import read_wave_file
from .wavio import read_wave_file

import matplotlib.pyplot as plt

Expand Down
12 changes: 6 additions & 6 deletions lib/group_velcurves.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import argparse
from sfzparser import parse, Group
from quantize import group_by_attr
from .sfzparser import parse, Group
from .quantize import group_by_attr

parser = argparse.ArgumentParser(
description='quantize and compress SFZ files'
Expand All @@ -18,14 +18,14 @@ def should_group_key(key):


def group_by_pitch(regions):
for key, regions in group_by_attr(regions, 'key').iteritems():
for key, regions in group_by_attr(regions, 'key').items():
# Group together all amp_velcurve_* and key params.
yield Group(dict([
(key, value)
for region in regions
for key, value in region.attributes.iteritems()
for key, value in region.attributes.items()
if should_group_key(key)
] + DEFAULT_ATTRIBUTES.items()), [
] + list(DEFAULT_ATTRIBUTES.items())), [
region.without_attributes(should_group_key) for region in regions
])

Expand All @@ -41,4 +41,4 @@ def group_by_pitch(regions):
groups = parse(open(filename).read())
regions = sum([group.flattened_regions() for group in groups], [])
for group in group_by_pitch(regions):
print group
print(group)
28 changes: 14 additions & 14 deletions lib/loop.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import sys
import numpy
from tqdm import tqdm
from truncate import read_wave_file
from audio_helpers import fundamental_frequency
from .truncate import read_wave_file
from .audio_helpers import fundamental_frequency

QUANTIZE_FACTOR = 8

Expand All @@ -12,20 +12,20 @@ def compare_windows(window_a, window_b):


def slide_window(file, period, start_at=0, end_before=0):
for power in reversed(xrange(7, 10)):
for power in reversed(range(7, 10)):
multiple = 2 ** power
window_size = int(period * multiple)
# Uncomment this to search from the start_at value to the end_before
# rather than just through one window's length
# end_range = len(file) - (window_size * 2) - end_before
end_range = start_at + window_size
for i in xrange(start_at, end_range):
for i in range(start_at, end_range):
yield power, i, window_size


def window_match(file):
period = (1.0 / fundamental_frequency(file, 1)) * 2
print period, 'period in samples'
print(period, 'period in samples')

winner = None

Expand All @@ -48,14 +48,14 @@ def window_match(file):
i,
abs(file[i] - file[window_start])
)
print 'new winner', winner
print('new winner', winner)

lowest_difference, winning_window_size, winning_index, gap = winner

print "Best loop match:", lowest_difference
print "window size", winning_window_size
print "winning index", winning_index
print "winning gap", gap
print("Best loop match:", lowest_difference)
print("window size", winning_window_size)
print("winning index", winning_index)
print("winning gap", gap)
return winning_index, winning_window_size


Expand All @@ -71,7 +71,7 @@ def find_similar_sample_index(
):
reference_slope = slope_at_index(file, reference_index) > 0
best_match = None
search_range = xrange(
search_range = range(
search_around_index - search_size,
search_around_index + search_size
)
Expand All @@ -93,12 +93,12 @@ def find_similar_sample_index(

def zero_crossing_match(file):
period = (1.0 / fundamental_frequency(file, 1)) * 2
print period, 'period in samples'
print(period, 'period in samples')

period_multiple = 64
period = period * period_multiple

for i in reversed(xrange(2 * len(file) / 3, 5 * len(file) / 6)):
for i in reversed(range(2 * len(file) / 3, 5 * len(file) / 6)):
if file[i] >= 0 and file[i + 1] < 0 and \
file[int(i + period)] >= 0 and \
file[int(i + 1 + period)] < 0 and \
Expand Down Expand Up @@ -234,7 +234,7 @@ def process(aif, sample_rate=48000):

file = file[0]

print 'start, end', loop_start, loop_end
print('start, end', loop_start, loop_end)

plt.plot(file[loop_start:loop_end])
plt.plot(file[loop_end:loop_start + (2 * loop_size)])
Expand Down
4 changes: 2 additions & 2 deletions lib/midi_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ def open_midi_port(midi_port_name):

def set_program_number(midiout, midi_channel, program_number):
if program_number is not None:
print "Sending program change to program %d..." % program_number
print("Sending program change to program %d..." % program_number)
# Bank change (fine) to (program_number / 128)
midiout.send_message([
CC_CHANNEL_OFFSET + midi_channel,
Expand All @@ -64,7 +64,7 @@ def set_program_number(midiout, midi_channel, program_number):
])

# All notes off, but like, a lot
for _ in xrange(0, 2):
for _ in range(0, 2):
all_notes_off(midiout, midi_channel)

time.sleep(0.5)
Expand Down
Loading

0 comments on commit aa71fe5

Please sign in to comment.