Mercurial > hg > octave-nkf
annotate scripts/audio/soundsc.m @ 20742:2480bbcd1333
fix documentation for new viridis colormap
* colormap.m: Fix typo in spelling of viridis.
* image.txi: Document viridis.
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Thu, 01 Oct 2015 13:37:03 -0400 |
parents | 7503499a252b |
children |
rev | line source |
---|---|
19883
c2478360291f
New functions sound and soundsc
Mike Miller <mtmiller@ieee.org>
parents:
diff
changeset
|
1 ## Copyright (C) 2015 Mike Miller |
c2478360291f
New functions sound and soundsc
Mike Miller <mtmiller@ieee.org>
parents:
diff
changeset
|
2 ## |
c2478360291f
New functions sound and soundsc
Mike Miller <mtmiller@ieee.org>
parents:
diff
changeset
|
3 ## This file is part of Octave. |
c2478360291f
New functions sound and soundsc
Mike Miller <mtmiller@ieee.org>
parents:
diff
changeset
|
4 ## |
c2478360291f
New functions sound and soundsc
Mike Miller <mtmiller@ieee.org>
parents:
diff
changeset
|
5 ## Octave is free software; you can redistribute it and/or modify it |
c2478360291f
New functions sound and soundsc
Mike Miller <mtmiller@ieee.org>
parents:
diff
changeset
|
6 ## under the terms of the GNU General Public License as published by |
c2478360291f
New functions sound and soundsc
Mike Miller <mtmiller@ieee.org>
parents:
diff
changeset
|
7 ## the Free Software Foundation; either version 3 of the License, or (at |
c2478360291f
New functions sound and soundsc
Mike Miller <mtmiller@ieee.org>
parents:
diff
changeset
|
8 ## your option) any later version. |
c2478360291f
New functions sound and soundsc
Mike Miller <mtmiller@ieee.org>
parents:
diff
changeset
|
9 ## |
c2478360291f
New functions sound and soundsc
Mike Miller <mtmiller@ieee.org>
parents:
diff
changeset
|
10 ## Octave is distributed in the hope that it will be useful, but |
c2478360291f
New functions sound and soundsc
Mike Miller <mtmiller@ieee.org>
parents:
diff
changeset
|
11 ## WITHOUT ANY WARRANTY; without even the implied warranty of |
c2478360291f
New functions sound and soundsc
Mike Miller <mtmiller@ieee.org>
parents:
diff
changeset
|
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
c2478360291f
New functions sound and soundsc
Mike Miller <mtmiller@ieee.org>
parents:
diff
changeset
|
13 ## General Public License for more details. |
c2478360291f
New functions sound and soundsc
Mike Miller <mtmiller@ieee.org>
parents:
diff
changeset
|
14 ## |
c2478360291f
New functions sound and soundsc
Mike Miller <mtmiller@ieee.org>
parents:
diff
changeset
|
15 ## You should have received a copy of the GNU General Public License |
c2478360291f
New functions sound and soundsc
Mike Miller <mtmiller@ieee.org>
parents:
diff
changeset
|
16 ## along with Octave; see the file COPYING. If not, see |
c2478360291f
New functions sound and soundsc
Mike Miller <mtmiller@ieee.org>
parents:
diff
changeset
|
17 ## <http://www.gnu.org/licenses/>. |
c2478360291f
New functions sound and soundsc
Mike Miller <mtmiller@ieee.org>
parents:
diff
changeset
|
18 |
c2478360291f
New functions sound and soundsc
Mike Miller <mtmiller@ieee.org>
parents:
diff
changeset
|
19 ## -*- texinfo -*- |
c2478360291f
New functions sound and soundsc
Mike Miller <mtmiller@ieee.org>
parents:
diff
changeset
|
20 ## @deftypefn {Function File} {} soundsc (@var{y}) |
c2478360291f
New functions sound and soundsc
Mike Miller <mtmiller@ieee.org>
parents:
diff
changeset
|
21 ## @deftypefnx {Function File} {} soundsc (@var{y}, @var{fs}) |
c2478360291f
New functions sound and soundsc
Mike Miller <mtmiller@ieee.org>
parents:
diff
changeset
|
22 ## @deftypefnx {Function File} {} soundsc (@var{y}, @var{fs}, @var{nbits}) |
c2478360291f
New functions sound and soundsc
Mike Miller <mtmiller@ieee.org>
parents:
diff
changeset
|
23 ## @deftypefnx {Function File} {} soundsc (@dots{}, [@var{ymin}, @var{ymax}]) |
c2478360291f
New functions sound and soundsc
Mike Miller <mtmiller@ieee.org>
parents:
diff
changeset
|
24 ## Scale the audio data @var{y} and play it at sample rate @var{fs} to the |
20368
7503499a252b
doc: Update docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
20038
diff
changeset
|
25 ## default audio device. |
7503499a252b
doc: Update docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
20038
diff
changeset
|
26 ## |
7503499a252b
doc: Update docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
20038
diff
changeset
|
27 ## The audio signal @var{y} can be a vector or a two-column array, representing |
7503499a252b
doc: Update docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
20038
diff
changeset
|
28 ## mono or stereo audio, respectively. |
7503499a252b
doc: Update docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
20038
diff
changeset
|
29 ## |
7503499a252b
doc: Update docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
20038
diff
changeset
|
30 ## If @var{fs} is not given, a default sample rate of 8000 samples per second |
7503499a252b
doc: Update docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
20038
diff
changeset
|
31 ## is used. |
7503499a252b
doc: Update docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
20038
diff
changeset
|
32 ## |
7503499a252b
doc: Update docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
20038
diff
changeset
|
33 ## The optional argument @var{nbits} specifies the bit depth to play to the |
7503499a252b
doc: Update docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
20038
diff
changeset
|
34 ## audio device and defaults to 8 bits. |
19883
c2478360291f
New functions sound and soundsc
Mike Miller <mtmiller@ieee.org>
parents:
diff
changeset
|
35 ## |
c2478360291f
New functions sound and soundsc
Mike Miller <mtmiller@ieee.org>
parents:
diff
changeset
|
36 ## By default, @var{y} is automatically normalized to the range [-1, 1]. If the |
c2478360291f
New functions sound and soundsc
Mike Miller <mtmiller@ieee.org>
parents:
diff
changeset
|
37 ## range [@var{ymin}, @var{ymax}] is given, then elements of @var{y} that fall |
20000
ca7599ae464d
doc: Grammarcheck documentation ahead of 4.0 release.
Rik <rik@octave.org>
parents:
19883
diff
changeset
|
38 ## within the range @var{ymin} @leq{} @var{y} @leq{} @var{ymax} are scaled to |
ca7599ae464d
doc: Grammarcheck documentation ahead of 4.0 release.
Rik <rik@octave.org>
parents:
19883
diff
changeset
|
39 ## the range [-1, 1] instead. |
19883
c2478360291f
New functions sound and soundsc
Mike Miller <mtmiller@ieee.org>
parents:
diff
changeset
|
40 ## |
c2478360291f
New functions sound and soundsc
Mike Miller <mtmiller@ieee.org>
parents:
diff
changeset
|
41 ## For more control over audio playback, use the @code{audioplayer} class. |
20368
7503499a252b
doc: Update docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
20038
diff
changeset
|
42 ## @seealso{sound, record} |
19883
c2478360291f
New functions sound and soundsc
Mike Miller <mtmiller@ieee.org>
parents:
diff
changeset
|
43 ## @end deftypefn |
c2478360291f
New functions sound and soundsc
Mike Miller <mtmiller@ieee.org>
parents:
diff
changeset
|
44 |
c2478360291f
New functions sound and soundsc
Mike Miller <mtmiller@ieee.org>
parents:
diff
changeset
|
45 function soundsc (y, fs, nbits, yrange) |
c2478360291f
New functions sound and soundsc
Mike Miller <mtmiller@ieee.org>
parents:
diff
changeset
|
46 |
c2478360291f
New functions sound and soundsc
Mike Miller <mtmiller@ieee.org>
parents:
diff
changeset
|
47 if (nargin < 1 || nargin > 4) |
c2478360291f
New functions sound and soundsc
Mike Miller <mtmiller@ieee.org>
parents:
diff
changeset
|
48 print_usage (); |
c2478360291f
New functions sound and soundsc
Mike Miller <mtmiller@ieee.org>
parents:
diff
changeset
|
49 endif |
c2478360291f
New functions sound and soundsc
Mike Miller <mtmiller@ieee.org>
parents:
diff
changeset
|
50 |
c2478360291f
New functions sound and soundsc
Mike Miller <mtmiller@ieee.org>
parents:
diff
changeset
|
51 if (nargin < 4) |
c2478360291f
New functions sound and soundsc
Mike Miller <mtmiller@ieee.org>
parents:
diff
changeset
|
52 yrange = []; |
c2478360291f
New functions sound and soundsc
Mike Miller <mtmiller@ieee.org>
parents:
diff
changeset
|
53 endif |
c2478360291f
New functions sound and soundsc
Mike Miller <mtmiller@ieee.org>
parents:
diff
changeset
|
54 |
c2478360291f
New functions sound and soundsc
Mike Miller <mtmiller@ieee.org>
parents:
diff
changeset
|
55 if (nargin < 2 || isempty (fs)) |
c2478360291f
New functions sound and soundsc
Mike Miller <mtmiller@ieee.org>
parents:
diff
changeset
|
56 fs = 8000; |
20038
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
20000
diff
changeset
|
57 elseif (nargin == 2 && numel (fs) > 1) |
19883
c2478360291f
New functions sound and soundsc
Mike Miller <mtmiller@ieee.org>
parents:
diff
changeset
|
58 yrange = fs; |
c2478360291f
New functions sound and soundsc
Mike Miller <mtmiller@ieee.org>
parents:
diff
changeset
|
59 fs = 8000; |
c2478360291f
New functions sound and soundsc
Mike Miller <mtmiller@ieee.org>
parents:
diff
changeset
|
60 elseif (! (isscalar (fs) && (fs > 0))) |
c2478360291f
New functions sound and soundsc
Mike Miller <mtmiller@ieee.org>
parents:
diff
changeset
|
61 error ("soundsc: sample rate FS must be a positive number"); |
c2478360291f
New functions sound and soundsc
Mike Miller <mtmiller@ieee.org>
parents:
diff
changeset
|
62 endif |
c2478360291f
New functions sound and soundsc
Mike Miller <mtmiller@ieee.org>
parents:
diff
changeset
|
63 |
c2478360291f
New functions sound and soundsc
Mike Miller <mtmiller@ieee.org>
parents:
diff
changeset
|
64 if (nargin < 3 || isempty (nbits)) |
c2478360291f
New functions sound and soundsc
Mike Miller <mtmiller@ieee.org>
parents:
diff
changeset
|
65 nbits = 8; |
20038
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
20000
diff
changeset
|
66 elseif (nargin == 3 && numel (nbits) > 1) |
19883
c2478360291f
New functions sound and soundsc
Mike Miller <mtmiller@ieee.org>
parents:
diff
changeset
|
67 yrange = nbits; |
c2478360291f
New functions sound and soundsc
Mike Miller <mtmiller@ieee.org>
parents:
diff
changeset
|
68 nbits = 8; |
c2478360291f
New functions sound and soundsc
Mike Miller <mtmiller@ieee.org>
parents:
diff
changeset
|
69 elseif (! (isscalar (nbits) && (nbits == 8 || nbits == 16 || nbits == 24))) |
c2478360291f
New functions sound and soundsc
Mike Miller <mtmiller@ieee.org>
parents:
diff
changeset
|
70 error ("soundsc: bit depth NBITS must be 8, 16, or 24"); |
c2478360291f
New functions sound and soundsc
Mike Miller <mtmiller@ieee.org>
parents:
diff
changeset
|
71 endif |
c2478360291f
New functions sound and soundsc
Mike Miller <mtmiller@ieee.org>
parents:
diff
changeset
|
72 |
c2478360291f
New functions sound and soundsc
Mike Miller <mtmiller@ieee.org>
parents:
diff
changeset
|
73 if (isreal (yrange) && (numel (yrange) == 2) && (yrange(1) <= yrange(2))) |
c2478360291f
New functions sound and soundsc
Mike Miller <mtmiller@ieee.org>
parents:
diff
changeset
|
74 ymin = yrange(1); |
c2478360291f
New functions sound and soundsc
Mike Miller <mtmiller@ieee.org>
parents:
diff
changeset
|
75 ymax = yrange(2); |
c2478360291f
New functions sound and soundsc
Mike Miller <mtmiller@ieee.org>
parents:
diff
changeset
|
76 elseif (isempty (yrange)) |
c2478360291f
New functions sound and soundsc
Mike Miller <mtmiller@ieee.org>
parents:
diff
changeset
|
77 ymin = min (y(:)); |
c2478360291f
New functions sound and soundsc
Mike Miller <mtmiller@ieee.org>
parents:
diff
changeset
|
78 ymax = max (y(:)); |
c2478360291f
New functions sound and soundsc
Mike Miller <mtmiller@ieee.org>
parents:
diff
changeset
|
79 else |
c2478360291f
New functions sound and soundsc
Mike Miller <mtmiller@ieee.org>
parents:
diff
changeset
|
80 error ("soundsc: audio range must be a 2-element [YMIN, YMAX] vector"); |
c2478360291f
New functions sound and soundsc
Mike Miller <mtmiller@ieee.org>
parents:
diff
changeset
|
81 endif |
c2478360291f
New functions sound and soundsc
Mike Miller <mtmiller@ieee.org>
parents:
diff
changeset
|
82 |
c2478360291f
New functions sound and soundsc
Mike Miller <mtmiller@ieee.org>
parents:
diff
changeset
|
83 ymin = double (ymin); |
c2478360291f
New functions sound and soundsc
Mike Miller <mtmiller@ieee.org>
parents:
diff
changeset
|
84 ymax = double (ymax); |
c2478360291f
New functions sound and soundsc
Mike Miller <mtmiller@ieee.org>
parents:
diff
changeset
|
85 ymedian = (ymax + ymin) / 2; |
c2478360291f
New functions sound and soundsc
Mike Miller <mtmiller@ieee.org>
parents:
diff
changeset
|
86 yscale = 2 / (ymax - ymin); |
c2478360291f
New functions sound and soundsc
Mike Miller <mtmiller@ieee.org>
parents:
diff
changeset
|
87 |
c2478360291f
New functions sound and soundsc
Mike Miller <mtmiller@ieee.org>
parents:
diff
changeset
|
88 y = (double (y) - ymedian) .* yscale; |
c2478360291f
New functions sound and soundsc
Mike Miller <mtmiller@ieee.org>
parents:
diff
changeset
|
89 |
c2478360291f
New functions sound and soundsc
Mike Miller <mtmiller@ieee.org>
parents:
diff
changeset
|
90 play = audioplayer (y, fs, nbits); |
c2478360291f
New functions sound and soundsc
Mike Miller <mtmiller@ieee.org>
parents:
diff
changeset
|
91 |
c2478360291f
New functions sound and soundsc
Mike Miller <mtmiller@ieee.org>
parents:
diff
changeset
|
92 playblocking (play); |
c2478360291f
New functions sound and soundsc
Mike Miller <mtmiller@ieee.org>
parents:
diff
changeset
|
93 |
c2478360291f
New functions sound and soundsc
Mike Miller <mtmiller@ieee.org>
parents:
diff
changeset
|
94 endfunction |
c2478360291f
New functions sound and soundsc
Mike Miller <mtmiller@ieee.org>
parents:
diff
changeset
|
95 |
c2478360291f
New functions sound and soundsc
Mike Miller <mtmiller@ieee.org>
parents:
diff
changeset
|
96 |
c2478360291f
New functions sound and soundsc
Mike Miller <mtmiller@ieee.org>
parents:
diff
changeset
|
97 ## Tests of soundsc must not actually play anything. |
c2478360291f
New functions sound and soundsc
Mike Miller <mtmiller@ieee.org>
parents:
diff
changeset
|
98 |
20038
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
20000
diff
changeset
|
99 ## Test input validation |
19883
c2478360291f
New functions sound and soundsc
Mike Miller <mtmiller@ieee.org>
parents:
diff
changeset
|
100 %!error soundsc () |
c2478360291f
New functions sound and soundsc
Mike Miller <mtmiller@ieee.org>
parents:
diff
changeset
|
101 %!error soundsc (1,2,3,4,5) |
c2478360291f
New functions sound and soundsc
Mike Miller <mtmiller@ieee.org>
parents:
diff
changeset
|
102 %!error soundsc (1, -1) |
c2478360291f
New functions sound and soundsc
Mike Miller <mtmiller@ieee.org>
parents:
diff
changeset
|
103 %!error soundsc (1, [], 2) |
c2478360291f
New functions sound and soundsc
Mike Miller <mtmiller@ieee.org>
parents:
diff
changeset
|
104 %!error soundsc (1, [2 1]) |
c2478360291f
New functions sound and soundsc
Mike Miller <mtmiller@ieee.org>
parents:
diff
changeset
|
105 %!error soundsc (1, [1 2 3]) |
c2478360291f
New functions sound and soundsc
Mike Miller <mtmiller@ieee.org>
parents:
diff
changeset
|
106 %!error soundsc (1, 8000, [2 1]) |
c2478360291f
New functions sound and soundsc
Mike Miller <mtmiller@ieee.org>
parents:
diff
changeset
|
107 %!error soundsc (1, 8000, [1 2 3]) |
c2478360291f
New functions sound and soundsc
Mike Miller <mtmiller@ieee.org>
parents:
diff
changeset
|
108 %!error soundsc (1, 8000, 8, [2 1]) |
c2478360291f
New functions sound and soundsc
Mike Miller <mtmiller@ieee.org>
parents:
diff
changeset
|
109 %!error soundsc (1, 8000, 8, [1 2 3]) |
c2478360291f
New functions sound and soundsc
Mike Miller <mtmiller@ieee.org>
parents:
diff
changeset
|
110 |