IC Python API:Audio Visualizer

From Reallusion Wiki!
Revision as of 20:41, 23 June 2019 by Chuck (RL) (Talk | contribs) (Created page with "{{TOC}} {{Parent|IC_Python_API:RL_Python_Samples|RL Python Samples}} == Required Modules == Besides the rudimentary Reallusion Python API we'll need the following modules: *...")

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search
Main article: RL Python Samples.

Required Modules

Besides the rudimentary Reallusion Python API we'll need the following modules:

  • os for reading from the Windows file system.
  • numpy for transforming arrays.
  • wave for reading wav file data.
  • Pyside2 for building the user interface.
import RLPy
import os
import numpy as np
import wave
from PySide2 import *
from PySide2.shiboken2 import wrapInstance

User Interface

For the sake of convenience, we'll be reading from a QT UI file. You can download Audio_Visualizer.ui File:Audio Visualizer.ui.

window = RLPy.RUi.CreateRDockWidget()
window.SetWindowTitle("Audio Visualizer (Mono)")
window.SetAllowedAreas(RLPy.EDockWidgetAreas_BottomDockWidgetArea)

dock = wrapInstance(int(window.GetWindow()), QtWidgets.QDockWidget)
dock.resize(1000, 300)

ui = QtCore.QFile(os.path.dirname(__file__) + "/Audio_Visualizer.ui")
ui.open(QtCore.QFile.ReadOnly)
widget = QtUiTools.QUiLoader().load(ui)
ui.close()

widget.progressBar.setVisible(False)

dock.setWidget(widget)

Audio Bit-Depth

Audio files just like images can be save with certain bit-depths depending on the fidelity of the audio to the original sound quality. Just like images, audio files with higher bit-depths have better "accuracy" and are more faithful to the recorded source audio.

Without installing and relying on the soundfile module, we can guess the bit-depth from the maximum signal value of an audio file. For example, if the highest signal magnitude (signed) is 32,767 then it is definitely higher than 8bit (256) and lower than 16bit (65,536) so it's safe to assume that the audio is 16 bit.

We can use the following function to tease out the bit-depth:

<syntaxhhighlight lang="python"> def guess_bit_depth(value):

   bits = [2**8., 2**16., 2**24., 2**32.]
   bits.append(value)
   bits.sort()
   bit_depth = bits[bits.index(value)+1]
   return bit_depth

</syntaxhighlight>

Again, this is a crude implementation, for more robust method you should use the soundfile module and read the audio sub-type attribute.