This page explains how you can incorporate the OpenVokaturi software in your own Python app.

The following example reads a WAV file from disk and analyzes its emotions, on the Mac. This example script is in a folder called examples, and next to this folder there is a folder api, which contains the module file Vokaturi.py, and a folder lib, which contains the library file Vokaturi_mac.so.

# measure_wav.py
# Paul Boersma 2017-01-03
#
# A sample script that uses the Vokaturi library to extract the emotions from
# a wav file on disk. The file can contain a mono or stereo recording.
#
# Call syntax:
#   python3 measure_wav.py path_to_sound_file.wav

import sys
import scipy.io.wavfile

sys.path.append("../api")
import Vokaturi

print("Loading library...")
Vokaturi.load("../lib/Vokaturi_mac.so")
print("Analyzed by: %s" % Vokaturi.versionAndLicense())

print("Reading sound file...")
file_name = sys.argv[1]
(sample_rate, samples) = scipy.io.wavfile.read(file_name)
print("   sample rate %.3f Hz" % sample_rate)

print("Allocating Vokaturi sample array...")
buffer_length = len(samples)
print("   %d samples, %d channels" % (buffer_length, samples.ndim))
c_buffer = Vokaturi.SampleArrayC(buffer_length)
if samples.ndim == 1:
    c_buffer[:] = samples[:] / 32768.0  # mono
else:
    c_buffer[:] = 0.5*(samples[:,0]+0.0+samples[:,1]) / 32768.0  # stereo

print("Creating VokaturiVoice...")
voice = Vokaturi.Voice (sample_rate, buffer_length)

print("Filling VokaturiVoice with samples...")
voice.fill(buffer_length, c_buffer)

print("Extracting emotions from VokaturiVoice...")
quality = Vokaturi.Quality()
emotionProbabilities = Vokaturi.EmotionProbabilities()
voice.extract(quality, emotionProbabilities)

if quality.valid:
    print("Neutral: %.3f" % emotionProbabilities.neutrality)
    print("Happy: %.3f" % emotionProbabilities.happiness)
    print("Sad: %.3f" % emotionProbabilities.sadness)
    print("Angry: %.3f" % emotionProbabilities.anger)
    print("Fear: %.3f" % emotionProbabilities.fear)

voice.destroy()

The trick in this “batch” example is to create a Vokaturi.Voice that is big enough to contain all samples, then use Vokaturi.fill() only once, i.e. to transfer all the samples of the sound file into the Vokaturi.Voice object in one go. The analysis by Vokaturi.extract() will then report the emotions averaged over the whole file.

On Windows, you replace Vokaturi_mac.so with Vokaturi_win32.dll or Vokaturi_win64.dll, and you may have to put libgcc_s_sjlj-1.dll in the same folder as your script. On 64-bit Linux, you replace Vokaturi_mac.so with Vokaturi_linux64.so.