annotate scripts/audio/@audiorecorder/audiorecorder.m @ 19668:b2fe4dbe5266

Don't play or record audio during 'make check' * @audioplayer/audioplayer.m, @audiorecorder/audiorecorder.m: Convert %!test blocks that play and record audio into %!demo blocks. Keep %!tests that can validate properties without playing or recording.
author Mike Miller <mtmiller@ieee.org>
date Sat, 07 Feb 2015 10:47:25 -0500
parents 7f5006dfa2eb
children 4197fc428c7d
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
19500
8bb399569393 Add Octave copyright header block to audio project files
Mike Miller <mtmiller@ieee.org>
parents: 19497
diff changeset
1 ## Copyright (C) 2013 Vytautas JanĨauskas
8bb399569393 Add Octave copyright header block to audio project files
Mike Miller <mtmiller@ieee.org>
parents: 19497
diff changeset
2 ##
8bb399569393 Add Octave copyright header block to audio project files
Mike Miller <mtmiller@ieee.org>
parents: 19497
diff changeset
3 ## This file is part of Octave.
8bb399569393 Add Octave copyright header block to audio project files
Mike Miller <mtmiller@ieee.org>
parents: 19497
diff changeset
4 ##
8bb399569393 Add Octave copyright header block to audio project files
Mike Miller <mtmiller@ieee.org>
parents: 19497
diff changeset
5 ## Octave is free software; you can redistribute it and/or modify it
8bb399569393 Add Octave copyright header block to audio project files
Mike Miller <mtmiller@ieee.org>
parents: 19497
diff changeset
6 ## under the terms of the GNU General Public License as published by
8bb399569393 Add Octave copyright header block to audio project files
Mike Miller <mtmiller@ieee.org>
parents: 19497
diff changeset
7 ## the Free Software Foundation; either version 3 of the License, or (at
8bb399569393 Add Octave copyright header block to audio project files
Mike Miller <mtmiller@ieee.org>
parents: 19497
diff changeset
8 ## your option) any later version.
8bb399569393 Add Octave copyright header block to audio project files
Mike Miller <mtmiller@ieee.org>
parents: 19497
diff changeset
9 ##
8bb399569393 Add Octave copyright header block to audio project files
Mike Miller <mtmiller@ieee.org>
parents: 19497
diff changeset
10 ## Octave is distributed in the hope that it will be useful, but
8bb399569393 Add Octave copyright header block to audio project files
Mike Miller <mtmiller@ieee.org>
parents: 19497
diff changeset
11 ## WITHOUT ANY WARRANTY; without even the implied warranty of
8bb399569393 Add Octave copyright header block to audio project files
Mike Miller <mtmiller@ieee.org>
parents: 19497
diff changeset
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
8bb399569393 Add Octave copyright header block to audio project files
Mike Miller <mtmiller@ieee.org>
parents: 19497
diff changeset
13 ## General Public License for more details.
8bb399569393 Add Octave copyright header block to audio project files
Mike Miller <mtmiller@ieee.org>
parents: 19497
diff changeset
14 ##
8bb399569393 Add Octave copyright header block to audio project files
Mike Miller <mtmiller@ieee.org>
parents: 19497
diff changeset
15 ## You should have received a copy of the GNU General Public License
8bb399569393 Add Octave copyright header block to audio project files
Mike Miller <mtmiller@ieee.org>
parents: 19497
diff changeset
16 ## along with Octave; see the file COPYING. If not, see
8bb399569393 Add Octave copyright header block to audio project files
Mike Miller <mtmiller@ieee.org>
parents: 19497
diff changeset
17 ## <http://www.gnu.org/licenses/>.
8bb399569393 Add Octave copyright header block to audio project files
Mike Miller <mtmiller@ieee.org>
parents: 19497
diff changeset
18
19476
e1f98e402a7e New files - @audiorecorder and @audioplayer classes
Vytautas Jančauskas <unaudio@gmail.com>
parents:
diff changeset
19 ## -*- texinfo -*-
19571
72304a4e010a Use Octave coding conventions for documentation of audio functions.
Rik <rik@octave.org>
parents: 19560
diff changeset
20 ## @deftypefn {Function File} {@var{recorder} =} audiorecorder ()
19506
fdb8a62ef17a fix docstring style in .m files
John W. Eaton <jwe@octave.org>
parents: 19505
diff changeset
21 ## @deftypefnx {Function File} {@var{recorder} =} audiorecorder (@var{fs}, @var{nbits}, @var{channels})
fdb8a62ef17a fix docstring style in .m files
John W. Eaton <jwe@octave.org>
parents: 19505
diff changeset
22 ## @deftypefnx {Function File} {@var{recorder} =} audiorecorder (@var{fs}, @var{nbits}, @var{channels}, @var{id})
19505
ce02743b6f2a Fix texinfo docstring syntax for all audio functions
Mike Miller <mtmiller@ieee.org>
parents: 19503
diff changeset
23 ## Create an audiorecorder object recording 8 bit mono audio at 8000 Hz
19506
fdb8a62ef17a fix docstring style in .m files
John W. Eaton <jwe@octave.org>
parents: 19505
diff changeset
24 ## sample rate. The optional arguments @var{fs}, @var{nbits},
fdb8a62ef17a fix docstring style in .m files
John W. Eaton <jwe@octave.org>
parents: 19505
diff changeset
25 ## @var{channels}, and @var{id} specify the sample rate, bit depth,
fdb8a62ef17a fix docstring style in .m files
John W. Eaton <jwe@octave.org>
parents: 19505
diff changeset
26 ## number of channels and recording device id, respectively. Device IDs
fdb8a62ef17a fix docstring style in .m files
John W. Eaton <jwe@octave.org>
parents: 19505
diff changeset
27 ## may be found using the audiodevinfo function.
19560
e75df9e43e63 disable Octave audio callback functions
John W. Eaton <jwe@octave.org>
parents: 19528
diff changeset
28 ## @end deftypefn
e75df9e43e63 disable Octave audio callback functions
John W. Eaton <jwe@octave.org>
parents: 19528
diff changeset
29
e75df9e43e63 disable Octave audio callback functions
John W. Eaton <jwe@octave.org>
parents: 19528
diff changeset
30 ## FIXME: callbacks don't work properly, apparently because portaudio
e75df9e43e63 disable Octave audio callback functions
John W. Eaton <jwe@octave.org>
parents: 19528
diff changeset
31 ## will execute the callbacks in a separate thread, and calling Octave
e75df9e43e63 disable Octave audio callback functions
John W. Eaton <jwe@octave.org>
parents: 19528
diff changeset
32 ## functions in a separate thread which is likely to cause trouble with
e75df9e43e63 disable Octave audio callback functions
John W. Eaton <jwe@octave.org>
parents: 19528
diff changeset
33 ## all of Octave's global data...
e75df9e43e63 disable Octave audio callback functions
John W. Eaton <jwe@octave.org>
parents: 19528
diff changeset
34 ##
e75df9e43e63 disable Octave audio callback functions
John W. Eaton <jwe@octave.org>
parents: 19528
diff changeset
35 ## @deftypefnx {Function File} {@var{recorder} =} audiorecorder (@var{function}, @dots{})
e75df9e43e63 disable Octave audio callback functions
John W. Eaton <jwe@octave.org>
parents: 19528
diff changeset
36 ##
19506
fdb8a62ef17a fix docstring style in .m files
John W. Eaton <jwe@octave.org>
parents: 19505
diff changeset
37 ## Given a function handle, use that function to process the audio.
19476
e1f98e402a7e New files - @audiorecorder and @audioplayer classes
Vytautas Jančauskas <unaudio@gmail.com>
parents:
diff changeset
38
e1f98e402a7e New files - @audiorecorder and @audioplayer classes
Vytautas Jančauskas <unaudio@gmail.com>
parents:
diff changeset
39 function recorder = audiorecorder (varargin)
19523
5802ea7037d4 style fixes for audioplayer and audiorecorder classes
John W. Eaton <jwe@octave.org>
parents: 19509
diff changeset
40
19476
e1f98e402a7e New files - @audiorecorder and @audioplayer classes
Vytautas Jančauskas <unaudio@gmail.com>
parents:
diff changeset
41 if (nargin > 5)
e1f98e402a7e New files - @audiorecorder and @audioplayer classes
Vytautas Jančauskas <unaudio@gmail.com>
parents:
diff changeset
42 print_usage ();
e1f98e402a7e New files - @audiorecorder and @audioplayer classes
Vytautas Jančauskas <unaudio@gmail.com>
parents:
diff changeset
43 endif
19523
5802ea7037d4 style fixes for audioplayer and audiorecorder classes
John W. Eaton <jwe@octave.org>
parents: 19509
diff changeset
44
19507
dac3191a5301 style fixes for audioplayer and audiorecorder .m files
John W. Eaton <jwe@octave.org>
parents: 19506
diff changeset
45 if (nargin > 0 && ischar (varargin{1}))
19503
36a26a131209 Apply Octave coding style to audio project additions
Mike Miller <mtmiller@ieee.org>
parents: 19500
diff changeset
46 varargin{1} = str2func (varargin{1});
19476
e1f98e402a7e New files - @audiorecorder and @audioplayer classes
Vytautas Jančauskas <unaudio@gmail.com>
parents:
diff changeset
47 endif
19523
5802ea7037d4 style fixes for audioplayer and audiorecorder classes
John W. Eaton <jwe@octave.org>
parents: 19509
diff changeset
48
19476
e1f98e402a7e New files - @audiorecorder and @audioplayer classes
Vytautas Jančauskas <unaudio@gmail.com>
parents:
diff changeset
49 recorder.recorder = __recorder_audiorecorder__ (varargin{:});
e1f98e402a7e New files - @audiorecorder and @audioplayer classes
Vytautas Jančauskas <unaudio@gmail.com>
parents:
diff changeset
50 recorder = class (recorder, "audiorecorder");
19523
5802ea7037d4 style fixes for audioplayer and audiorecorder classes
John W. Eaton <jwe@octave.org>
parents: 19509
diff changeset
51
19476
e1f98e402a7e New files - @audiorecorder and @audioplayer classes
Vytautas Jančauskas <unaudio@gmail.com>
parents:
diff changeset
52 endfunction
e1f98e402a7e New files - @audiorecorder and @audioplayer classes
Vytautas Jančauskas <unaudio@gmail.com>
parents:
diff changeset
53
19668
b2fe4dbe5266 Don't play or record audio during 'make check'
Mike Miller <mtmiller@ieee.org>
parents: 19575
diff changeset
54 %!demo
19509
2a16dac2176f fix audioplayer tests
John W. Eaton <jwe@octave.org>
parents: 19507
diff changeset
55 %! recorder = audiorecorder (44100, 16, 2);
2a16dac2176f fix audioplayer tests
John W. Eaton <jwe@octave.org>
parents: 19507
diff changeset
56 %! record (recorder, 1);
2a16dac2176f fix audioplayer tests
John W. Eaton <jwe@octave.org>
parents: 19507
diff changeset
57 %! sleep (2);
2a16dac2176f fix audioplayer tests
John W. Eaton <jwe@octave.org>
parents: 19507
diff changeset
58 %! player1 = audioplayer (recorder);
2a16dac2176f fix audioplayer tests
John W. Eaton <jwe@octave.org>
parents: 19507
diff changeset
59 %! player2 = getplayer (recorder);
2a16dac2176f fix audioplayer tests
John W. Eaton <jwe@octave.org>
parents: 19507
diff changeset
60 %! play (player1);
2a16dac2176f fix audioplayer tests
John W. Eaton <jwe@octave.org>
parents: 19507
diff changeset
61 %! sleep (2);
2a16dac2176f fix audioplayer tests
John W. Eaton <jwe@octave.org>
parents: 19507
diff changeset
62 %! play (player2);
2a16dac2176f fix audioplayer tests
John W. Eaton <jwe@octave.org>
parents: 19507
diff changeset
63 %! sleep (2);
19668
b2fe4dbe5266 Don't play or record audio during 'make check'
Mike Miller <mtmiller@ieee.org>
parents: 19575
diff changeset
64 %! stop (player1);
b2fe4dbe5266 Don't play or record audio during 'make check'
Mike Miller <mtmiller@ieee.org>
parents: 19575
diff changeset
65 %! stop (player2);
b2fe4dbe5266 Don't play or record audio during 'make check'
Mike Miller <mtmiller@ieee.org>
parents: 19575
diff changeset
66
b2fe4dbe5266 Don't play or record audio during 'make check'
Mike Miller <mtmiller@ieee.org>
parents: 19575
diff changeset
67 ## Tests of audiorecorder must not actually record anything.
b2fe4dbe5266 Don't play or record audio during 'make check'
Mike Miller <mtmiller@ieee.org>
parents: 19575
diff changeset
68
b2fe4dbe5266 Don't play or record audio during 'make check'
Mike Miller <mtmiller@ieee.org>
parents: 19575
diff changeset
69 %!testif HAVE_PORTAUDIO
b2fe4dbe5266 Don't play or record audio during 'make check'
Mike Miller <mtmiller@ieee.org>
parents: 19575
diff changeset
70 %! recorder = audiorecorder (44100, 16, 2);
b2fe4dbe5266 Don't play or record audio during 'make check'
Mike Miller <mtmiller@ieee.org>
parents: 19575
diff changeset
71 %! data = getaudiodata (recorder, "int16");
b2fe4dbe5266 Don't play or record audio during 'make check'
Mike Miller <mtmiller@ieee.org>
parents: 19575
diff changeset
72 %! assert (strcmp (class (data), "int16"));
b2fe4dbe5266 Don't play or record audio during 'make check'
Mike Miller <mtmiller@ieee.org>
parents: 19575
diff changeset
73 %! data = getaudiodata (recorder, "int8");
b2fe4dbe5266 Don't play or record audio during 'make check'
Mike Miller <mtmiller@ieee.org>
parents: 19575
diff changeset
74 %! assert (strcmp (class (data), "int8"));
b2fe4dbe5266 Don't play or record audio during 'make check'
Mike Miller <mtmiller@ieee.org>
parents: 19575
diff changeset
75 %! data = getaudiodata (recorder, "uint8");
b2fe4dbe5266 Don't play or record audio during 'make check'
Mike Miller <mtmiller@ieee.org>
parents: 19575
diff changeset
76 %! assert (strcmp (class (data), "uint8"));
b2fe4dbe5266 Don't play or record audio during 'make check'
Mike Miller <mtmiller@ieee.org>
parents: 19575
diff changeset
77 %! assert (size (data)(1), recorder.TotalSamples);
b2fe4dbe5266 Don't play or record audio during 'make check'
Mike Miller <mtmiller@ieee.org>
parents: 19575
diff changeset
78 %! assert (size (data)(2), 2);
19476
e1f98e402a7e New files - @audiorecorder and @audioplayer classes
Vytautas Jančauskas <unaudio@gmail.com>
parents:
diff changeset
79
19528
4f615cd9a68d Use "testif HAVE_PORTAUDIO" for new audio function tests
Andreas Weber <andy.weber.aw@gmail.com>
parents: 19523
diff changeset
80 %!testif HAVE_PORTAUDIO
19509
2a16dac2176f fix audioplayer tests
John W. Eaton <jwe@octave.org>
parents: 19507
diff changeset
81 %! recorder = audiorecorder;
2a16dac2176f fix audioplayer tests
John W. Eaton <jwe@octave.org>
parents: 19507
diff changeset
82 %! set (recorder, {"SampleRate", "Tag", "UserData"}, {8000, "tag", [1, 2; 3, 4]});
2a16dac2176f fix audioplayer tests
John W. Eaton <jwe@octave.org>
parents: 19507
diff changeset
83 %! assert (recorder.SampleRate, 8000);
2a16dac2176f fix audioplayer tests
John W. Eaton <jwe@octave.org>
parents: 19507
diff changeset
84 %! assert (recorder.Tag, "tag");
2a16dac2176f fix audioplayer tests
John W. Eaton <jwe@octave.org>
parents: 19507
diff changeset
85 %! assert (recorder.UserData, [1, 2; 3, 4]);
19476
e1f98e402a7e New files - @audiorecorder and @audioplayer classes
Vytautas Jančauskas <unaudio@gmail.com>
parents:
diff changeset
86
19528
4f615cd9a68d Use "testif HAVE_PORTAUDIO" for new audio function tests
Andreas Weber <andy.weber.aw@gmail.com>
parents: 19523
diff changeset
87 %!testif HAVE_PORTAUDIO
19509
2a16dac2176f fix audioplayer tests
John W. Eaton <jwe@octave.org>
parents: 19507
diff changeset
88 %! recorder = audiorecorder;
2a16dac2176f fix audioplayer tests
John W. Eaton <jwe@octave.org>
parents: 19507
diff changeset
89 %! settable = set (recorder);
2a16dac2176f fix audioplayer tests
John W. Eaton <jwe@octave.org>
parents: 19507
diff changeset
90 %! settable.SampleRate = 8000;
2a16dac2176f fix audioplayer tests
John W. Eaton <jwe@octave.org>
parents: 19507
diff changeset
91 %! settable.Tag = "tag";
2a16dac2176f fix audioplayer tests
John W. Eaton <jwe@octave.org>
parents: 19507
diff changeset
92 %! settable.UserData = [1, 2; 3, 4];
2a16dac2176f fix audioplayer tests
John W. Eaton <jwe@octave.org>
parents: 19507
diff changeset
93 %! set (recorder, settable);
2a16dac2176f fix audioplayer tests
John W. Eaton <jwe@octave.org>
parents: 19507
diff changeset
94 %! assert (recorder.SampleRate, 8000);
2a16dac2176f fix audioplayer tests
John W. Eaton <jwe@octave.org>
parents: 19507
diff changeset
95 %! assert (recorder.Tag, "tag");
2a16dac2176f fix audioplayer tests
John W. Eaton <jwe@octave.org>
parents: 19507
diff changeset
96 %! assert (recorder.UserData, [1, 2; 3, 4]);
19476
e1f98e402a7e New files - @audiorecorder and @audioplayer classes
Vytautas Jančauskas <unaudio@gmail.com>
parents:
diff changeset
97
19528
4f615cd9a68d Use "testif HAVE_PORTAUDIO" for new audio function tests
Andreas Weber <andy.weber.aw@gmail.com>
parents: 19523
diff changeset
98 %!testif HAVE_PORTAUDIO
19509
2a16dac2176f fix audioplayer tests
John W. Eaton <jwe@octave.org>
parents: 19507
diff changeset
99 %! recorder = audiorecorder;
2a16dac2176f fix audioplayer tests
John W. Eaton <jwe@octave.org>
parents: 19507
diff changeset
100 %! recorder.SampleRate = 8000;
2a16dac2176f fix audioplayer tests
John W. Eaton <jwe@octave.org>
parents: 19507
diff changeset
101 %! recorder.Tag = "tag";
2a16dac2176f fix audioplayer tests
John W. Eaton <jwe@octave.org>
parents: 19507
diff changeset
102 %! recorder.UserData = [1, 2; 3, 4];
2a16dac2176f fix audioplayer tests
John W. Eaton <jwe@octave.org>
parents: 19507
diff changeset
103 %! properties = get (recorder, {"SampleRate", "Tag", "UserData"});
2a16dac2176f fix audioplayer tests
John W. Eaton <jwe@octave.org>
parents: 19507
diff changeset
104 %! assert (properties, {8000, "tag", [1, 2; 3, 4]});
19476
e1f98e402a7e New files - @audiorecorder and @audioplayer classes
Vytautas Jančauskas <unaudio@gmail.com>
parents:
diff changeset
105
e1f98e402a7e New files - @audiorecorder and @audioplayer classes
Vytautas Jančauskas <unaudio@gmail.com>
parents:
diff changeset
106 #%!function status = callback_record (sound)
19503
36a26a131209 Apply Octave coding style to audio project additions
Mike Miller <mtmiller@ieee.org>
parents: 19500
diff changeset
107 #%! fid = fopen ("record.txt", "at");
19476
e1f98e402a7e New files - @audiorecorder and @audioplayer classes
Vytautas Jančauskas <unaudio@gmail.com>
parents:
diff changeset
108 #%! for index = 1:rows(sound)
19503
36a26a131209 Apply Octave coding style to audio project additions
Mike Miller <mtmiller@ieee.org>
parents: 19500
diff changeset
109 #%! fprintf (fid, "%.4f, %.4f\n", sound(index, 1), sound(index, 2));
19476
e1f98e402a7e New files - @audiorecorder and @audioplayer classes
Vytautas Jančauskas <unaudio@gmail.com>
parents:
diff changeset
110 #%! endfor
19503
36a26a131209 Apply Octave coding style to audio project additions
Mike Miller <mtmiller@ieee.org>
parents: 19500
diff changeset
111 #%! fclose (fid);
19476
e1f98e402a7e New files - @audiorecorder and @audioplayer classes
Vytautas Jančauskas <unaudio@gmail.com>
parents:
diff changeset
112 #%! status = 0;
e1f98e402a7e New files - @audiorecorder and @audioplayer classes
Vytautas Jančauskas <unaudio@gmail.com>
parents:
diff changeset
113 #%!endfunction
e1f98e402a7e New files - @audiorecorder and @audioplayer classes
Vytautas Jančauskas <unaudio@gmail.com>
parents:
diff changeset
114
19528
4f615cd9a68d Use "testif HAVE_PORTAUDIO" for new audio function tests
Andreas Weber <andy.weber.aw@gmail.com>
parents: 19523
diff changeset
115 #%!testif HAVE_PORTAUDIO
19509
2a16dac2176f fix audioplayer tests
John W. Eaton <jwe@octave.org>
parents: 19507
diff changeset
116 #%! recorder = audiorecorder (@callback_record, 44100);
2a16dac2176f fix audioplayer tests
John W. Eaton <jwe@octave.org>
parents: 19507
diff changeset
117 #%! unlink ("record.txt")
2a16dac2176f fix audioplayer tests
John W. Eaton <jwe@octave.org>
parents: 19507
diff changeset
118 #%! record (recorder);
2a16dac2176f fix audioplayer tests
John W. Eaton <jwe@octave.org>
parents: 19507
diff changeset
119 #%! sleep (2);
19575
7f5006dfa2eb * audiorecorder.m: Fix test.
John W. Eaton <jwe@octave.org>
parents: 19571
diff changeset
120 #%! stop (recorder);
19509
2a16dac2176f fix audioplayer tests
John W. Eaton <jwe@octave.org>
parents: 19507
diff changeset
121 #%! s = stat ("record.txt");
2a16dac2176f fix audioplayer tests
John W. Eaton <jwe@octave.org>
parents: 19507
diff changeset
122 #%! assert (s.size > 0);
19476
e1f98e402a7e New files - @audiorecorder and @audioplayer classes
Vytautas Jančauskas <unaudio@gmail.com>
parents:
diff changeset
123
19528
4f615cd9a68d Use "testif HAVE_PORTAUDIO" for new audio function tests
Andreas Weber <andy.weber.aw@gmail.com>
parents: 19523
diff changeset
124 #%!testif HAVE_PORTAUDIO
19509
2a16dac2176f fix audioplayer tests
John W. Eaton <jwe@octave.org>
parents: 19507
diff changeset
125 #%! recorder = audiorecorder (@callback_record, 44100);
2a16dac2176f fix audioplayer tests
John W. Eaton <jwe@octave.org>
parents: 19507
diff changeset
126 #%! unlink ("record.txt")
2a16dac2176f fix audioplayer tests
John W. Eaton <jwe@octave.org>
parents: 19507
diff changeset
127 #%! record (recorder);
2a16dac2176f fix audioplayer tests
John W. Eaton <jwe@octave.org>
parents: 19507
diff changeset
128 #%! sleep (2);
2a16dac2176f fix audioplayer tests
John W. Eaton <jwe@octave.org>
parents: 19507
diff changeset
129 #%! stop (recorder);
2a16dac2176f fix audioplayer tests
John W. Eaton <jwe@octave.org>
parents: 19507
diff changeset
130 #%! s = stat ("record.txt");
2a16dac2176f fix audioplayer tests
John W. Eaton <jwe@octave.org>
parents: 19507
diff changeset
131 #%! assert (s.size > 0);