| 1 |
#!/usr/bin/python |
| 2 |
# |
| 3 |
# Project: lib Instrument Patch Python testing suite |
| 4 |
# File: SF2.py |
| 5 |
# Descr: SoundFont tests |
| 6 |
# Author: Element Green <[email protected]> |
| 7 |
# Date: 2016-07-01 |
| 8 |
# License: Public Domain |
| 9 |
# |
| 10 |
""" |
| 11 |
Convert between all possible audio format pairs using the following steps: |
| 12 |
- Convert double to first format of format pair |
| 13 |
- Convert from first format to second format |
| 14 |
- Convert second format back to double |
| 15 |
- Calculate maximum audio sample difference between new double audio and original |
| 16 |
""" |
| 17 |
|
| 18 |
import Test |
| 19 |
import gi |
| 20 |
import ctypes |
| 21 |
import math |
| 22 |
import sys |
| 23 |
|
| 24 |
from gi.repository import GObject |
| 25 |
|
| 26 |
gi.require_version ('Ipatch', '1.1') |
| 27 |
from gi.repository import Ipatch |
| 28 |
|
| 29 |
# Default values are non powers of 2 to decrease the chance of masking periodic data issues |
| 30 |
DEFAULT_SAMPLE_SIZE = 1756 # default test waveform size in samples |
| 31 |
DEFAULT_SAMPLE_PERIOD = 123 # default waveform period in samples |
| 32 |
DEFAULT_TRANSFORM_SIZE = 65000 # default transform buffer size in bytes |
| 33 |
MAX_DIFF_ALLOWED = 0.016 # maximum difference allowed |
| 34 |
|
| 35 |
# all available sample format combinations |
| 36 |
testFormats = ( |
| 37 |
Ipatch.SampleWidth.BIT8, |
| 38 |
Ipatch.SampleWidth.BIT16, |
| 39 |
Ipatch.SampleWidth.BIT24, |
| 40 |
Ipatch.SampleWidth.BIT32, |
| 41 |
Ipatch.SampleWidth.FLOAT, |
| 42 |
Ipatch.SampleWidth.DOUBLE, |
| 43 |
Ipatch.SampleWidth.REAL24BIT, |
| 44 |
|
| 45 |
Ipatch.SampleWidth.BIT8 | Ipatch.SampleSign.UNSIGNED, |
| 46 |
Ipatch.SampleWidth.BIT16 | Ipatch.SampleSign.UNSIGNED, |
| 47 |
Ipatch.SampleWidth.BIT24 | Ipatch.SampleSign.UNSIGNED, |
| 48 |
Ipatch.SampleWidth.BIT32 | Ipatch.SampleSign.UNSIGNED, |
| 49 |
Ipatch.SampleWidth.REAL24BIT | Ipatch.SampleSign.UNSIGNED, |
| 50 |
|
| 51 |
Ipatch.SampleWidth.BIT16 | Ipatch.SampleEndian.BENDIAN, |
| 52 |
Ipatch.SampleWidth.BIT24 | Ipatch.SampleEndian.BENDIAN, |
| 53 |
Ipatch.SampleWidth.BIT32 | Ipatch.SampleEndian.BENDIAN, |
| 54 |
Ipatch.SampleWidth.FLOAT | Ipatch.SampleEndian.BENDIAN, |
| 55 |
Ipatch.SampleWidth.DOUBLE | Ipatch.SampleEndian.BENDIAN, |
| 56 |
Ipatch.SampleWidth.REAL24BIT | Ipatch.SampleEndian.BENDIAN, |
| 57 |
|
| 58 |
Ipatch.SampleWidth.BIT16 | Ipatch.SampleSign.UNSIGNED | Ipatch.SampleEndian.BENDIAN, |
| 59 |
Ipatch.SampleWidth.BIT24 | Ipatch.SampleSign.UNSIGNED | Ipatch.SampleEndian.BENDIAN, |
| 60 |
Ipatch.SampleWidth.BIT32 | Ipatch.SampleSign.UNSIGNED | Ipatch.SampleEndian.BENDIAN, |
| 61 |
Ipatch.SampleWidth.REAL24BIT | Ipatch.SampleSign.UNSIGNED | Ipatch.SampleEndian.BENDIAN |
| 62 |
) |
| 63 |
|
| 64 |
testFormatNames = ( |
| 65 |
"8bit-signed", |
| 66 |
"16bit-signed-lendian", |
| 67 |
"24bit-signed-lendian", |
| 68 |
"32bit-signed-lendian", |
| 69 |
"float-lendian", |
| 70 |
"double-lendian", |
| 71 |
"real24bit-signed-lendian", |
| 72 |
|
| 73 |
"8bit-unsigned", |
| 74 |
"16bit-unsigned-lendian", |
| 75 |
"24bit-unsigned-lendian", |
| 76 |
"32bit-unsigned-lendian", |
| 77 |
"real24bit-unsigned-lendian", |
| 78 |
|
| 79 |
"16bit-signed-bendian", |
| 80 |
"24bit-signed-bendian", |
| 81 |
"32bit-signed-bendian", |
| 82 |
"float-bendian", |
| 83 |
"double-bendian", |
| 84 |
"real24bit-signed-bendian", |
| 85 |
|
| 86 |
"16bit-unsigned-bendian", |
| 87 |
"24bit-unsigned-bendian", |
| 88 |
"32bit-unsigned-bendian", |
| 89 |
"real24bit-unsigned-bendian" |
| 90 |
) |
| 91 |
|
| 92 |
|
| 93 |
parser = Test.createArgParser ('libInstPatch audio sample transform tests') |
| 94 |
parser.add_argument ( '-s', '--size', type=int, default=DEFAULT_SAMPLE_SIZE, help='Size of audio to transform in frames') |
| 95 |
parser.add_argument ('-p', '--period', type=int, default=DEFAULT_SAMPLE_PERIOD, help='Period size in frames') |
| 96 |
parser.add_argument ('-t', '--trans', type=int, default=DEFAULT_TRANSFORM_SIZE, help='Transform size in bytes') |
| 97 |
args = Test.parseArgs (parser) |
| 98 |
|
| 99 |
Ipatch.init () |
| 100 |
|
| 101 |
# Determine host endian byte order |
| 102 |
hostEndian = Ipatch.SampleEndian.BENDIAN if sys.byteorder == 'big' else Ipatch.SampleEndian.LENDIAN |
| 103 |
|
| 104 |
# Create double floating point sample data audio |
| 105 |
sampleSize = args.size |
| 106 |
period = args.period |
| 107 |
sampleDataArray = (ctypes.c_double * sampleSize)() |
| 108 |
|
| 109 |
for pos in xrange (0, sampleSize): |
| 110 |
periodPos = math.fmod (pos, period) / period |
| 111 |
sampleDataArray[pos] = math.sin (periodPos * 2 * math.pi) |
| 112 |
|
| 113 |
sampleData = bytearray (sampleDataArray) |
| 114 |
|
| 115 |
# Create sample transform object and initialize size |
| 116 |
trans = Ipatch.SampleTransform () |
| 117 |
trans.alloc_size (args.trans) |
| 118 |
|
| 119 |
failcount = 0 |
| 120 |
max_maxdiff = 0.0 |
| 121 |
|
| 122 |
for isrc in xrange (0, len (testFormats)): |
| 123 |
srcform = testFormats[isrc] |
| 124 |
|
| 125 |
for idest in xrange (0, len (testFormats)): |
| 126 |
destform = testFormats[idest] |
| 127 |
|
| 128 |
# convert generated double floating point waveform to source format |
| 129 |
trans.set_formats (Ipatch.SampleWidth.DOUBLE | hostEndian, srcform, |
| 130 |
Ipatch.SAMPLE_UNITY_CHANNEL_MAP) |
| 131 |
srcData = trans.convert (sampleData) |
| 132 |
|
| 133 |
# convert source format to destination format |
| 134 |
trans.set_formats (srcform, destform, Ipatch.SAMPLE_UNITY_CHANNEL_MAP) |
| 135 |
destData = trans.convert (srcData) |
| 136 |
|
| 137 |
# convert destination format to final double output |
| 138 |
trans.set_formats (destform, Ipatch.SampleWidth.DOUBLE | hostEndian, |
| 139 |
Ipatch.SAMPLE_UNITY_CHANNEL_MAP) |
| 140 |
finalData = trans.convert (destData) |
| 141 |
finalDataArray = (ctypes.c_double * sampleSize).from_buffer_copy (finalData) |
| 142 |
|
| 143 |
maxdiff = 0.0 |
| 144 |
maxindex = 0 |
| 145 |
|
| 146 |
# compare final waveform against original |
| 147 |
for i in xrange (0, sampleSize): |
| 148 |
d = abs (sampleDataArray[i] - finalDataArray[i]) |
| 149 |
|
| 150 |
if d > maxdiff: |
| 151 |
maxdiff = d |
| 152 |
maxindex = i |
| 153 |
|
| 154 |
if maxdiff > max_maxdiff: |
| 155 |
max_maxdiff = maxdiff |
| 156 |
|
| 157 |
if maxdiff <= MAX_DIFF_ALLOWED: |
| 158 |
Test.info ("Converted %s to %s: maxdiff=%0.6f, index=%d" |
| 159 |
% (testFormatNames[isrc], testFormatNames[idest], maxdiff, maxindex)) |
| 160 |
else: |
| 161 |
Test.error ("FAILED Converting %s to %s: maxdiff=%0.6f, index=%d" |
| 162 |
% (testFormatNames[isrc], testFormatNames[idest], maxdiff, maxindex)) |
| 163 |
failcount += 1 |
| 164 |
|
| 165 |
if failcount > 0: |
| 166 |
Test.error ("%d of %d format conversions FAILED (max diff=%0.6f)" % (failcount, len (testFormats) ** 2, max_maxdiff)) |
| 167 |
else: Test.msg ("All %d sample format conversions PASSED (max diff=%0.6f)" % (len (testFormats) ** 2, max_maxdiff)) |
| 168 |
|
| 169 |
|
| 170 |
Test.exit () |
| 171 |
|