Mercurial > octave-nkf
annotate scripts/audio/@audiorecorder/audiorecorder.m @ 19609:7f5006dfa2eb
* audiorecorder.m: Fix test.
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Tue, 13 Jan 2015 23:06:08 -0500 |
parents | 72304a4e010a |
children | b2fe4dbe5266 |
rev | line source |
---|---|
19534
8bb399569393
Add Octave copyright header block to audio project files
Mike Miller <mtmiller@ieee.org>
parents:
19531
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:
19531
diff
changeset
|
2 ## |
8bb399569393
Add Octave copyright header block to audio project files
Mike Miller <mtmiller@ieee.org>
parents:
19531
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:
19531
diff
changeset
|
4 ## |
8bb399569393
Add Octave copyright header block to audio project files
Mike Miller <mtmiller@ieee.org>
parents:
19531
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:
19531
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:
19531
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:
19531
diff
changeset
|
8 ## your option) any later version. |
8bb399569393
Add Octave copyright header block to audio project files
Mike Miller <mtmiller@ieee.org>
parents:
19531
diff
changeset
|
9 ## |
8bb399569393
Add Octave copyright header block to audio project files
Mike Miller <mtmiller@ieee.org>
parents:
19531
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:
19531
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:
19531
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:
19531
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:
19531
diff
changeset
|
14 ## |
8bb399569393
Add Octave copyright header block to audio project files
Mike Miller <mtmiller@ieee.org>
parents:
19531
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:
19531
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:
19531
diff
changeset
|
17 ## <http://www.gnu.org/licenses/>. |
8bb399569393
Add Octave copyright header block to audio project files
Mike Miller <mtmiller@ieee.org>
parents:
19531
diff
changeset
|
18 |
19510
e1f98e402a7e
New files - @audiorecorder and @audioplayer classes
Vytautas Jančauskas <unaudio@gmail.com>
parents:
diff
changeset
|
19 ## -*- texinfo -*- |
19605
72304a4e010a
Use Octave coding conventions for documentation of audio functions.
Rik <rik@octave.org>
parents:
19594
diff
changeset
|
20 ## @deftypefn {Function File} {@var{recorder} =} audiorecorder () |
19540
fdb8a62ef17a
fix docstring style in .m files
John W. Eaton <jwe@octave.org>
parents:
19539
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:
19539
diff
changeset
|
22 ## @deftypefnx {Function File} {@var{recorder} =} audiorecorder (@var{fs}, @var{nbits}, @var{channels}, @var{id}) |
19539
ce02743b6f2a
Fix texinfo docstring syntax for all audio functions
Mike Miller <mtmiller@ieee.org>
parents:
19537
diff
changeset
|
23 ## Create an audiorecorder object recording 8 bit mono audio at 8000 Hz |
19540
fdb8a62ef17a
fix docstring style in .m files
John W. Eaton <jwe@octave.org>
parents:
19539
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:
19539
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:
19539
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:
19539
diff
changeset
|
27 ## may be found using the audiodevinfo function. |
19594
e75df9e43e63
disable Octave audio callback functions
John W. Eaton <jwe@octave.org>
parents:
19562
diff
changeset
|
28 ## @end deftypefn |
e75df9e43e63
disable Octave audio callback functions
John W. Eaton <jwe@octave.org>
parents:
19562
diff
changeset
|
29 |
e75df9e43e63
disable Octave audio callback functions
John W. Eaton <jwe@octave.org>
parents:
19562
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:
19562
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:
19562
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:
19562
diff
changeset
|
33 ## all of Octave's global data... |
e75df9e43e63
disable Octave audio callback functions
John W. Eaton <jwe@octave.org>
parents:
19562
diff
changeset
|
34 ## |
e75df9e43e63
disable Octave audio callback functions
John W. Eaton <jwe@octave.org>
parents:
19562
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:
19562
diff
changeset
|
36 ## |
19540
fdb8a62ef17a
fix docstring style in .m files
John W. Eaton <jwe@octave.org>
parents:
19539
diff
changeset
|
37 ## Given a function handle, use that function to process the audio. |
19510
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) |
19557
5802ea7037d4
style fixes for audioplayer and audiorecorder classes
John W. Eaton <jwe@octave.org>
parents:
19543
diff
changeset
|
40 |
19510
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 |
19557
5802ea7037d4
style fixes for audioplayer and audiorecorder classes
John W. Eaton <jwe@octave.org>
parents:
19543
diff
changeset
|
44 |
19541
dac3191a5301
style fixes for audioplayer and audiorecorder .m files
John W. Eaton <jwe@octave.org>
parents:
19540
diff
changeset
|
45 if (nargin > 0 && ischar (varargin{1})) |
19537
36a26a131209
Apply Octave coding style to audio project additions
Mike Miller <mtmiller@ieee.org>
parents:
19534
diff
changeset
|
46 varargin{1} = str2func (varargin{1}); |
19510
e1f98e402a7e
New files - @audiorecorder and @audioplayer classes
Vytautas Jančauskas <unaudio@gmail.com>
parents:
diff
changeset
|
47 endif |
19557
5802ea7037d4
style fixes for audioplayer and audiorecorder classes
John W. Eaton <jwe@octave.org>
parents:
19543
diff
changeset
|
48 |
19510
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"); |
19557
5802ea7037d4
style fixes for audioplayer and audiorecorder classes
John W. Eaton <jwe@octave.org>
parents:
19543
diff
changeset
|
51 |
19510
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 |
19562
4f615cd9a68d
Use "testif HAVE_PORTAUDIO" for new audio function tests
Andreas Weber <andy.weber.aw@gmail.com>
parents:
19557
diff
changeset
|
54 %!testif HAVE_PORTAUDIO |
19543 | 55 %! recorder = audiorecorder (44100, 16, 2); |
56 %! recordblocking (recorder, 1); | |
57 %! data = getaudiodata (recorder, "int16"); | |
58 %! assert (strcmp (class (data), "int16")); | |
59 %! data = getaudiodata (recorder, "int8"); | |
60 %! assert (strcmp (class (data), "int8")); | |
61 %! data = getaudiodata (recorder, "uint8"); | |
62 %! assert (strcmp (class (data), "uint8")); | |
63 %! assert (size (data)(1), recorder.TotalSamples); | |
64 %! assert (size (data)(2), 2); | |
65 %! assert (size (data)(1) != 0); | |
19510
e1f98e402a7e
New files - @audiorecorder and @audioplayer classes
Vytautas Jančauskas <unaudio@gmail.com>
parents:
diff
changeset
|
66 |
19562
4f615cd9a68d
Use "testif HAVE_PORTAUDIO" for new audio function tests
Andreas Weber <andy.weber.aw@gmail.com>
parents:
19557
diff
changeset
|
67 %!testif HAVE_PORTAUDIO |
19543 | 68 %! recorder = audiorecorder (44100, 16, 2); |
69 %! record (recorder, 1) | |
70 %! sleep (2); | |
71 %! record (recorder, 1); | |
72 %! sleep (2); | |
73 %! data = getaudiodata (recorder); | |
74 %! assert (size (data)(1) < 44100 * 2); | |
19510
e1f98e402a7e
New files - @audiorecorder and @audioplayer classes
Vytautas Jančauskas <unaudio@gmail.com>
parents:
diff
changeset
|
75 |
19562
4f615cd9a68d
Use "testif HAVE_PORTAUDIO" for new audio function tests
Andreas Weber <andy.weber.aw@gmail.com>
parents:
19557
diff
changeset
|
76 %!testif HAVE_PORTAUDIO |
19543 | 77 %! recorder = audiorecorder (44100, 16, 2); |
78 %! record (recorder, 1); | |
79 %! sleep (2); | |
80 %! player1 = audioplayer (recorder); | |
81 %! player2 = getplayer (recorder); | |
82 %! play (player1); | |
83 %! sleep (2); | |
84 %! play (player2); | |
85 %! sleep (2); | |
86 %! assert (player1.TotalSamples, recorder.TotalSamples); | |
87 %! assert (player2.TotalSamples, recorder.TotalSamples); | |
19510
e1f98e402a7e
New files - @audiorecorder and @audioplayer classes
Vytautas Jančauskas <unaudio@gmail.com>
parents:
diff
changeset
|
88 |
19562
4f615cd9a68d
Use "testif HAVE_PORTAUDIO" for new audio function tests
Andreas Weber <andy.weber.aw@gmail.com>
parents:
19557
diff
changeset
|
89 %!testif HAVE_PORTAUDIO |
19543 | 90 %! recorder = audiorecorder; |
91 %! set (recorder, {"SampleRate", "Tag", "UserData"}, {8000, "tag", [1, 2; 3, 4]}); | |
92 %! assert (recorder.SampleRate, 8000); | |
93 %! assert (recorder.Tag, "tag"); | |
94 %! assert (recorder.UserData, [1, 2; 3, 4]); | |
19510
e1f98e402a7e
New files - @audiorecorder and @audioplayer classes
Vytautas Jančauskas <unaudio@gmail.com>
parents:
diff
changeset
|
95 |
19562
4f615cd9a68d
Use "testif HAVE_PORTAUDIO" for new audio function tests
Andreas Weber <andy.weber.aw@gmail.com>
parents:
19557
diff
changeset
|
96 %!testif HAVE_PORTAUDIO |
19543 | 97 %! recorder = audiorecorder; |
98 %! settable = set (recorder); | |
99 %! settable.SampleRate = 8000; | |
100 %! settable.Tag = "tag"; | |
101 %! settable.UserData = [1, 2; 3, 4]; | |
102 %! set (recorder, settable); | |
103 %! assert (recorder.SampleRate, 8000); | |
104 %! assert (recorder.Tag, "tag"); | |
105 %! assert (recorder.UserData, [1, 2; 3, 4]); | |
19510
e1f98e402a7e
New files - @audiorecorder and @audioplayer classes
Vytautas Jančauskas <unaudio@gmail.com>
parents:
diff
changeset
|
106 |
19562
4f615cd9a68d
Use "testif HAVE_PORTAUDIO" for new audio function tests
Andreas Weber <andy.weber.aw@gmail.com>
parents:
19557
diff
changeset
|
107 %!testif HAVE_PORTAUDIO |
19543 | 108 %! recorder = audiorecorder; |
109 %! recorder.SampleRate = 8000; | |
110 %! recorder.Tag = "tag"; | |
111 %! recorder.UserData = [1, 2; 3, 4]; | |
112 %! properties = get (recorder, {"SampleRate", "Tag", "UserData"}); | |
113 %! assert (properties, {8000, "tag", [1, 2; 3, 4]}); | |
19510
e1f98e402a7e
New files - @audiorecorder and @audioplayer classes
Vytautas Jančauskas <unaudio@gmail.com>
parents:
diff
changeset
|
114 |
e1f98e402a7e
New files - @audiorecorder and @audioplayer classes
Vytautas Jančauskas <unaudio@gmail.com>
parents:
diff
changeset
|
115 #%!function status = callback_record (sound) |
19537
36a26a131209
Apply Octave coding style to audio project additions
Mike Miller <mtmiller@ieee.org>
parents:
19534
diff
changeset
|
116 #%! fid = fopen ("record.txt", "at"); |
19510
e1f98e402a7e
New files - @audiorecorder and @audioplayer classes
Vytautas Jančauskas <unaudio@gmail.com>
parents:
diff
changeset
|
117 #%! for index = 1:rows(sound) |
19537
36a26a131209
Apply Octave coding style to audio project additions
Mike Miller <mtmiller@ieee.org>
parents:
19534
diff
changeset
|
118 #%! fprintf (fid, "%.4f, %.4f\n", sound(index, 1), sound(index, 2)); |
19510
e1f98e402a7e
New files - @audiorecorder and @audioplayer classes
Vytautas Jančauskas <unaudio@gmail.com>
parents:
diff
changeset
|
119 #%! endfor |
19537
36a26a131209
Apply Octave coding style to audio project additions
Mike Miller <mtmiller@ieee.org>
parents:
19534
diff
changeset
|
120 #%! fclose (fid); |
19510
e1f98e402a7e
New files - @audiorecorder and @audioplayer classes
Vytautas Jančauskas <unaudio@gmail.com>
parents:
diff
changeset
|
121 #%! status = 0; |
e1f98e402a7e
New files - @audiorecorder and @audioplayer classes
Vytautas Jančauskas <unaudio@gmail.com>
parents:
diff
changeset
|
122 #%!endfunction |
e1f98e402a7e
New files - @audiorecorder and @audioplayer classes
Vytautas Jančauskas <unaudio@gmail.com>
parents:
diff
changeset
|
123 |
19562
4f615cd9a68d
Use "testif HAVE_PORTAUDIO" for new audio function tests
Andreas Weber <andy.weber.aw@gmail.com>
parents:
19557
diff
changeset
|
124 #%!testif HAVE_PORTAUDIO |
19543 | 125 #%! recorder = audiorecorder (@callback_record, 44100); |
126 #%! unlink ("record.txt") | |
127 #%! record (recorder); | |
128 #%! sleep (2); | |
19609
7f5006dfa2eb
* audiorecorder.m: Fix test.
John W. Eaton <jwe@octave.org>
parents:
19605
diff
changeset
|
129 #%! stop (recorder); |
19543 | 130 #%! s = stat ("record.txt"); |
131 #%! assert (s.size > 0); | |
19510
e1f98e402a7e
New files - @audiorecorder and @audioplayer classes
Vytautas Jančauskas <unaudio@gmail.com>
parents:
diff
changeset
|
132 |
19562
4f615cd9a68d
Use "testif HAVE_PORTAUDIO" for new audio function tests
Andreas Weber <andy.weber.aw@gmail.com>
parents:
19557
diff
changeset
|
133 #%!testif HAVE_PORTAUDIO |
19543 | 134 #%! recorder = audiorecorder (@callback_record, 44100); |
135 #%! unlink ("record.txt") | |
136 #%! record (recorder); | |
137 #%! sleep (2); | |
138 #%! stop (recorder); | |
139 #%! s = stat ("record.txt"); | |
140 #%! assert (s.size > 0); |