Skip to content

Commit

Permalink
add: convert to mono import option
Browse files Browse the repository at this point in the history
  • Loading branch information
JonathanTaquet committed Mar 30, 2018
1 parent 4e352d5 commit bd8f512
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 7 deletions.
27 changes: 27 additions & 0 deletions GUI/import_options.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,11 @@ def __init__(self, parent, options, *args, **kwargs):
self.smp_num_from = tk.IntVar()
self.smp_num_from.set(options.smp_num_from)

self.force_mono = tk.IntVar()
self.force_mono.set(options.force_mono)
self.mono_mix = tk.DoubleVar()
self.mono_mix.set(options.mono_mix)

fr = tk.Frame(self)

tk.Label(fr, text="OSC Cat.").grid(row=0, column=1)
Expand Down Expand Up @@ -127,6 +132,26 @@ def __init__(self, parent, options, *args, **kwargs):

fr.pack(fill=tk.BOTH, expand=True, padx=5, pady=5)

fr = tk.Frame(self)

tk.Label(fr, text="Force mono : ").pack(side=tk.LEFT)
tk.Checkbutton(
fr,
variable=self.force_mono
).pack(side=tk.LEFT)
tk.Label(fr, text=" L").pack(side=tk.LEFT)
tk.Scale(
fr,
variable=self.mono_mix,
orient=tk.HORIZONTAL,
from_=-1,
to=1,
resolution=0.001
).pack(fill=tk.X, side=tk.LEFT, expand=True)
tk.Label(fr, text="R").pack(side=tk.LEFT)

fr.pack(fill=tk.BOTH, expand=True, padx=5, pady=5)

box = tk.Frame(self)

tk.Button(
Expand Down Expand Up @@ -170,6 +195,8 @@ def update_data(self):
self.options.force_loop_type = self.force_loop_type.get()
self.options.force_plus_12_db = self.force_plus_12_db.get()
self.options.smp_num_from = self.smp_num_from.get()
self.options.force_mono = self.force_mono.get()
self.options.mono_mix = self.mono_mix.get()

#
# standard button semantics
Expand Down
16 changes: 11 additions & 5 deletions Oe2sSLE_GUI.py
Original file line number Diff line number Diff line change
Expand Up @@ -1522,18 +1522,21 @@ def fct():
res = self.master.parent._import_sample_helper(filename)

if res:
sample, converted_from = res
sample, converted_from, converted_to_mono = res
esli = sample.get_esli()
esli.OSC_0index = esli.OSC_0index1 = self.e2s_sample.get_esli().OSC_0index
self.master.e2s_samples[self.sample_num] = sample
self.master.update_sample(self.sample_num)
self.master.update_WAVDataSize()

if converted_from:
if converted_from or converted_to_mono:
conversion = (
["from {} bits to 16 bits".format(converted_from)] if converted_from else []
+ ["to mono"] if converted_to_mono else []
)
tk.messagebox.showinfo(
"Import WAV",
("'{}' file converted from 8 bits to 16 bits.\n".format(filename) if converted_from == 8 else
"'{}' file converted from 24 bits to 16 bits.\n".format(filename))
"'{}' file converted {}.\n".format(filename, " and ".join(conversion))
)
if filename:
wd = WaitDialog(self.master.parent)
Expand Down Expand Up @@ -2196,10 +2199,12 @@ def fct():
if not res:
continue

sample, converted_from = res
sample, converted_from, converted_to_mono = res

if converted_from:
num_converted[converted_from] = num_converted.get(converted_from, 0) + 1
if converted_to_mono:
num_converted['mono'] = num_converted.get('mono', 0) + 1
try:
self.register_new_sample(sample)
except ToManySamples:
Expand All @@ -2213,6 +2218,7 @@ def fct():
if num_converted:
tk.messagebox.showinfo(
"Import WAV",
("{} file(s) converted to mono.\n".format(num_converted['mono']) if num_converted.get('mono') else "") +
("{} file(s) converted from 8 bits to 16 bits.\n".format(num_converted[8]) if num_converted.get(8) else "") +
("{} file(s) converted from 24 bits to 16 bits.\n".format(num_converted[24]) if num_converted.get(24) else "")
)
Expand Down
40 changes: 38 additions & 2 deletions e2s_sample_import.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
along with Oe2sSLE. If not, see <http://www.gnu.org/licenses/>
"""

import copy
import math
import os

Expand Down Expand Up @@ -46,6 +47,11 @@ def __init__(self):
# A free sample number will be searched from smp_num_from value
# when a new sample will be imported
self.smp_num_from = 19
# force to import stereo samples as mono
self.force_mono = 0
# the mix value used when samples are converted to mono
# (-1.0: Left, 0.0: Center, 1.0: Right)
self.mono_mix = 0.0


class FromWavError(Exception):
Expand Down Expand Up @@ -170,11 +176,12 @@ def from_wav(filename, import_opts=ImportOptions()):
else:
esli = esli_chunk.data

apply_forced_options(sample, import_opts)
return sample, converted_from
converted_to_mono = apply_forced_options(sample, import_opts)
return sample, converted_from, converted_to_mono


def apply_forced_options(e2s_sample, import_opts):
converted_to_mono = False
esli = e2s_sample.get_esli()

if import_opts.force_osc_cat:
Expand All @@ -192,3 +199,32 @@ def apply_forced_options(e2s_sample, import_opts):

if import_opts.force_plus_12_db:
esli.playLevel12dB = import_opts.plus_12_db

if import_opts.force_mono:
converted_to_mono = _convert_to_mono(e2s_sample, import_opts.mono_mix)

return converted_to_mono

def _convert_to_mono(e2s_sample, mix):
converted = False
num_chans = e2s_sample.get_fmt().channels
if num_chans > 1:
converted = True
_esli = e2s.RIFF_korg_esli()
_esli.rawdata[:] = e2s_sample.get_esli().rawdata[:]
prev_fmt = e2s_sample.get_fmt()
_fmt = copy.deepcopy(prev_fmt)
_fmt.channels=1
_fmt.avgBytesPerSec = _fmt.avgBytesPerSec // prev_fmt.channels
_fmt.blockAlign = _fmt.blockAlign // prev_fmt.channels
_esli.OSC_StartPoint_address = _esli.OSC_StartPoint_address // prev_fmt.channels
_esli.OSC_LoopStartPoint_offset = _esli.OSC_LoopStartPoint_offset // prev_fmt.channels
_esli.OSC_EndPoint_offset = _esli.OSC_EndPoint_offset // prev_fmt.channels
_esli.WAV_dataSize = _esli.WAV_dataSize // prev_fmt.channels
_esli.useChan1 = False
w = ((1 - mix)/2, 1 - (1 - mix)/2) + (0,)*(num_chans-2)
_data = wav_tools.wav_mchan_to_mono(e2s_sample.get_data().rawdata, w)
e2s_sample.get_esli().rawdata = _esli.rawdata
e2s_sample.get_fmt().__dict__ = _fmt.__dict__
e2s_sample.get_data().rawdata = _data
return converted

0 comments on commit bd8f512

Please sign in to comment.