]> Creatis software - CreaPhase.git/blob - octave_packages/audio-1.1.4/soundsc.m
Add a useful package (from Source forge) for octave
[CreaPhase.git] / octave_packages / audio-1.1.4 / soundsc.m
1 ## Copyright (C) 2000 Paul Kienzle
2 ##
3 ## This program is free software; you can redistribute it and/or modify
4 ## it under the terms of the GNU General Public License as published by
5 ## the Free Software Foundation; either version 2 of the License, or
6 ## (at your option) any later version.
7 ##
8 ## This program is distributed in the hope that it will be useful,
9 ## but WITHOUT ANY WARRANTY; without even the implied warranty of
10 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11 ## GNU General Public License for more details.
12 ##
13 ## You should have received a copy of the GNU General Public License
14 ## along with this program; If not, see <http://www.gnu.org/licenses/>.
15
16 ## usage: soundsc(x, fs, limit) or soundsc(x, fs, [ lo, hi ])
17 ##
18 ## soundsc(x)
19 ##    Scale the signal so that [min(x), max(x)] -> [-1, 1], then 
20 ##    play it through the speakers at 8000 Hz sampling rate.  The
21 ##    signal has one column per channel.  
22 ##
23 ## soundsc(x,fs)
24 ##    Scale the signal and play it at sampling rate fs.
25 ##
26 ## soundsc(x, fs, limit)
27 ##    Scale the signal so that [-|limit|, |limit|] -> [-1, 1], then
28 ##    play it at sampling rate fs.  If fs is empty, then the default
29 ##    8000 Hz sampling rate is used.
30 ##
31 ## soundsc(x, fs, [ lo, hi ])
32 ##    Scale the signal so that [lo, hi] -> [-1, 1], then play it
33 ##    at sampling rate fs.  If fs is empty, then the default 8000 Hz
34 ##    sampling rate is used.
35 ##
36 ## y=soundsc(...)
37 ##    return the scaled waveform rather than play it.
38 ##
39 ## See sound for more information.
40
41 function data_r = soundsc(data, rate, range)
42
43   if nargin < 1 || nargin > 3, usage("soundsc(x, fs, [lo, hi])") endif
44   if nargin < 2, rate = []; endif
45   if nargin < 3, range = [min(data(:)), max(data(:))]; endif
46   if isscalar(range), range = [-abs(range), abs(range)]; endif
47   
48   data=(data - mean(range))/((range(2)-range(1))/2);
49   if nargout > 0
50     data_r = data;
51   else
52     sound(data, rate);
53   endif
54 endfunction
55
56
57 %!demo
58 %! [x, fs] = auload(file_in_loadpath("sample.wav"));
59 %! soundsc(x,fs);
60
61 %!shared y
62 %! [x, fs] = auload(file_in_loadpath("sample.wav"));
63 %! y=soundsc(x);
64 %!assert (min(y(:)), -1, eps)
65 %!assert (max(y(:)), 1, eps)