Skip to content

Commit

Permalink
Develop (#6)
Browse files Browse the repository at this point in the history
* Bugfix
* Added useless prints and added a clipping warning
* change readme
* Added logging of version to __init__
  • Loading branch information
thomas-imt committed Jan 23, 2018
1 parent 9b622df commit ce2a025
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 35 deletions.
9 changes: 5 additions & 4 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,14 @@ Create ``pyBinSimSettings.txt`` file with content like this
::

soundfile signals/test441kHz.wav
blockSize 256
blockSize 512
filterSize 16384
filterList brirs/filter_list_kemar5.txt
maxChannels 8
maxChannels 2
samplingRate 44100
enableCrossfading False
enableCrossfading True
useHeadphoneFilter False
loudnessFactor 1
loudnessFactor 0.5
loopSound False


Expand All @@ -46,6 +46,7 @@ Start Binaural Simulation
import logging

pybinsim.logger.setLevel(logging.DEBUG) # defaults to INFO
#Use logging.WARNING for printing warnings only

with pybinsim.BinSim('pyBinSimSettings.txt') as binsim:
binsim.stream_start()
Expand Down
3 changes: 2 additions & 1 deletion pybinsim/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import logging

__version__ = "1.2.0"
__version__ = "1.2.1"

from pybinsim.application import BinSim

Expand All @@ -20,3 +20,4 @@ def init_logging(loglevel):


logger = init_logging(logging.INFO)
logger.info("Starting pybinsim v{}".format(__version__))
3 changes: 3 additions & 0 deletions pybinsim/application.py
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,9 @@ def callback(in_data, frame_count, time_info, status):
binsim.result = np.divide(binsim.result, float((binsim.soundHandler.get_sound_channels()) * 2))
binsim.result = np.multiply(binsim.result,callback.config.get('loudnessFactor'))

if np.max(np.abs(binsim.result))>1:
binsim.log.warn('Clipping occurred: Adjust loudnessFactor!')

# When the last block is small than the blockSize, this is probably the end of the file.
# Call pyaudio to stop after this frame
# Should not be the case for current soundhandler implementation
Expand Down
58 changes: 30 additions & 28 deletions pybinsim/convolver.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@

import numpy as np
import pyfftw
from past.builtins import xrange


nThreads = multiprocessing.cpu_count()

Expand Down Expand Up @@ -54,12 +54,12 @@ def __init__(self, ir_size, block_size, process_stereo):
self.IR_blocks = self.IR_size // block_size

# Calculate LINEAR crossfade windows
#self.crossFadeIn = np.array(xrange(0, self.block_size), dtype='float32')
#self.crossFadeIn = np.array(range(0, self.block_size), dtype='float32')
#self.crossFadeIn *= 1 / float((self.block_size - 1))
#self.crossFadeOut = np.flipud(self.crossFadeIn)

# Calculate COSINE-Square crossfade windows
self.crossFadeOut = np.array(xrange(0, self.block_size), dtype='float32')
self.crossFadeOut = np.array(range(0, self.block_size), dtype='float32')
self.crossFadeOut = np.square(np.cos(self.crossFadeOut/(self.block_size-1)*(np.pi/2)))
self.crossFadeIn = np.flipud(self.crossFadeOut)

Expand All @@ -78,11 +78,12 @@ def __init__(self, ir_size, block_size, process_stereo):
# Create arrays for the filters and the FDLs.
self.TF_left_blocked = np.zeros((self.IR_blocks, self.block_size + 1), dtype='complex64')
self.TF_right_blocked = np.zeros((self.IR_blocks, self.block_size + 1), dtype='complex64')
self.TF_left_blocked_previous = np.zeros((self.IR_blocks, self.block_size + 1), dtype='complex64')
self.TF_right_blocked_previous = np.zeros((self.IR_blocks, self.block_size + 1), dtype='complex64')
self.TF_left_blocked_previous = np.zeros((self.IR_blocks, self.block_size + 1), dtype='complex64')
self.TF_right_blocked_previous = np.zeros((self.IR_blocks, self.block_size + 1), dtype='complex64')

self.filter_fftw_plan = pyfftw.builders.rfft(np.zeros(self.block_size),n=self.block_size * 2, overwrite_input=True, threads=nThreads,
planner_effort=self.fftw_planning_effort,avoid_copy=False)
self.filter_fftw_plan = pyfftw.builders.rfft(np.zeros(self.block_size),n=self.block_size * 2, overwrite_input=True,
threads=nThreads, planner_effort=self.fftw_planning_effort,
avoid_copy=False)

self.FDL_size = self.IR_blocks * (self.block_size + 1)
self.FDL_left = np.zeros(self.FDL_size, dtype='complex64')
Expand Down Expand Up @@ -140,10 +141,12 @@ def transform_filter(self, filter):
# Get blocked IRs
IR_left_blocked, IR_right_blocked = filter.getFilter()

# Add zeroes to each block and transform to frequency domain
self.TF_left_blocked = np.zeros([self.IR_blocks, self.block_size + 1], dtype='complex64')
self.TF_right_blocked = np.zeros([self.IR_blocks, self.block_size + 1], dtype='complex64')

for ir_block_count in range(0, self.IR_blocks):
self.TF_left_blocked[ir_block_count,:] = self.filter_fftw_plan(IR_left_blocked[ir_block_count])
self.TF_right_blocked[ir_block_count,:] = self.filter_fftw_plan(IR_right_blocked[ir_block_count])
self.TF_left_blocked[ir_block_count] = self.filter_fftw_plan(IR_left_blocked[ir_block_count])
self.TF_right_blocked[ir_block_count] = self.filter_fftw_plan(IR_right_blocked[ir_block_count])


def setIR(self, filter, do_interpolation):
Expand Down Expand Up @@ -190,15 +193,15 @@ def fill_buffer_mono(self, block):

else:
# shift buffer
self.buffer[:self.block_size] = self.buffer[self.block_size:]
self.buffer = np.roll(self.buffer, -self.block_size)
# insert new block to buffer
self.buffer[self.block_size:] = block
self.buffer[self.block_size:self.block_size * 2] = block
# shift FDLs
self.FDL_left[self.block_size + 1:] = self.FDL_left[:self.FDL_size - (self.block_size + 1)]
self.FDL_right[self.block_size + 1:] = self.FDL_right[:self.FDL_size - (self.block_size + 1)]
self.FDL_left = np.roll(self.FDL_left, self.block_size + 1)
self.FDL_right = np.roll(self.FDL_right, self.block_size + 1)

# transform buffer into freq domain and copy to FDLs
self.FDL_left[:self.block_size + 1] = self.FDL_right[0:self.block_size + 1] = self.bufferFftPlan(
self.FDL_left[:self.block_size + 1] = self.FDL_right[:self.block_size + 1] = self.bufferFftPlan(
self.buffer)

def fill_buffer_stereo(self, block):
Expand All @@ -222,18 +225,18 @@ def fill_buffer_stereo(self, block):

else:
# shift buffer
self.buffer[:self.block_size] = self.buffer[self.block_size:]
self.buffer2[:self.block_size] = self.buffer2[self.block_size:]
self.buffer = np.roll(self.buffer, -self.block_size)
self.buffer2 = np.roll(self.buffer2, -self.block_size)
# insert new block to buffer
self.buffer[self.block_size:] = block[:, 0]
self.buffer2[self.block_size:] = block[:, 1]
# shift FDLs
self.FDL_left[self.block_size + 1:] = self.FDL_left[:self.FDL_size - (self.block_size + 1)]
self.FDL_right[self.block_size + 1:] = self.FDL_right[:self.FDL_size - (self.block_size + 1)]
self.FDL_left = np.roll(self.FDL_left, self.block_size + 1)
self.FDL_right = np.roll(self.FDL_right, self.block_size + 1)

# transform buffer into freq domain and copy to FDLs
self.FDL_left[0:self.block_size + 1] = self.bufferFftPlan()
self.FDL_right[0:self.block_size + 1] = self.buffer2FftPlan()
self.FDL_left[0:self.block_size + 1] = self.bufferFftPlan(self.buffer)
self.FDL_right[0:self.block_size + 1] = self.buffer2FftPlan(self.buffer2)

def multiply_and_add(self,IR_block_count,result,input1,input2):

Expand All @@ -255,7 +258,6 @@ def process(self, block):
:param block:
:return: (outputLeft, outputRight)
"""
# print("Convolver: process")

# First: Fill buffer and FDLs with current block
if not self.processStereo:
Expand All @@ -266,7 +268,7 @@ def process(self, block):
self.fill_buffer_stereo(block)

# Second: Multiplikation with IR block und accumulation with previous data
for irBlockCount in xrange(0, self.IR_blocks):
for irBlockCount in range(0, self.IR_blocks):
# Always convolute current filter
self.resultLeftFreq[:] = self.multiply_and_add(irBlockCount, self.resultLeftFreq,
self.TF_left_blocked, self.FDL_left)
Expand All @@ -281,18 +283,18 @@ def process(self, block):
self.TF_right_blocked_previous, self.FDL_right)

# Third: Transformation back to time domain
self.outputLeft = self.resultLeftIFFTPlan()[self.block_size:self.block_size * 2]
self.outputRight = self.resultRightIFFTPlan()[self.block_size:self.block_size * 2]
self.outputLeft = self.resultLeftIFFTPlan(self.resultLeftFreq)[self.block_size:self.block_size * 2]
self.outputRight = self.resultRightIFFTPlan(self.resultRightFreq)[self.block_size:self.block_size * 2]

if self.interpolate:
# fade over full block size
print('do block interpolation')
# print('do block interpolation')
self.outputLeft = np.add(np.multiply(self.outputLeft,self.crossFadeIn),
np.multiply(self.resultLeftPreviousIFFTPlan()[
np.multiply(self.resultLeftPreviousIFFTPlan(self.resultLeftFreqPrevious)[
self.block_size:self.block_size * 2], self.crossFadeOut))

self.outputRight = np.add(np.multiply(self.outputRight,self.crossFadeIn),
np.multiply(self.resultRightPreviousIFFTPlan()[
np.multiply(self.resultRightPreviousIFFTPlan(self.resultRightFreqPrevious)[
self.block_size:self.block_size * 2], self.crossFadeOut))

self.processCounter += 1
Expand Down
2 changes: 1 addition & 1 deletion pybinsim/osc_receiver.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ def handle_filter_input(self, identifier, channel, *args):
current_channel = channel

if args != self.valueList[current_channel]:
self.log.info("new filter")
#self.log.info("new filter")
self.filters_updated[current_channel] = True
self.valueList[current_channel] = tuple(args)
else:
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from setuptools import setup
from setuptools.command.test import test as TestCommand

VERSION = "1.2.0"
VERSION = "1.2.1"


class PyTest(TestCommand):
Expand Down

0 comments on commit ce2a025

Please sign in to comment.