# HG changeset patch # User Mike Miller # Date 1424536315 18000 # Node ID f9ca80482c41c919855f48333fee86ce457e12e1 # Parent f24d9486e66be4aaa461f759cc6aa9af5aee55cc audiowrite: Scale integer inputs before writing to an audio file * audioread.cc (Faudiowrite): Check for recognized integer input types and scale accordingly to the range [-1,1] before writing. Add clipping of values outside the range [-1,1] after normalizing. diff -r f24d9486e66b -r f9ca80482c41 libinterp/dldfcn/audioread.cc --- a/libinterp/dldfcn/audioread.cc Sat Feb 21 11:08:12 2015 -0500 +++ b/libinterp/dldfcn/audioread.cc Sat Feb 21 11:31:55 2015 -0500 @@ -311,6 +311,21 @@ if (error_state) return retval; + double bias = 0.0; + double scale = 1.0; + + if (args(1).is_uint8_type ()) + bias = scale = std::pow (2.0, 7); + else if (args(1).is_int16_type ()) + scale = std::pow (2.0, 15); + else if (args(1).is_int32_type ()) + scale = std::pow (2.0, 31); + else if (args(1).is_integer_type ()) + { + gripe_wrong_type_arg ("audiowrite", args(1)); + return retval; + } + int samplerate = args(2).int_value (); if (error_state) @@ -327,7 +342,10 @@ for (int i = 0; i < audio.rows (); i++) { for (int j = 0; j < audio.columns (); j++) - data[idx++] = audio.xelem (i, j); + { + double elem = (audio.xelem (i, j) - bias) / scale; + data[idx++] = std::min (std::max (elem, -1.0), 1.0); + } } SF_INFO info;