annotate scripts/audio/@audioplayer/audioplayer.m @ 30572:6d96538052b9

Overhaul @audioplayer class. Eliminate unnecessary input validation that one argument is supplied to class methods as interpreter guarantees the first argument is an @audioplayer object. Accept case-insensitive property names for get()/set() functions. Add BIST tests on a per function basis rather than only in @audioplayer constructor. * @audioplayer/__get_properties__.m: Eliminate nargin checking. Use intermediate variable hplayer to clarify code. Use ifelse() to simplify 5-line if/else/endif tree. * @audioplayer/audioplayer.m: Add input validation to prevent use of callback functions (not currently supported). Add FIXME note and comment out rudimentary support for callback functions. Remove tests of @audioplayer functionality to the methods files. Add input validation BIST tests for callback function validation. * @audioplayer/disp.m: Eliminate nargin checking. Mark file as tested for BIST. * @audioplayer/get.m: Rename "retval" to "value" in function prototype. Use input parameters with names matching documentation rather than varargin. Use new function getproperty() to do actual property retrieval rather than getfield(). Add BIST tests. * @audioplayer/get.m (getproperty): New function. Function checks property names without regard to case sensitivity and also issues a meaningful error message if the property name does not exist. * @audioplayer/isplaying.m: Eliminate nargin checking. Add BIST tests. * @audioplayer/pause.m: Eliminate nargin checking. Mark file as tested for BIST. * @audioplayer/play.m: Eliminate nargin checking. Use input parameters with names matching documentation rather than varargin. Use intermediate variable hplayer to clarify code. Mark file as tested for BIST. * @audioplayer/playblocking.m: Use input parameters with names matching documentation rather than varargin. Mark file as tested for BIST. * @audioplayer/resume.m: Eliminate nargin checking. Mark file as tested for BIST. * @audioplayer/set.m: Eliminate nargin checking of first argument. Use input parameter "player" for first argument rather than varargin. Use intermediate variable hplayer to clarify code. Add BIST tests. * @audioplayer/set.m (setproperty): Use lower() to implement case insensitive matching of property names. Rewrite error() message to be clearer and report the incorrect name. * @audioplayer/stop.m: Eliminate nargin checking. Mark file as tested for BIST. * @audioplayer/subsasgn.m: Change output variable name to "player" for clarity. Add BIST tests. * @audioplayer/subsref.m: Add BIST tests.
author Rik <rik@octave.org>
date Thu, 30 Dec 2021 16:11:55 -0800
parents 82b685157e2b
children 597f3ee61a48
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
27923
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
1 ########################################################################
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
2 ##
30564
796f54d4ddbf update Octave Project Developers copyright for the new year
John W. Eaton <jwe@octave.org>
parents: 29358
diff changeset
3 ## Copyright (C) 2013-2022 The Octave Project Developers
27918
b442ec6dda5c use centralized file for copyright info for individual contributors
John W. Eaton <jwe@octave.org>
parents: 26376
diff changeset
4 ##
27923
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
5 ## See the file COPYRIGHT.md in the top-level directory of this
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
6 ## distribution or <https://octave.org/copyright/>.
19500
8bb399569393 Add Octave copyright header block to audio project files
Mike Miller <mtmiller@ieee.org>
parents: 19496
diff changeset
7 ##
8bb399569393 Add Octave copyright header block to audio project files
Mike Miller <mtmiller@ieee.org>
parents: 19496
diff changeset
8 ## This file is part of Octave.
8bb399569393 Add Octave copyright header block to audio project files
Mike Miller <mtmiller@ieee.org>
parents: 19496
diff changeset
9 ##
24534
194eb4bd202b maint: Update punctuation for GPL v3 license text.
Rik <rik@octave.org>
parents: 23361
diff changeset
10 ## Octave is free software: you can redistribute it and/or modify it
19500
8bb399569393 Add Octave copyright header block to audio project files
Mike Miller <mtmiller@ieee.org>
parents: 19496
diff changeset
11 ## under the terms of the GNU General Public License as published by
24534
194eb4bd202b maint: Update punctuation for GPL v3 license text.
Rik <rik@octave.org>
parents: 23361
diff changeset
12 ## the Free Software Foundation, either version 3 of the License, or
22755
3a2b891d0b33 maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents: 22544
diff changeset
13 ## (at your option) any later version.
19500
8bb399569393 Add Octave copyright header block to audio project files
Mike Miller <mtmiller@ieee.org>
parents: 19496
diff changeset
14 ##
8bb399569393 Add Octave copyright header block to audio project files
Mike Miller <mtmiller@ieee.org>
parents: 19496
diff changeset
15 ## 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: 19496
diff changeset
16 ## WITHOUT ANY WARRANTY; without even the implied warranty of
22755
3a2b891d0b33 maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents: 22544
diff changeset
17 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
3a2b891d0b33 maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents: 22544
diff changeset
18 ## GNU General Public License for more details.
19500
8bb399569393 Add Octave copyright header block to audio project files
Mike Miller <mtmiller@ieee.org>
parents: 19496
diff changeset
19 ##
8bb399569393 Add Octave copyright header block to audio project files
Mike Miller <mtmiller@ieee.org>
parents: 19496
diff changeset
20 ## 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: 19496
diff changeset
21 ## along with Octave; see the file COPYING. If not, see
24534
194eb4bd202b maint: Update punctuation for GPL v3 license text.
Rik <rik@octave.org>
parents: 23361
diff changeset
22 ## <https://www.gnu.org/licenses/>.
27923
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
23 ##
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
24 ########################################################################
19500
8bb399569393 Add Octave copyright header block to audio project files
Mike Miller <mtmiller@ieee.org>
parents: 19496
diff changeset
25
19476
e1f98e402a7e New files - @audiorecorder and @audioplayer classes
Vytautas Jančauskas <unaudio@gmail.com>
parents:
diff changeset
26 ## -*- texinfo -*-
20852
516bb87ea72e 2015 Code Sprint: remove class of function from docstring for all m-files.
Rik <rik@octave.org>
parents: 20158
diff changeset
27 ## @deftypefn {} {@var{player} =} audioplayer (@var{y}, @var{fs})
516bb87ea72e 2015 Code Sprint: remove class of function from docstring for all m-files.
Rik <rik@octave.org>
parents: 20158
diff changeset
28 ## @deftypefnx {} {@var{player} =} audioplayer (@var{y}, @var{fs}, @var{nbits})
516bb87ea72e 2015 Code Sprint: remove class of function from docstring for all m-files.
Rik <rik@octave.org>
parents: 20158
diff changeset
29 ## @deftypefnx {} {@var{player} =} audioplayer (@var{y}, @var{fs}, @var{nbits}, @var{id})
516bb87ea72e 2015 Code Sprint: remove class of function from docstring for all m-files.
Rik <rik@octave.org>
parents: 20158
diff changeset
30 ## @deftypefnx {} {@var{player} =} audioplayer (@var{recorder})
516bb87ea72e 2015 Code Sprint: remove class of function from docstring for all m-files.
Rik <rik@octave.org>
parents: 20158
diff changeset
31 ## @deftypefnx {} {@var{player} =} audioplayer (@var{recorder}, @var{id})
19505
ce02743b6f2a Fix texinfo docstring syntax for all audio functions
Mike Miller <mtmiller@ieee.org>
parents: 19503
diff changeset
32 ## Create an audioplayer object that will play back data @var{y} at sample
20158
7503499a252b doc: Update docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents: 19833
diff changeset
33 ## rate @var{fs}.
7503499a252b doc: Update docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents: 19833
diff changeset
34 ##
30568
82b685157e2b doc: Update documentation in audio/ directory.
Rik <rik@octave.org>
parents: 30564
diff changeset
35 ## The signal @var{y} can be a vector (mono audio) or a two-dimensional array
82b685157e2b doc: Update documentation in audio/ directory.
Rik <rik@octave.org>
parents: 30564
diff changeset
36 ## (multi-channel audio).
82b685157e2b doc: Update documentation in audio/ directory.
Rik <rik@octave.org>
parents: 30564
diff changeset
37 ##
82b685157e2b doc: Update documentation in audio/ directory.
Rik <rik@octave.org>
parents: 30564
diff changeset
38 ## The optional arguments @var{nbits} and @var{id} specify the number of bits
82b685157e2b doc: Update documentation in audio/ directory.
Rik <rik@octave.org>
parents: 30564
diff changeset
39 ## per sample and player device ID, respectively. Device IDs may be found
82b685157e2b doc: Update documentation in audio/ directory.
Rik <rik@octave.org>
parents: 30564
diff changeset
40 ## using the @code{audiodevinfo} function.
82b685157e2b doc: Update documentation in audio/ directory.
Rik <rik@octave.org>
parents: 30564
diff changeset
41 ##
82b685157e2b doc: Update documentation in audio/ directory.
Rik <rik@octave.org>
parents: 30564
diff changeset
42 ## Given an audiorecorder object @var{recorder}, use the data from the object
82b685157e2b doc: Update documentation in audio/ directory.
Rik <rik@octave.org>
parents: 30564
diff changeset
43 ## to initialize the player.
82b685157e2b doc: Update documentation in audio/ directory.
Rik <rik@octave.org>
parents: 30564
diff changeset
44 ##
82b685157e2b doc: Update documentation in audio/ directory.
Rik <rik@octave.org>
parents: 30564
diff changeset
45 ## The list of actions for an audioplayer object are shown below. All
82b685157e2b doc: Update documentation in audio/ directory.
Rik <rik@octave.org>
parents: 30564
diff changeset
46 ## methods require an audioplayer object as the first argument.
19476
e1f98e402a7e New files - @audiorecorder and @audioplayer classes
Vytautas Jančauskas <unaudio@gmail.com>
parents:
diff changeset
47 ##
30568
82b685157e2b doc: Update documentation in audio/ directory.
Rik <rik@octave.org>
parents: 30564
diff changeset
48 ## @multitable @columnfractions 0.2 0.75
82b685157e2b doc: Update documentation in audio/ directory.
Rik <rik@octave.org>
parents: 30564
diff changeset
49 ## @headitem Method @tab Description
82b685157e2b doc: Update documentation in audio/ directory.
Rik <rik@octave.org>
parents: 30564
diff changeset
50 ## @item get @tab Read audioplayer property values
82b685157e2b doc: Update documentation in audio/ directory.
Rik <rik@octave.org>
parents: 30564
diff changeset
51 ## @item isplaying @tab Return true if audioplayer is playing
82b685157e2b doc: Update documentation in audio/ directory.
Rik <rik@octave.org>
parents: 30564
diff changeset
52 ## @item pause @tab Pause audioplayer playback
82b685157e2b doc: Update documentation in audio/ directory.
Rik <rik@octave.org>
parents: 30564
diff changeset
53 ## @item play @tab Play audio stored in audioplayer object w/o blocking
82b685157e2b doc: Update documentation in audio/ directory.
Rik <rik@octave.org>
parents: 30564
diff changeset
54 ## @item playblocking @tab Play audio stored in audioplayer object
82b685157e2b doc: Update documentation in audio/ directory.
Rik <rik@octave.org>
parents: 30564
diff changeset
55 ## @item resume @tab Resume playback after pause
82b685157e2b doc: Update documentation in audio/ directory.
Rik <rik@octave.org>
parents: 30564
diff changeset
56 ## @item set @tab Write audioplayer property values
82b685157e2b doc: Update documentation in audio/ directory.
Rik <rik@octave.org>
parents: 30564
diff changeset
57 ## @item stop @tab Stop playback
82b685157e2b doc: Update documentation in audio/ directory.
Rik <rik@octave.org>
parents: 30564
diff changeset
58 ## @end multitable
19476
e1f98e402a7e New files - @audiorecorder and @audioplayer classes
Vytautas Jančauskas <unaudio@gmail.com>
parents:
diff changeset
59 ##
30568
82b685157e2b doc: Update documentation in audio/ directory.
Rik <rik@octave.org>
parents: 30564
diff changeset
60 ## Example
82b685157e2b doc: Update documentation in audio/ directory.
Rik <rik@octave.org>
parents: 30564
diff changeset
61 ##
82b685157e2b doc: Update documentation in audio/ directory.
Rik <rik@octave.org>
parents: 30564
diff changeset
62 ## Create an audioplayer object that will play back one second of white noise
82b685157e2b doc: Update documentation in audio/ directory.
Rik <rik@octave.org>
parents: 30564
diff changeset
63 ## at 44100 sample rate using 8 bits per sample.
19476
e1f98e402a7e New files - @audiorecorder and @audioplayer classes
Vytautas Jančauskas <unaudio@gmail.com>
parents:
diff changeset
64 ##
e1f98e402a7e New files - @audiorecorder and @audioplayer classes
Vytautas Jančauskas <unaudio@gmail.com>
parents:
diff changeset
65 ## @example
e1f98e402a7e New files - @audiorecorder and @audioplayer classes
Vytautas Jančauskas <unaudio@gmail.com>
parents:
diff changeset
66 ## @group
22441
bb8914d68f25 Fix noise signal generation in audioplayer doc string and demos (bug #49011)
Mike Miller <mtmiller@octave.org>
parents: 22323
diff changeset
67 ## y = 0.25 * randn (2, 44100);
19506
fdb8a62ef17a fix docstring style in .m files
John W. Eaton <jwe@octave.org>
parents: 19505
diff changeset
68 ## player = audioplayer (y, 44100, 8);
fdb8a62ef17a fix docstring style in .m files
John W. Eaton <jwe@octave.org>
parents: 19505
diff changeset
69 ## play (player);
19476
e1f98e402a7e New files - @audiorecorder and @audioplayer classes
Vytautas Jančauskas <unaudio@gmail.com>
parents:
diff changeset
70 ## @end group
e1f98e402a7e New files - @audiorecorder and @audioplayer classes
Vytautas Jančauskas <unaudio@gmail.com>
parents:
diff changeset
71 ## @end example
30568
82b685157e2b doc: Update documentation in audio/ directory.
Rik <rik@octave.org>
parents: 30564
diff changeset
72 ## @seealso{@audioplayer/get, @audioplayer/isplaying, @audioplayer/pause,
82b685157e2b doc: Update documentation in audio/ directory.
Rik <rik@octave.org>
parents: 30564
diff changeset
73 ## @audioplayer/play, @audioplayer/playblocking, @audioplayer/resume,
82b685157e2b doc: Update documentation in audio/ directory.
Rik <rik@octave.org>
parents: 30564
diff changeset
74 ## @audioplayer/set, @audioplayer/stop, audiodevinfo,
82b685157e2b doc: Update documentation in audio/ directory.
Rik <rik@octave.org>
parents: 30564
diff changeset
75 ## @audiorecorder/audiorecorder, sound, soundsc}
19571
72304a4e010a Use Octave coding conventions for documentation of audio functions.
Rik <rik@octave.org>
parents: 19560
diff changeset
76 ## @end deftypefn
19560
e75df9e43e63 disable Octave audio callback functions
John W. Eaton <jwe@octave.org>
parents: 19528
diff changeset
77
30568
82b685157e2b doc: Update documentation in audio/ directory.
Rik <rik@octave.org>
parents: 30564
diff changeset
78 ################################################################################
19560
e75df9e43e63 disable Octave audio callback functions
John W. Eaton <jwe@octave.org>
parents: 19528
diff changeset
79 ## 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
80 ## 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
81 ## 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
82 ## all of Octave's global data...
19476
e1f98e402a7e New files - @audiorecorder and @audioplayer classes
Vytautas Jančauskas <unaudio@gmail.com>
parents:
diff changeset
83 ##
20852
516bb87ea72e 2015 Code Sprint: remove class of function from docstring for all m-files.
Rik <rik@octave.org>
parents: 20158
diff changeset
84 ## @deftypefnx {} {@var{player} =} audioplayer (@var{function}, @dots{})
19560
e75df9e43e63 disable Octave audio callback functions
John W. Eaton <jwe@octave.org>
parents: 19528
diff changeset
85 ##
e75df9e43e63 disable Octave audio callback functions
John W. Eaton <jwe@octave.org>
parents: 19528
diff changeset
86 ## Given a function handle, use that function to process the audio.
e75df9e43e63 disable Octave audio callback functions
John W. Eaton <jwe@octave.org>
parents: 19528
diff changeset
87 #
19505
ce02743b6f2a Fix texinfo docstring syntax for all audio functions
Mike Miller <mtmiller@ieee.org>
parents: 19503
diff changeset
88 ## The following example will create and register a callback that generates
ce02743b6f2a Fix texinfo docstring syntax for all audio functions
Mike Miller <mtmiller@ieee.org>
parents: 19503
diff changeset
89 ## a sine wave on both channels.
19476
e1f98e402a7e New files - @audiorecorder and @audioplayer classes
Vytautas Jančauskas <unaudio@gmail.com>
parents:
diff changeset
90 ##
e1f98e402a7e New files - @audiorecorder and @audioplayer classes
Vytautas Jančauskas <unaudio@gmail.com>
parents:
diff changeset
91 ## @example
e1f98e402a7e New files - @audiorecorder and @audioplayer classes
Vytautas Jančauskas <unaudio@gmail.com>
parents:
diff changeset
92 ## @group
19506
fdb8a62ef17a fix docstring style in .m files
John W. Eaton <jwe@octave.org>
parents: 19505
diff changeset
93 ## function [ sound, status ] = callback_sine (frames)
fdb8a62ef17a fix docstring style in .m files
John W. Eaton <jwe@octave.org>
parents: 19505
diff changeset
94 ## global lphase = 0.0;
fdb8a62ef17a fix docstring style in .m files
John W. Eaton <jwe@octave.org>
parents: 19505
diff changeset
95 ## global rphase = 0.0;
fdb8a62ef17a fix docstring style in .m files
John W. Eaton <jwe@octave.org>
parents: 19505
diff changeset
96 ## incl = 440.0 / 44100.0;
fdb8a62ef17a fix docstring style in .m files
John W. Eaton <jwe@octave.org>
parents: 19505
diff changeset
97 ## incr = 443.0 / 44100.0;
fdb8a62ef17a fix docstring style in .m files
John W. Eaton <jwe@octave.org>
parents: 19505
diff changeset
98 ## nl = incl * frames;
fdb8a62ef17a fix docstring style in .m files
John W. Eaton <jwe@octave.org>
parents: 19505
diff changeset
99 ## nr = incr * frames;
fdb8a62ef17a fix docstring style in .m files
John W. Eaton <jwe@octave.org>
parents: 19505
diff changeset
100 ## left = sin (2.0 * pi * [lphase:incl:lphase+nl]);
fdb8a62ef17a fix docstring style in .m files
John W. Eaton <jwe@octave.org>
parents: 19505
diff changeset
101 ## right = sin (2.0 * pi * [rphase:incr:rphase+nr]);
fdb8a62ef17a fix docstring style in .m files
John W. Eaton <jwe@octave.org>
parents: 19505
diff changeset
102 ## sound = [left', right'];
fdb8a62ef17a fix docstring style in .m files
John W. Eaton <jwe@octave.org>
parents: 19505
diff changeset
103 ## status = 0;
fdb8a62ef17a fix docstring style in .m files
John W. Eaton <jwe@octave.org>
parents: 19505
diff changeset
104 ## lphase = lphase + nl;
fdb8a62ef17a fix docstring style in .m files
John W. Eaton <jwe@octave.org>
parents: 19505
diff changeset
105 ## rphase = rphase + nr;
fdb8a62ef17a fix docstring style in .m files
John W. Eaton <jwe@octave.org>
parents: 19505
diff changeset
106 ## endfunction
fdb8a62ef17a fix docstring style in .m files
John W. Eaton <jwe@octave.org>
parents: 19505
diff changeset
107 ## player = audioplayer (@@callback_sine, 44100);
fdb8a62ef17a fix docstring style in .m files
John W. Eaton <jwe@octave.org>
parents: 19505
diff changeset
108 ## play (player);
fdb8a62ef17a fix docstring style in .m files
John W. Eaton <jwe@octave.org>
parents: 19505
diff changeset
109 ## # play for as long as you want
fdb8a62ef17a fix docstring style in .m files
John W. Eaton <jwe@octave.org>
parents: 19505
diff changeset
110 ## stop (player);
19476
e1f98e402a7e New files - @audiorecorder and @audioplayer classes
Vytautas Jančauskas <unaudio@gmail.com>
parents:
diff changeset
111 ## @end group
30568
82b685157e2b doc: Update documentation in audio/ directory.
Rik <rik@octave.org>
parents: 30564
diff changeset
112 ################################################################################
19476
e1f98e402a7e New files - @audiorecorder and @audioplayer classes
Vytautas Jančauskas <unaudio@gmail.com>
parents:
diff changeset
113
e1f98e402a7e New files - @audiorecorder and @audioplayer classes
Vytautas Jančauskas <unaudio@gmail.com>
parents:
diff changeset
114 function player = audioplayer (varargin)
19523
5802ea7037d4 style fixes for audioplayer and audiorecorder classes
John W. Eaton <jwe@octave.org>
parents: 19509
diff changeset
115
5802ea7037d4 style fixes for audioplayer and audiorecorder classes
John W. Eaton <jwe@octave.org>
parents: 19509
diff changeset
116 if (nargin < 1 || nargin > 4
22544
6763f91685da audioplayer: fix input validation, fix crash when called with a single argument
Mike Miller <mtmiller@octave.org>
parents: 22441
diff changeset
117 || (nargin < 2 && ! (is_function_handle (varargin{1})
6763f91685da audioplayer: fix input validation, fix crash when called with a single argument
Mike Miller <mtmiller@octave.org>
parents: 22441
diff changeset
118 || ischar (varargin{1}))))
19476
e1f98e402a7e New files - @audiorecorder and @audioplayer classes
Vytautas Jančauskas <unaudio@gmail.com>
parents:
diff changeset
119 print_usage ();
e1f98e402a7e New files - @audiorecorder and @audioplayer classes
Vytautas Jančauskas <unaudio@gmail.com>
parents:
diff changeset
120 endif
19523
5802ea7037d4 style fixes for audioplayer and audiorecorder classes
John W. Eaton <jwe@octave.org>
parents: 19509
diff changeset
121
5802ea7037d4 style fixes for audioplayer and audiorecorder classes
John W. Eaton <jwe@octave.org>
parents: 19509
diff changeset
122 if (isa (varargin{1}, "audiorecorder"))
5802ea7037d4 style fixes for audioplayer and audiorecorder classes
John W. Eaton <jwe@octave.org>
parents: 19509
diff changeset
123 if (nargin == 1)
19503
36a26a131209 Apply Octave coding style to audio project additions
Mike Miller <mtmiller@ieee.org>
parents: 19500
diff changeset
124 player = getplayer (varargin{1});
19523
5802ea7037d4 style fixes for audioplayer and audiorecorder classes
John W. Eaton <jwe@octave.org>
parents: 19509
diff changeset
125 elseif (nargin == 2)
19476
e1f98e402a7e New files - @audiorecorder and @audioplayer classes
Vytautas Jančauskas <unaudio@gmail.com>
parents:
diff changeset
126 recorder = varargin{1};
e1f98e402a7e New files - @audiorecorder and @audioplayer classes
Vytautas Jančauskas <unaudio@gmail.com>
parents:
diff changeset
127 data = getaudiodata (recorder);
30572
6d96538052b9 Overhaul @audioplayer class.
Rik <rik@octave.org>
parents: 30568
diff changeset
128 player = audioplayer (data,
6d96538052b9 Overhaul @audioplayer class.
Rik <rik@octave.org>
parents: 30568
diff changeset
129 get (recorder, "SampleRate"),
6d96538052b9 Overhaul @audioplayer class.
Rik <rik@octave.org>
parents: 30568
diff changeset
130 get (recorder, "BitsPerSample"),
6d96538052b9 Overhaul @audioplayer class.
Rik <rik@octave.org>
parents: 30568
diff changeset
131 varargin{2});
19476
e1f98e402a7e New files - @audiorecorder and @audioplayer classes
Vytautas Jančauskas <unaudio@gmail.com>
parents:
diff changeset
132 else
e1f98e402a7e New files - @audiorecorder and @audioplayer classes
Vytautas Jančauskas <unaudio@gmail.com>
parents:
diff changeset
133 print_usage ();
e1f98e402a7e New files - @audiorecorder and @audioplayer classes
Vytautas Jančauskas <unaudio@gmail.com>
parents:
diff changeset
134 endif
e1f98e402a7e New files - @audiorecorder and @audioplayer classes
Vytautas Jančauskas <unaudio@gmail.com>
parents:
diff changeset
135 else
30572
6d96538052b9 Overhaul @audioplayer class.
Rik <rik@octave.org>
parents: 30568
diff changeset
136 ## FIXME: Prevent use of callbacks until situation is fixed.
6d96538052b9 Overhaul @audioplayer class.
Rik <rik@octave.org>
parents: 30568
diff changeset
137 if (is_function_handle (varargin{1}) || ischar (varargin{1}))
6d96538052b9 Overhaul @audioplayer class.
Rik <rik@octave.org>
parents: 30568
diff changeset
138 error ("audioplayer: first argument cannot be a callback function");
19476
e1f98e402a7e New files - @audiorecorder and @audioplayer classes
Vytautas Jančauskas <unaudio@gmail.com>
parents:
diff changeset
139 endif
30572
6d96538052b9 Overhaul @audioplayer class.
Rik <rik@octave.org>
parents: 30568
diff changeset
140 ## FIXME: Uncomment when callback functions are supported.
6d96538052b9 Overhaul @audioplayer class.
Rik <rik@octave.org>
parents: 30568
diff changeset
141 ## if (ischar (varargin{1}))
6d96538052b9 Overhaul @audioplayer class.
Rik <rik@octave.org>
parents: 30568
diff changeset
142 ## varargin{1} = str2func (varargin{1});
6d96538052b9 Overhaul @audioplayer class.
Rik <rik@octave.org>
parents: 30568
diff changeset
143 ## endif
19476
e1f98e402a7e New files - @audiorecorder and @audioplayer classes
Vytautas Jančauskas <unaudio@gmail.com>
parents:
diff changeset
144 player.player = __player_audioplayer__ (varargin{:});
e1f98e402a7e New files - @audiorecorder and @audioplayer classes
Vytautas Jančauskas <unaudio@gmail.com>
parents:
diff changeset
145 player = class (player, "audioplayer");
e1f98e402a7e New files - @audiorecorder and @audioplayer classes
Vytautas Jančauskas <unaudio@gmail.com>
parents:
diff changeset
146 endif
19523
5802ea7037d4 style fixes for audioplayer and audiorecorder classes
John W. Eaton <jwe@octave.org>
parents: 19509
diff changeset
147
19476
e1f98e402a7e New files - @audiorecorder and @audioplayer classes
Vytautas Jančauskas <unaudio@gmail.com>
parents:
diff changeset
148 endfunction
e1f98e402a7e New files - @audiorecorder and @audioplayer classes
Vytautas Jančauskas <unaudio@gmail.com>
parents:
diff changeset
149
19833
9fc020886ae9 maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents: 19697
diff changeset
150
19668
b2fe4dbe5266 Don't play or record audio during 'make check'
Mike Miller <mtmiller@ieee.org>
parents: 19571
diff changeset
151 %!demo
30572
6d96538052b9 Overhaul @audioplayer class.
Rik <rik@octave.org>
parents: 30568
diff changeset
152 %! ## Generate 2 seconds of white noise and play it back with a pause
19668
b2fe4dbe5266 Don't play or record audio during 'make check'
Mike Miller <mtmiller@ieee.org>
parents: 19571
diff changeset
153 %! fs = 44100;
22441
bb8914d68f25 Fix noise signal generation in audioplayer doc string and demos (bug #49011)
Mike Miller <mtmiller@octave.org>
parents: 22323
diff changeset
154 %! audio = 0.25 * randn (2, 2*fs);
19668
b2fe4dbe5266 Don't play or record audio during 'make check'
Mike Miller <mtmiller@ieee.org>
parents: 19571
diff changeset
155 %! player = audioplayer (audio, fs);
b2fe4dbe5266 Don't play or record audio during 'make check'
Mike Miller <mtmiller@ieee.org>
parents: 19571
diff changeset
156 %! play (player);
20923
58263bea2fdf Unified "sleep" functions to "octave_sleep" in C++ and "pause" in Octave.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents: 20852
diff changeset
157 %! pause (1);
19668
b2fe4dbe5266 Don't play or record audio during 'make check'
Mike Miller <mtmiller@ieee.org>
parents: 19571
diff changeset
158 %! pause (player);
20923
58263bea2fdf Unified "sleep" functions to "octave_sleep" in C++ and "pause" in Octave.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents: 20852
diff changeset
159 %! pause (1);
19668
b2fe4dbe5266 Don't play or record audio during 'make check'
Mike Miller <mtmiller@ieee.org>
parents: 19571
diff changeset
160 %! resume (player);
20923
58263bea2fdf Unified "sleep" functions to "octave_sleep" in C++ and "pause" in Octave.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents: 20852
diff changeset
161 %! pause (1);
19668
b2fe4dbe5266 Don't play or record audio during 'make check'
Mike Miller <mtmiller@ieee.org>
parents: 19571
diff changeset
162 %! stop (player);
b2fe4dbe5266 Don't play or record audio during 'make check'
Mike Miller <mtmiller@ieee.org>
parents: 19571
diff changeset
163
b2fe4dbe5266 Don't play or record audio during 'make check'
Mike Miller <mtmiller@ieee.org>
parents: 19571
diff changeset
164 ## Tests of audioplayer must not actually play anything.
b2fe4dbe5266 Don't play or record audio during 'make check'
Mike Miller <mtmiller@ieee.org>
parents: 19571
diff changeset
165
23361
7fbf0d4e48c9 skip some audio tests if no audio devices are available
John W. Eaton <jwe@octave.org>
parents: 23220
diff changeset
166 %!testif HAVE_PORTAUDIO; audiodevinfo (0) > 0
22441
bb8914d68f25 Fix noise signal generation in audioplayer doc string and demos (bug #49011)
Mike Miller <mtmiller@octave.org>
parents: 22323
diff changeset
167 %! mono = 0.25 * randn (1, 44100);
bb8914d68f25 Fix noise signal generation in audioplayer doc string and demos (bug #49011)
Mike Miller <mtmiller@octave.org>
parents: 22323
diff changeset
168 %! stereo = 0.25 * randn (2, 44100);
19509
2a16dac2176f fix audioplayer tests
John W. Eaton <jwe@octave.org>
parents: 19507
diff changeset
169 %! fs = 44100;
2a16dac2176f fix audioplayer tests
John W. Eaton <jwe@octave.org>
parents: 19507
diff changeset
170 %! player1 = audioplayer (mono, fs);
2a16dac2176f fix audioplayer tests
John W. Eaton <jwe@octave.org>
parents: 19507
diff changeset
171 %! player2 = audioplayer (stereo, fs);
2a16dac2176f fix audioplayer tests
John W. Eaton <jwe@octave.org>
parents: 19507
diff changeset
172 %! assert (player1.NumberOfChannels, 1);
2a16dac2176f fix audioplayer tests
John W. Eaton <jwe@octave.org>
parents: 19507
diff changeset
173 %! assert (player2.NumberOfChannels, 2);
2a16dac2176f fix audioplayer tests
John W. Eaton <jwe@octave.org>
parents: 19507
diff changeset
174 %! assert (player1.SampleRate, 44100);
2a16dac2176f fix audioplayer tests
John W. Eaton <jwe@octave.org>
parents: 19507
diff changeset
175 %! assert (player2.SampleRate, 44100);
2a16dac2176f fix audioplayer tests
John W. Eaton <jwe@octave.org>
parents: 19507
diff changeset
176 %! assert (player1.TotalSamples, 44100);
2a16dac2176f fix audioplayer tests
John W. Eaton <jwe@octave.org>
parents: 19507
diff changeset
177 %! assert (player2.TotalSamples, 44100);
19476
e1f98e402a7e New files - @audiorecorder and @audioplayer classes
Vytautas Jančauskas <unaudio@gmail.com>
parents:
diff changeset
178
23361
7fbf0d4e48c9 skip some audio tests if no audio devices are available
John W. Eaton <jwe@octave.org>
parents: 23220
diff changeset
179 %!testif HAVE_PORTAUDIO; audiodevinfo (0) > 0
30572
6d96538052b9 Overhaul @audioplayer class.
Rik <rik@octave.org>
parents: 30568
diff changeset
180 %! audio = randn (8000, 1);
19509
2a16dac2176f fix audioplayer tests
John W. Eaton <jwe@octave.org>
parents: 19507
diff changeset
181 %! fs = 44100;
30572
6d96538052b9 Overhaul @audioplayer class.
Rik <rik@octave.org>
parents: 30568
diff changeset
182 %! player = audioplayer (audio, fs, 16);
6d96538052b9 Overhaul @audioplayer class.
Rik <rik@octave.org>
parents: 30568
diff changeset
183 %! assert (player.NumberOfChannels, 1);
6d96538052b9 Overhaul @audioplayer class.
Rik <rik@octave.org>
parents: 30568
diff changeset
184 %! assert (player.SampleRate, 44100);
6d96538052b9 Overhaul @audioplayer class.
Rik <rik@octave.org>
parents: 30568
diff changeset
185 %! assert (player.BitsPerSample, 16);
6d96538052b9 Overhaul @audioplayer class.
Rik <rik@octave.org>
parents: 30568
diff changeset
186
6d96538052b9 Overhaul @audioplayer class.
Rik <rik@octave.org>
parents: 30568
diff changeset
187 ## FIXME: Callbacks do not work currently (5/31/2020) so BIST tests commented.
6d96538052b9 Overhaul @audioplayer class.
Rik <rik@octave.org>
parents: 30568
diff changeset
188 %!#function [sound, status] = callback (samples)
6d96538052b9 Overhaul @audioplayer class.
Rik <rik@octave.org>
parents: 30568
diff changeset
189 %!# sound = rand (samples, 2) - 0.5;
6d96538052b9 Overhaul @audioplayer class.
Rik <rik@octave.org>
parents: 30568
diff changeset
190 %!# status = 0;
6d96538052b9 Overhaul @audioplayer class.
Rik <rik@octave.org>
parents: 30568
diff changeset
191 %!#endfunction
19476
e1f98e402a7e New files - @audiorecorder and @audioplayer classes
Vytautas Jančauskas <unaudio@gmail.com>
parents:
diff changeset
192
30572
6d96538052b9 Overhaul @audioplayer class.
Rik <rik@octave.org>
parents: 30568
diff changeset
193 %!#testif HAVE_PORTAUDIO
6d96538052b9 Overhaul @audioplayer class.
Rik <rik@octave.org>
parents: 30568
diff changeset
194 %!# player = audioplayer (@callback, 44100);
6d96538052b9 Overhaul @audioplayer class.
Rik <rik@octave.org>
parents: 30568
diff changeset
195 %!# play (player);
6d96538052b9 Overhaul @audioplayer class.
Rik <rik@octave.org>
parents: 30568
diff changeset
196 %!# pause (2);
6d96538052b9 Overhaul @audioplayer class.
Rik <rik@octave.org>
parents: 30568
diff changeset
197 %!# stop (player);
6d96538052b9 Overhaul @audioplayer class.
Rik <rik@octave.org>
parents: 30568
diff changeset
198 %!# assert (1);
19476
e1f98e402a7e New files - @audiorecorder and @audioplayer classes
Vytautas Jančauskas <unaudio@gmail.com>
parents:
diff changeset
199
30572
6d96538052b9 Overhaul @audioplayer class.
Rik <rik@octave.org>
parents: 30568
diff changeset
200 %!#testif HAVE_PORTAUDIO
6d96538052b9 Overhaul @audioplayer class.
Rik <rik@octave.org>
parents: 30568
diff changeset
201 %!# player = audioplayer ("callback", 44100, 16);
6d96538052b9 Overhaul @audioplayer class.
Rik <rik@octave.org>
parents: 30568
diff changeset
202 %!# play (player);
6d96538052b9 Overhaul @audioplayer class.
Rik <rik@octave.org>
parents: 30568
diff changeset
203 %!# pause (2);
6d96538052b9 Overhaul @audioplayer class.
Rik <rik@octave.org>
parents: 30568
diff changeset
204 %!# stop (player);
6d96538052b9 Overhaul @audioplayer class.
Rik <rik@octave.org>
parents: 30568
diff changeset
205 %!# assert (1);
6d96538052b9 Overhaul @audioplayer class.
Rik <rik@octave.org>
parents: 30568
diff changeset
206
6d96538052b9 Overhaul @audioplayer class.
Rik <rik@octave.org>
parents: 30568
diff changeset
207 ## Verify input validation
28387
28676df1deaf Fix input validation for audioplayer (bug #57939).
Rik <rik@octave.org>
parents: 27923
diff changeset
208 %!testif HAVE_PORTAUDIO; audiodevinfo (0) > 0
28676df1deaf Fix input validation for audioplayer (bug #57939).
Rik <rik@octave.org>
parents: 27923
diff changeset
209 %! ## Verify nbits option only accepts 8, 16, 24
28676df1deaf Fix input validation for audioplayer (bug #57939).
Rik <rik@octave.org>
parents: 27923
diff changeset
210 %! fail ("audioplayer (1, 8e3, 9)", "NBITS must be 8, 16, or 24");
28676df1deaf Fix input validation for audioplayer (bug #57939).
Rik <rik@octave.org>
parents: 27923
diff changeset
211 %! fail ("audioplayer (1, 8e3, 32)", "NBITS must be 8, 16, or 24");
28676df1deaf Fix input validation for audioplayer (bug #57939).
Rik <rik@octave.org>
parents: 27923
diff changeset
212 %! player = audioplayer (1, 8e3, 8);
28676df1deaf Fix input validation for audioplayer (bug #57939).
Rik <rik@octave.org>
parents: 27923
diff changeset
213 %! player = audioplayer (1, 8e3, 16);
28676df1deaf Fix input validation for audioplayer (bug #57939).
Rik <rik@octave.org>
parents: 27923
diff changeset
214 %! player = audioplayer (1, 8e3, 24);
28676df1deaf Fix input validation for audioplayer (bug #57939).
Rik <rik@octave.org>
parents: 27923
diff changeset
215
30572
6d96538052b9 Overhaul @audioplayer class.
Rik <rik@octave.org>
parents: 30568
diff changeset
216 %!error <first argument cannot be a callback> audioplayer (@ls, 8000)
6d96538052b9 Overhaul @audioplayer class.
Rik <rik@octave.org>
parents: 30568
diff changeset
217 %!error <first argument cannot be a callback> audioplayer ("ls", 8000)