changeset 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 faf96757915a
children 3b2558676d0e
files scripts/audio/@audioplayer/__get_properties__.m scripts/audio/@audioplayer/audioplayer.m scripts/audio/@audioplayer/disp.m scripts/audio/@audioplayer/get.m scripts/audio/@audioplayer/isplaying.m scripts/audio/@audioplayer/pause.m scripts/audio/@audioplayer/play.m scripts/audio/@audioplayer/playblocking.m scripts/audio/@audioplayer/resume.m scripts/audio/@audioplayer/set.m scripts/audio/@audioplayer/stop.m scripts/audio/@audioplayer/subsasgn.m scripts/audio/@audioplayer/subsref.m
diffstat 13 files changed, 214 insertions(+), 129 deletions(-) [+]
line wrap: on
line diff
--- a/scripts/audio/@audioplayer/__get_properties__.m	Thu Dec 30 11:42:19 2021 -0800
+++ b/scripts/audio/@audioplayer/__get_properties__.m	Thu Dec 30 16:11:55 2021 -0800
@@ -32,44 +32,36 @@
 
 function props = __get_properties__ (player)
 
-  if (nargin < 1)
-    print_usage ();
-  endif
-
-  if (__player_isplaying__ (struct (player).player))
-    running = "on";
-  else
-    running = "off";
-  endif
+  hplayer = struct (player).player;
 
   props = struct ("BitsPerSample",
-                  __player_get_nbits__ (struct (player).player),
+                  __player_get_nbits__ (hplayer),
 
                   "CurrentSample",
-                  __player_get_sample_number__ (struct (player).player),
+                  __player_get_sample_number__ (hplayer),
 
                   "DeviceID",
-                  __player_get_id__ (struct (player).player),
+                  __player_get_id__ (hplayer),
 
                   "NumberOfChannels",
-                  __player_get_channels__ (struct (player).player),
+                  __player_get_channels__ (hplayer),
 
                   "Running",
-                  running,
+                  ifelse (__player_isplaying__ (hplayer), "on", "off"),
 
                   "SampleRate",
-                  __player_get_fs__ (struct (player).player),
+                  __player_get_fs__ (hplayer),
 
                   "TotalSamples",
-                  __player_get_total_samples__ (struct (player).player),
+                  __player_get_total_samples__ (hplayer),
 
                   "Tag",
-                  __player_get_tag__ (struct (player).player),
+                  __player_get_tag__ (hplayer),
 
                   "Type",
                   "audioplayer",
 
                   "UserData",
-                  __player_get_userdata__ (struct (player).player));
+                  __player_get_userdata__ (hplayer));
 
 endfunction
--- a/scripts/audio/@audioplayer/audioplayer.m	Thu Dec 30 11:42:19 2021 -0800
+++ b/scripts/audio/@audioplayer/audioplayer.m	Thu Dec 30 16:11:55 2021 -0800
@@ -125,15 +125,22 @@
     elseif (nargin == 2)
       recorder = varargin{1};
       data = getaudiodata (recorder);
-      player = audioplayer (data, get (recorder, "SampleRate"),
-                            get (recorder, "BitsPerSample"), varargin{2});
+      player = audioplayer (data,
+                            get (recorder, "SampleRate"),
+                            get (recorder, "BitsPerSample"),
+                            varargin{2});
     else
       print_usage ();
     endif
   else
-    if (ischar (varargin{1}))
-      varargin{1} = str2func (varargin{1});
+    ## FIXME: Prevent use of callbacks until situation is fixed.
+    if (is_function_handle (varargin{1}) || ischar (varargin{1}))
+      error ("audioplayer: first argument cannot be a callback function");
     endif
+    ## FIXME: Uncomment when callback functions are supported.
+    ## if (ischar (varargin{1}))
+    ##   varargin{1} = str2func (varargin{1});
+    ## endif
     player.player = __player_audioplayer__ (varargin{:});
     player = class (player, "audioplayer");
   endif
@@ -142,6 +149,7 @@
 
 
 %!demo
+%! ## Generate 2 seconds of white noise and play it back with a pause
 %! fs = 44100;
 %! audio = 0.25 * randn (2, 2*fs);
 %! player = audioplayer (audio, fs);
@@ -169,37 +177,34 @@
 %! assert (player2.TotalSamples, 44100);
 
 %!testif HAVE_PORTAUDIO; audiodevinfo (0) > 0
-%! audio = 0.25 * randn (2, 44100);
-%! fs = 44100;
-%! player = audioplayer (audio, fs);
-%! set (player, {"SampleRate", "Tag", "UserData"}, {8000, "tag", [1, 2; 3, 4]});
-%! assert (player.SampleRate, 8000);
-%! assert (player.Tag, "tag");
-%! assert (player.UserData, [1, 2; 3, 4]);
-
-%!testif HAVE_PORTAUDIO; audiodevinfo (0) > 0
-%! audio = 0.25 * randn (2, 44100);
+%! audio = randn (8000, 1);
 %! fs = 44100;
-%! player = audioplayer (audio, fs);
-%! settable = set (player);
-%! settable.SampleRate = 8000;
-%! settable.Tag = "tag";
-%! settable.UserData = [1, 2; 3, 4];
-%! set (player, settable);
-%! assert (player.SampleRate, 8000);
-%! assert (player.Tag, "tag");
-%! assert (player.UserData, [1, 2; 3, 4]);
+%! player = audioplayer (audio, fs, 16);
+%! assert (player.NumberOfChannels, 1);
+%! assert (player.SampleRate, 44100);
+%! assert (player.BitsPerSample, 16);
+
+## FIXME: Callbacks do not work currently (5/31/2020) so BIST tests commented.
+%!#function [sound, status] = callback (samples)
+%!#  sound = rand (samples, 2) - 0.5;
+%!#  status = 0;
+%!#endfunction
 
-%!testif HAVE_PORTAUDIO; audiodevinfo (0) > 0
-%! audio = 0.25 * randn (2, 44100);
-%! fs = 44100;
-%! player = audioplayer (audio, fs);
-%! player.SampleRate = 8000;
-%! player.Tag = "tag";
-%! player.UserData = [1, 2; 3, 4];
-%! properties = get (player, {"SampleRate", "Tag", "UserData"});
-%! assert (properties, {8000, "tag", [1, 2; 3, 4]});
+%!#testif HAVE_PORTAUDIO
+%!# player = audioplayer (@callback, 44100);
+%!# play (player);
+%!# pause (2);
+%!# stop (player);
+%!# assert (1);
 
+%!#testif HAVE_PORTAUDIO
+%!# player = audioplayer ("callback", 44100, 16);
+%!# play (player);
+%!# pause (2);
+%!# stop (player);
+%!# assert (1);
+
+## Verify input validation
 %!testif HAVE_PORTAUDIO; audiodevinfo (0) > 0
 %! ## Verify nbits option only accepts 8, 16, 24
 %! fail ("audioplayer (1, 8e3, 9)", "NBITS must be 8, 16, or 24");
@@ -208,22 +213,5 @@
 %! player = audioplayer (1, 8e3, 16);
 %! player = audioplayer (1, 8e3, 24);
 
-## FIXME: Callbacks do not work currently (5/31/2020) so BIST tests commented.
-#%!function [sound, status] = callback (samples)
-#%!  sound = rand (samples, 2) - 0.5;
-#%!  status = 0;
-#%!endfunction
-
-#%!testif HAVE_PORTAUDIO
-#%! player = audioplayer (@callback, 44100);
-#%! play (player);
-#%! pause (2);
-#%! stop (player);
-#%! assert (1);
-
-#%!testif HAVE_PORTAUDIO
-#%! player = audioplayer ("callback", 44100, 16);
-#%! play (player);
-#%! pause (2);
-#%! stop (player);
-#%! assert (1);
+%!error <first argument cannot be a callback> audioplayer (@ls, 8000)
+%!error <first argument cannot be a callback> audioplayer ("ls", 8000)
--- a/scripts/audio/@audioplayer/disp.m	Thu Dec 30 11:42:19 2021 -0800
+++ b/scripts/audio/@audioplayer/disp.m	Thu Dec 30 16:11:55 2021 -0800
@@ -31,13 +31,13 @@
 
 function disp (player)
 
-  if (nargin < 1)
-    print_usage ();
-  endif
-
   printf ("audioplayer object with properties:\n\n");
   for [val, prop] = __get_properties__ (player)
     printf ("  %s = ", prop), disp (val);
   endfor
 
 endfunction
+
+
+## No tests possible/needed for this function
+%!assert (1)
--- a/scripts/audio/@audioplayer/get.m	Thu Dec 30 11:42:19 2021 -0800
+++ b/scripts/audio/@audioplayer/get.m	Thu Dec 30 16:11:55 2021 -0800
@@ -37,28 +37,62 @@
 ## @seealso{@audioplayer/set, @audioplayer/audioplayer}
 ## @end deftypefn
 
-function retval = get (varargin)
+function value = get (player, name)
 
-  if (nargin < 1 || nargin > 2)
-    print_usage ();
-  endif
-
-  properties = __get_properties__ (varargin{1});
+  properties = __get_properties__ (player);
 
   if (nargin == 1)
-    retval = properties;
+    value = properties;
   elseif (nargin == 2)
-    pnames = varargin{2};
+    pnames = name;
     if (ischar (pnames))
-      retval = getfield (properties, pnames);
+      value = getproperty (properties, pnames);
     elseif (iscellstr (pnames))
-      retval = cell (size (pnames));
+      value = cell (size (pnames));
       for i = 1:numel (pnames)
-        retval{i} = getfield (properties, pnames{i});
+        value{i} = getproperty (properties, pnames{i});
       endfor
     else
-      error ("@audioplayer/get: invalid NAME argument");
+      error ("@audioplayer/get: NAME must be a string or cell array of strings");
     endif
   endif
 
 endfunction
+
+function value = getproperty (properties, pname)
+
+  persistent valid_props;
+  if (isempty (valid_props))
+    valid_props = { "BitsPerSample", "CurrentSample", "DeviceID", ...
+                    "NumberOfChannels", "Running", "SampleRate", ...
+                    "TotalSamples", "Tag", "Type", "UserData" };
+  endif
+
+  idx = find (strcmpi (pname, valid_props), 1);
+  if (isempty (idx))
+    error ('@audioplayer/get: "%s" is not a valid property name', pname);
+  endif
+
+  value = properties.(valid_props{idx});
+
+endfunction
+
+
+%!testif HAVE_PORTAUDIO; audiodevinfo (0) > 0
+%! player = audioplayer ([-1, 1], 44100, 8);
+%! props = get (player);
+%! assert (fieldnames (props), {"BitsPerSample"; "CurrentSample"; "DeviceID";
+%!         "NumberOfChannels"; "Running"; "SampleRate"; "TotalSamples"; "Tag";
+%!         "Type"; "UserData"});
+%! value = get (player, "Running");
+%! assert (value, "off");
+%! value = get (player, "ruNNIng");  # test case insensitivity
+%! assert (value, "off");
+%! values = get (player, {"SampleRate", "BitsPerSample", "TotalSamples"});
+%! assert (values, {44100, 8, 2});
+
+## Test input validation
+%!testif HAVE_PORTAUDIO; audiodevinfo (0) > 0
+%! player = audioplayer ([-1, 1], 44100, 8);
+%! fail ("get (player, 1)", "NAME must be a string");
+%! fail ('get (player, "foobar")', '"foobar" is not a valid property');
--- a/scripts/audio/@audioplayer/isplaying.m	Thu Dec 30 11:42:19 2021 -0800
+++ b/scripts/audio/@audioplayer/isplaying.m	Thu Dec 30 16:11:55 2021 -0800
@@ -32,10 +32,11 @@
 
 function tf = isplaying (player)
 
-  if (nargin < 1)
-    print_usage ();
-  endif
-
   tf = __player_isplaying__ (struct (player).player);
 
 endfunction
+
+
+%!testif HAVE_PORTAUDIO; audiodevinfo (1) > 0
+%! player = audioplayer ([-1, 1], 44100, 8);
+%! assert (isplaying (player), false);
--- a/scripts/audio/@audioplayer/pause.m	Thu Dec 30 11:42:19 2021 -0800
+++ b/scripts/audio/@audioplayer/pause.m	Thu Dec 30 16:11:55 2021 -0800
@@ -31,10 +31,10 @@
 
 function pause (player)
 
-  if (nargin < 1)
-    print_usage ();
-  endif
-
   __player_pause__ (struct (player).player);
 
 endfunction
+
+
+## No tests possible for this function
+%!assert (1)
--- a/scripts/audio/@audioplayer/play.m	Thu Dec 30 11:42:19 2021 -0800
+++ b/scripts/audio/@audioplayer/play.m	Thu Dec 30 16:11:55 2021 -0800
@@ -38,12 +38,18 @@
 ## @audioplayer/audioplayer}
 ## @end deftypefn
 
-function play (varargin)
+function play (player, length)
+
+  hplayer = struct (player).player;
 
-  if (nargin < 1 || nargin > 2)
-    print_usage ();
+  if (nargin == 1)
+    __player_play__ (hplayer);
+  else
+    __player_play__ (hplayer, length);
   endif
 
-  __player_play__ (struct (varargin{1}).player, varargin{2:end});
+endfunction
+
 
-endfunction
+## No tests possible for this function
+%!assert (1)
--- a/scripts/audio/@audioplayer/playblocking.m	Thu Dec 30 11:42:19 2021 -0800
+++ b/scripts/audio/@audioplayer/playblocking.m	Thu Dec 30 16:11:55 2021 -0800
@@ -31,7 +31,7 @@
 ## (synchronous I/O).
 ##
 ## If the optional argument @var{start} is provided, begin playing
-## @var{start} samples in to the recording.
+## @var{start} samples into the recording.
 ##
 ## If the optional argument @var{end} is provided, stop playing at
 ## @var{end} samples into the recording.
@@ -39,12 +39,16 @@
 ## @audioplayer/audioplayer}
 ## @end deftypefn
 
-function playblocking (varargin)
+function playblocking (player, length)
 
-  if (nargin < 1 || nargin > 2)
+  if (nargin != 2)
     print_usage ();
   endif
 
-  __player_playblocking__ (struct (varargin{1}).player, varargin{2:end});
+  __player_playblocking__ (struct (player).player, length);
 
 endfunction
+
+
+## No tests possible for this function
+%!assert (1)
--- a/scripts/audio/@audioplayer/resume.m	Thu Dec 30 11:42:19 2021 -0800
+++ b/scripts/audio/@audioplayer/resume.m	Thu Dec 30 16:11:55 2021 -0800
@@ -31,10 +31,10 @@
 
 function resume (player)
 
-  if (nargin < 1)
-    print_usage ();
-  endif
-
   __player_resume__ (struct (player).player);
 
 endfunction
+
+
+## No tests possible for this function
+%!assert (1)
--- a/scripts/audio/@audioplayer/set.m	Thu Dec 30 11:42:19 2021 -0800
+++ b/scripts/audio/@audioplayer/set.m	Thu Dec 30 16:11:55 2021 -0800
@@ -38,49 +38,81 @@
 ## @seealso{@audioplayer/get, @audioplayer/audioplayer}
 ## @end deftypefn
 
-function properties = set (varargin)
+function properties = set (player, varargin)
 
-  if (nargin < 1 || nargin > 3)
+  if (nargin > 3)
     print_usage ();
   endif
 
-  player = struct (varargin{1}).player;
+  hplayer = struct (player).player;
 
   if (nargin == 1)
-    properties.SampleRate = {};
-    properties.Tag = {};
-    properties.UserData = {};
+    properties = struct ("SampleRate", {{}}, "Tag", {{}}, "UserData", {{}});
   elseif (nargin == 2)
-    for [value, property] = varargin{2}
-      setproperty (player, property, value);
+    for [value, property] = varargin{1}
+      setproperty (hplayer, property, value);
     endfor
   elseif (nargin == 3)
-    if (iscell (varargin{2}))
+    if (iscell (varargin{1}))
       index = 1;
-      for property = varargin{2}
-        setproperty (player, char (property), varargin{3}{index});
+      for property = varargin{1}
+        setproperty (hplayer, char (property), varargin{2}{index});
         index += 1;
       endfor
     else
-      setproperty (player, varargin{2}, varargin{3});
+      setproperty (hplayer, varargin{1}, varargin{2});
     endif
-  else
-    error ("@audioplayer/set: wrong number of arguments to the set method");
   endif
 
 endfunction
 
 function setproperty (player, property, value)
 
-  switch (property)
-    case "SampleRate"
+  switch (lower (property))
+    case "samplerate"
       __player_set_fs__ (player, value);
-    case "Tag"
+    case "tag"
       __player_set_tag__ (player, value);
-    case "UserData"
+    case "userdata"
       __player_set_userdata__ (player, value);
     otherwise
-      error ("audioplayer: no such property or the property specified is read-only");
+      error ('@audioplayer/set: "%s" is not a valid property name or is read-only', property);
   endswitch
 
 endfunction
+
+
+%!testif HAVE_PORTAUDIO; audiodevinfo (0) > 0
+%! player = audioplayer ([-1, 1], 44100, 8);
+%! set (player, "SampleRate", 8800);
+%! set (player, "Tag", "mytag");
+%! ## Also test case insensitivity
+%! set (player, "USERdata", [1, 2; 3, 4]);
+%! assert (player.SampleRate, 8800);
+%! assert (player.Tag, "mytag");
+%! assert (player.UserData, [1, 2; 3, 4]);
+
+%!testif HAVE_PORTAUDIO; audiodevinfo (0) > 0
+%! player = audioplayer ([-1, 1], 44100, 8);
+%! set (player, {"SampleRate", "Tag", "UserData"},
+%!              {8800, "mytag", [1, 2; 3, 4]});
+%! assert (player.SampleRate, 8800);
+%! assert (player.Tag, "mytag");
+%! assert (player.UserData, [1, 2; 3, 4]);
+
+%!testif HAVE_PORTAUDIO; audiodevinfo (0) > 0
+%! player = audioplayer ([-1, 1], 44100, 8);
+%! props = set (player);
+%! props.SampleRate = 8800;
+%! props.Tag = "mytag";
+%! props.UserData = [1, 2; 3, 4];
+%! set (player, props);
+%! assert (player.SampleRate, 8800);
+%! assert (player.Tag, "mytag");
+%! assert (player.UserData, [1, 2; 3, 4]);
+
+## Test input validation
+%!testif HAVE_PORTAUDIO; audiodevinfo (0) > 0
+%! player = audioplayer ([-1, 1], 44100, 8);
+%! fail ('set (player, "foobar", 1)', "not a valid property name"); 
+%! fail ('set (player, "Running", 1)', "is read-only"); 
--- a/scripts/audio/@audioplayer/stop.m	Thu Dec 30 11:42:19 2021 -0800
+++ b/scripts/audio/@audioplayer/stop.m	Thu Dec 30 16:11:55 2021 -0800
@@ -32,10 +32,10 @@
 
 function stop (player)
 
-  if (nargin < 1)
-    print_usage ();
-  endif
-
   __player_stop__ (struct (player).player);
 
 endfunction
+
+
+## No tests possible for this function
+%!assert (1)
--- a/scripts/audio/@audioplayer/subsasgn.m	Thu Dec 30 11:42:19 2021 -0800
+++ b/scripts/audio/@audioplayer/subsasgn.m	Thu Dec 30 16:11:55 2021 -0800
@@ -33,6 +33,10 @@
 
 function value = subsasgn (player, idx, rhs)
 
+  if (nargin != 3)
+    print_usage ();
+  endif
+
   if (isempty (idx))
     error ("audioplayer: missing index");
   endif
@@ -46,3 +50,15 @@
   endif
 
 endfunction
+
+
+%!testif HAVE_PORTAUDIO; audiodevinfo (0) > 0
+%! player = audioplayer ([-1, 1], 44100, 8);
+%! player.Tag = "mytag";
+%! assert (get (player, "Tag"), "mytag");
+
+## Test input validation
+%!testif HAVE_PORTAUDIO; audiodevinfo (0) > 0
+%! player = audioplayer ([-1, 1], 44100, 8);
+%! fail ("player(1).Tag = 5", "invalid subscript type");
+%! fail ("player{1}.Tag = 5", "invalid subscript type");
--- a/scripts/audio/@audioplayer/subsref.m	Thu Dec 30 11:42:19 2021 -0800
+++ b/scripts/audio/@audioplayer/subsref.m	Thu Dec 30 16:11:55 2021 -0800
@@ -49,3 +49,15 @@
   endif
 
 endfunction
+
+
+%!testif HAVE_PORTAUDIO; audiodevinfo (0) > 0
+%! player = audioplayer ([-1, 1], 44100, 8);
+%! set (player, "Tag", "mytag");
+%! assert (player.Tag, "mytag");
+
+## Test input validation
+%!testif HAVE_PORTAUDIO; audiodevinfo (0) > 0
+%! player = audioplayer ([-1, 1], 44100, 8);
+%! fail ("player(1).Tag", "invalid subscript type");
+%! fail ("player{1}.Tag", "invalid subscript type");