changeset 30569:2d6e60776588

Overhaul @audiorecorder class. Eliminate unnecessary input validation that one argument is supplied to class methods as interpreter guarantees the first argument is an @audiorecorder object. Accept case-insensitive property names for get()/set() functions. Add BIST tests on a per function basis rather than only in @audiorecorder constructor. * @audiorecorder/__get_properties__.m: Eliminate nargin checking. Use intermediate variable hrecorder to clarify code. Use ifelse() to simplify 5-line if/else/endif tree. * @audiorecorder/audiorecorder.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 @audiorecorder functionality to the methods files. Add input validation BIST tests for callback function validation. * @audiorecorder/disp.m: Eliminate nargin checking. Mark file as tested for BIST. * @audiorecorder/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. * @audiorecorder/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. * @audiorecorder/getaudiodata.m: Eliminate nargin checking. Use intermediate variable hrecorder to clarify code. Use input parameters with names matching documentation rather than varargin. Add case statements for "double" and "single" to support these datatypes. Add input validation for DATATYPE input. Use transpose rather than Hermitian conjugate on data which is never complex. Add BIST tests. * @audiorecorder/getplayer.m: Eliminate nargin checking. Use input parameters with names matching documentation rather than varargin. Add BIST tests, but commented out for now. * @audiorecorder/isrecording.m: Eliminate nargin checking. Add BIST tests. * @audiorecorder/pause.m: Eliminate nargin checking. Mark file as tested for BIST. * @audiorecorder/play.m: Eliminate nargin checking. Use input parameters with names matching documentation rather than varargin. Mark file as tested for BIST. * @audiorecorder/record.m: Eliminate nargin checking. Use input parameters with names matching documentation rather than varargin. Use intermediate variable hrecorder to clarify code. Mark file as tested for BIST. * @audiorecorder/recordblocking.m: Use input parameters with names matching documentation rather than varargin. Mark file as tested for BIST. * @audiorecorder/resume.m: Eliminate nargin checking. Mark file as tested for BIST. * @audiorecorder/set.m: Eliminate nargin checking of first argument. Use input parameter "recorder" for first argument rather than varargin. Use intermediate variable hrecorder to clarify code. Add BIST tests. * @audiorecorder/set.m (setproperty): Use lower() to implement case insensitive matching of property names. Rewrite error() message to be clearer and report the incorrect name. * @audiorecorder/stop.m: Eliminate nargin checking. Mark file as tested for BIST. * @audiorecorder/subsasgn.m: Change output variable name to "recorder" for clarity. Add BIST tests. * @audiorecorder/subsref.m: Add BIST tests.
author Rik <rik@octave.org>
date Thu, 30 Dec 2021 11:20:10 -0800
parents 82b685157e2b
children d3b1d0e770e2
files scripts/audio/@audiorecorder/__get_properties__.m scripts/audio/@audiorecorder/audiorecorder.m scripts/audio/@audiorecorder/disp.m scripts/audio/@audiorecorder/get.m scripts/audio/@audiorecorder/getaudiodata.m scripts/audio/@audiorecorder/getplayer.m scripts/audio/@audiorecorder/isrecording.m scripts/audio/@audiorecorder/pause.m scripts/audio/@audiorecorder/play.m scripts/audio/@audiorecorder/record.m scripts/audio/@audiorecorder/recordblocking.m scripts/audio/@audiorecorder/resume.m scripts/audio/@audiorecorder/set.m scripts/audio/@audiorecorder/stop.m scripts/audio/@audiorecorder/subsasgn.m scripts/audio/@audiorecorder/subsref.m
diffstat 16 files changed, 273 insertions(+), 167 deletions(-) [+]
line wrap: on
line diff
--- a/scripts/audio/@audiorecorder/__get_properties__.m	Wed Dec 29 18:17:53 2021 -0800
+++ b/scripts/audio/@audiorecorder/__get_properties__.m	Thu Dec 30 11:20:10 2021 -0800
@@ -33,44 +33,36 @@
 
 function props = __get_properties__ (recorder)
 
-  if (nargin < 1)
-    print_usage ();
-  endif
-
-  if (__recorder_isrecording__ (struct (recorder).recorder))
-    running = "on";
-  else
-    running = "off";
-  endif
+  hrecorder = struct (recorder).recorder;
 
   props = struct ("BitsPerSample",
-                  __recorder_get_nbits__ (struct (recorder).recorder),
+                  __recorder_get_nbits__ (hrecorder),
 
                   "CurrentSample",
-                  __recorder_get_sample_number__ (struct (recorder).recorder),
+                  __recorder_get_sample_number__ (hrecorder),
 
                   "DeviceID",
-                  __recorder_get_id__ (struct (recorder).recorder),
+                  __recorder_get_id__ (hrecorder),
 
                   "NumberOfChannels",
-                  __recorder_get_channels__ (struct (recorder).recorder),
+                  __recorder_get_channels__ (hrecorder),
 
                   "Running",
-                  running,
+                  ifelse (__recorder_isrecording__ (hrecorder), "on", "off"),
 
                   "SampleRate",
-                  __recorder_get_fs__ (struct (recorder).recorder),
+                  __recorder_get_fs__ (hrecorder),
 
                   "TotalSamples",
-                  __recorder_get_total_samples__ (struct (recorder).recorder),
+                  __recorder_get_total_samples__ (hrecorder),
 
                   "Tag",
-                  __recorder_get_tag__ (struct (recorder).recorder),
+                  __recorder_get_tag__ (hrecorder),
 
                   "Type",
                   "audiorecorder",
 
                   "UserData",
-                  __recorder_get_userdata__ (struct (recorder).recorder));
+                  __recorder_get_userdata__ (hrecorder));
 
 endfunction
--- a/scripts/audio/@audiorecorder/audiorecorder.m	Wed Dec 29 18:17:53 2021 -0800
+++ b/scripts/audio/@audiorecorder/audiorecorder.m	Thu Dec 30 11:20:10 2021 -0800
@@ -76,9 +76,15 @@
     print_usage ();
   endif
 
-  if (nargin > 0 && ischar (varargin{1}))
-    varargin{1} = str2func (varargin{1});
+  ## FIXME: Prevent use of callbacks until situation is fixed.
+  if (nargin > 0 && (is_function_handle (varargin{1}) || ischar (varargin{1})))
+    error ("audiorecorder: first argument cannot be a callback function");
   endif
+  
+  ## FIXME: Uncomment when callback functions are supported.
+  ## if (nargin > 0 && ischar (varargin{1}))
+  ##   varargin{1} = str2func (varargin{1});
+  ## endif
 
   recorder.recorder = __recorder_audiorecorder__ (varargin{:});
   recorder = class (recorder, "audiorecorder");
@@ -87,6 +93,7 @@
 
 
 %!demo
+%! ## Record 1 second of audio and play it back in two ways
 %! recorder = audiorecorder (44100, 16, 2);
 %! record (recorder, 1);
 %! pause (2);
@@ -101,67 +108,34 @@
 
 ## Tests of audiorecorder must not actually record anything.
 
-%!testif HAVE_PORTAUDIO; audiodevinfo (1) > 0
-%! recorder = audiorecorder (44100, 16, 2);
-%! data = getaudiodata (recorder, "int16");
-%! assert (strcmp (class (data), "int16"));
-%! data = getaudiodata (recorder, "int8");
-%! assert (strcmp (class (data), "int8"));
-%! data = getaudiodata (recorder, "uint8");
-%! assert (strcmp (class (data), "uint8"));
-%! assert (size (data)(1), recorder.TotalSamples);
-%! assert (size (data)(2), 2);
-
-%!testif HAVE_PORTAUDIO; audiodevinfo (1) > 0
-%! recorder = audiorecorder ();
-%! set (recorder, {"SampleRate", "Tag", "UserData"},
-%!                {8000, "tag", [1, 2; 3, 4]});
-%! assert (recorder.SampleRate, 8000);
-%! assert (recorder.Tag, "tag");
-%! assert (recorder.UserData, [1, 2; 3, 4]);
-
-%!testif HAVE_PORTAUDIO; audiodevinfo (1) > 0
-%! recorder = audiorecorder ();
-%! props = set (recorder);
-%! props.SampleRate = 8000;
-%! props.Tag = "tag";
-%! props.UserData = [1, 2; 3, 4];
-%! set (recorder, props);
-%! assert (recorder.SampleRate, 8000);
-%! assert (recorder.Tag, "tag");
-%! assert (recorder.UserData, [1, 2; 3, 4]);
+## FIXME: Uncomment when callbacks are supported
+%!#function status = callback_record (sound)
+%!#  fid = fopen ("record.txt", "at");
+%!#  for index = 1:rows(sound)
+%!#    fprintf (fid, "%.4f, %.4f\n", sound(index, 1), sound(index, 2));
+%!#  endfor
+%!#  fclose (fid);
+%!#  status = 0;
+%!#endfunction
 
-%!testif HAVE_PORTAUDIO; audiodevinfo (1) > 0
-%! recorder = audiorecorder ();
-%! recorder.SampleRate = 8000;
-%! recorder.Tag = "tag";
-%! recorder.UserData = [1, 2; 3, 4];
-%! properties = get (recorder, {"SampleRate", "Tag", "UserData"});
-%! assert (properties, {8000, "tag", [1, 2; 3, 4]});
-
-#%!function status = callback_record (sound)
-#%!  fid = fopen ("record.txt", "at");
-#%!  for index = 1:rows(sound)
-#%!    fprintf (fid, "%.4f, %.4f\n", sound(index, 1), sound(index, 2));
-#%!  endfor
-#%!  fclose (fid);
-#%!  status = 0;
-#%!endfunction
+%!#testif HAVE_PORTAUDIO
+%!# recorder = audiorecorder (@callback_record, 44100);
+%!# unlink ("record.txt")
+%!# record (recorder);
+%!# pause (2);
+%!# stop (recorder);
+%!# s = stat ("record.txt");
+%!# assert (s.size > 0);
 
-#%!testif HAVE_PORTAUDIO
-#%! recorder = audiorecorder (@callback_record, 44100);
-#%! unlink ("record.txt")
-#%! record (recorder);
-#%! pause (2);
-#%! stop (recorder);
-#%! s = stat ("record.txt");
-#%! assert (s.size > 0);
+%!#testif HAVE_PORTAUDIO
+%!# recorder = audiorecorder (@callback_record, 44100);
+%!# unlink ("record.txt")
+%!# record (recorder);
+%!# pause (2);
+%!# stop (recorder);
+%!# s = stat ("record.txt");
+%!# assert (s.size > 0);
 
-#%!testif HAVE_PORTAUDIO
-#%! recorder = audiorecorder (@callback_record, 44100);
-#%! unlink ("record.txt")
-#%! record (recorder);
-#%! pause (2);
-#%! stop (recorder);
-#%! s = stat ("record.txt");
-#%! assert (s.size > 0);
+## Test input validation
+%!error <first argument cannot be a callback> audiorecorder (@ls)
+%!error <first argument cannot be a callback> audiorecorder ("ls")
--- a/scripts/audio/@audiorecorder/disp.m	Wed Dec 29 18:17:53 2021 -0800
+++ b/scripts/audio/@audiorecorder/disp.m	Thu Dec 30 11:20:10 2021 -0800
@@ -31,13 +31,13 @@
 
 function disp (recorder)
 
-  if (nargin < 1)
-    print_usage ();
-  endif
-
   printf ("audiorecorder object with properties:\n\n");
   for [val, prop] = __get_properties__ (recorder)
     printf ("  %s = ", prop), disp (val);
   endfor
 
 endfunction
+
+
+## No tests possible/needed for this function
+%!assert (1)
--- a/scripts/audio/@audiorecorder/get.m	Wed Dec 29 18:17:53 2021 -0800
+++ b/scripts/audio/@audiorecorder/get.m	Thu Dec 30 11:20:10 2021 -0800
@@ -37,28 +37,60 @@
 ## @seealso{@audiorecorder/set, @audiorecorder/audiorecorder}
 ## @end deftypefn
 
-function retval = get (varargin)
+function value = get (recorder, name)
 
-  if (nargin < 1 || nargin > 2)
-    print_usage ();
-  endif
-
-  properties = __get_properties__ (varargin{1});
+  properties = __get_properties__ (recorder);
 
   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 ("@audiorecorder/get: invalid NAME argument");
+      error ("@audiorecorder/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 ('@audiorecorder/get: "%s" is not a valid property name', pname);
+  endif
+
+  value = properties.(valid_props{idx});
+
+endfunction
+
+
+%!testif HAVE_PORTAUDIO; audiodevinfo (1) > 0
+%! recorder = audiorecorder (44100, 16, 2);
+%! props = get (recorder);
+%! assert (fieldnames (props), {"BitsPerSample"; "CurrentSample"; "DeviceID";
+%!         "NumberOfChannels"; "Running"; "SampleRate"; "TotalSamples"; "Tag";
+%!         "Type"; "UserData"});
+%! value = get (recorder, "Running");
+%! assert (value, "off");
+%! values = get (recorder, {"SampleRate", "BitsPerSample", "NumberOfChannels"});
+%! assert (values, {44100, 16, 2});
+
+## Test input validation
+%!testif HAVE_PORTAUDIO; audiodevinfo (1) > 0
+%! recorder = audiorecorder (44100, 16, 2);
+%! fail ("get (recorder, 1)", "NAME must be a string");
+%! fail ('get (recorder, "foobar")', '"foobar" is not a valid property');
--- a/scripts/audio/@audiorecorder/getaudiodata.m	Wed Dec 29 18:17:53 2021 -0800
+++ b/scripts/audio/@audiorecorder/getaudiodata.m	Thu Dec 30 11:20:10 2021 -0800
@@ -36,33 +36,64 @@
 ## @seealso{@audiorecorder/audiorecorder}
 ## @end deftypefn
 
-function data = getaudiodata (varargin)
+function data = getaudiodata (recorder, datatype)
 
-  if (nargin < 1 || nargin > 2)
-    print_usage ();
-  endif
-
-  recorder = varargin{1};
+  hrecorder = struct (recorder).recorder;
 
   if (nargin == 1)
-    data = __recorder_getaudiodata__ (struct (recorder).recorder);
+    data = __recorder_getaudiodata__ (hrecorder);
   else
-    data = __recorder_getaudiodata__ (struct (recorder).recorder);
-    type = varargin{2};
-    switch (type)
+    data = __recorder_getaudiodata__ (hrecorder);
+    switch (datatype)
+      case "double"
+        ## Do nothing, data is already of type double
+      case "single"
+        data = single (data);
       case "int16"
         data = int16 (data * (2.0 ^ 15));
       case "int8"
         data = int8 (data * (2.0 ^ 7));
       case "uint8"
         data = uint8 ((data + 1.0) * 0.5 * (2.0 ^ 8 - 1));
+      otherwise
+        error ('@audiorecorder/getaudiodata: invalid DATATYPE "%s"', datatype)
     endswitch
   endif
 
   if (get (recorder, "NumberOfChannels") == 2)
-    data = data';
+    data = data.';
   else
-    data = data(1,:)';
+    data = data(1,:).';
   endif
 
 endfunction
+
+
+## Tests of audiorecorder must not actually record anything.
+%!testif HAVE_PORTAUDIO; audiodevinfo (1) > 0
+%! recorder = audiorecorder (44100, 16, 2);
+%! data = getaudiodata (recorder);
+%! assert (isa (data, "double"));
+%! data = getaudiodata (recorder, "double");
+%! assert (isa (data, "double"));
+%! data = getaudiodata (recorder, "single");
+%! assert (isa (data, "single"));
+%! data = getaudiodata (recorder, "int16");
+%! assert (isa (data, "int16"));
+%! data = getaudiodata (recorder, "int8");
+%! assert (isa (data, "int8"));
+%! data = getaudiodata (recorder, "uint8");
+%! assert (isa (data, "uint8"));
+%! assert (size (data)(1), recorder.TotalSamples);
+%! assert (size (data)(2), 2);
+
+%!testif HAVE_PORTAUDIO; audiodevinfo (1) > 0
+%! recorder = audiorecorder (44100, 8, 1);
+%! data = getaudiodata (recorder);
+%! assert (size (data)(1), recorder.TotalSamples);
+%! assert (size (data)(2), 1);
+
+## Test input validation
+%!testif HAVE_PORTAUDIO; audiodevinfo (1) > 0
+%! recorder = audiorecorder (44100, 16, 2);
+%! fail ("getaudiodata (recorder, 'foobar')", "invalid DATATYPE");
--- a/scripts/audio/@audiorecorder/getplayer.m	Wed Dec 29 18:17:53 2021 -0800
+++ b/scripts/audio/@audiorecorder/getplayer.m	Thu Dec 30 11:20:10 2021 -0800
@@ -30,15 +30,20 @@
 ## @seealso{@audioplayer/audioplayer, @audiorecorder/audiorecorder}
 ## @end deftypefn
 
-function player = getplayer (varargin)
+function player = getplayer (recorder)
 
-  if (nargin < 1 || nargin > 2)
-    print_usage ();
-  endif
-
-  recorder = varargin{1};
   data = getaudiodata (recorder);
-  player = audioplayer (data, get (recorder, "SampleRate"),
+  player = audioplayer (data,
+                        get (recorder, "SampleRate"),
                         get (recorder, "BitsPerSample"));
 
 endfunction
+
+
+## FIXME: Uncomment when audioplayer constructor supports null data
+%!#testif HAVE_PORTAUDIO; audiodevinfo (1) > 0
+%!# recorder = audiorecorder (44100, 16, 2);
+%!# player = getplayer (recorder);
+%!# assert (isa (player, "audioplayer"));
+%!# assert (player.SampleRate, 44100);
+%!# assert (player.BitsPerSample, 16);
--- a/scripts/audio/@audiorecorder/isrecording.m	Wed Dec 29 18:17:53 2021 -0800
+++ b/scripts/audio/@audiorecorder/isrecording.m	Thu Dec 30 11:20:10 2021 -0800
@@ -32,10 +32,11 @@
 
 function tf = isrecording (recorder)
 
-  if (nargin < 1)
-    print_usage ();
-  endif
-
   tf = __recorder_isrecording__ (struct (recorder).recorder);
 
 endfunction
+
+
+%!testif HAVE_PORTAUDIO; audiodevinfo (1) > 0
+%! recorder = audiorecorder (44100, 16, 2);
+%! assert (isrecording (recorder), false);
--- a/scripts/audio/@audiorecorder/pause.m	Wed Dec 29 18:17:53 2021 -0800
+++ b/scripts/audio/@audiorecorder/pause.m	Thu Dec 30 11:20:10 2021 -0800
@@ -32,10 +32,10 @@
 
 function pause (recorder)
 
-  if (nargin < 1)
-    print_usage ();
-  endif
-
   __recorder_pause__ (struct (recorder).recorder);
 
 endfunction
+
+
+## No tests possible for this function
+%!assert (1)
--- a/scripts/audio/@audiorecorder/play.m	Wed Dec 29 18:17:53 2021 -0800
+++ b/scripts/audio/@audiorecorder/play.m	Thu Dec 30 11:20:10 2021 -0800
@@ -39,20 +39,21 @@
 ## @audiorecorder/audiorecorder}
 ## @end deftypefn
 
-function player = play (varargin)
+function player = play (recorder, start)
 
-  if (nargin < 1 || nargin > 2)
-    print_usage ();
-  endif
+  data = getaudiodata (recorder);
+  player = audioplayer (data,
+                        get (recorder, "SampleRate"),
+                        get (recorder, "BitsPerSample"));
 
-  recorder = varargin{1};
-  data = getaudiodata (recorder);
-  player = audioplayer (data, get (recorder, "SampleRate"),
-                        get (recorder, "BitsPerSample"));
   if (nargin == 1)
     play (player);
   else
-    play (player, varargin{2});
+    play (player, start);
   endif
 
 endfunction
+
+
+## No tests possible for this function
+%!assert (1)
--- a/scripts/audio/@audiorecorder/record.m	Wed Dec 29 18:17:53 2021 -0800
+++ b/scripts/audio/@audiorecorder/record.m	Thu Dec 30 11:20:10 2021 -0800
@@ -34,12 +34,18 @@
 ## @seealso{@audiorecorder/recordblocking, @audiorecorder/audiorecorder}
 ## @end deftypefn
 
-function record (varargin)
+function record (recorder, length)
+
+  hrecorder = struct (recorder).recorder;
 
-  if (nargin < 1 || nargin > 2)
-    print_usage ();
+  if (nargin == 1)
+    __recorder_record__ (hrecorder);
+  else
+    __recorder_record__ (hrecorder, length);
   endif
 
-  __recorder_record__ (struct (varargin{1}).recorder, varargin{2:end});
+endfunction
+
 
-endfunction
+## No tests possible for this function
+%!assert (1)
--- a/scripts/audio/@audiorecorder/recordblocking.m	Wed Dec 29 18:17:53 2021 -0800
+++ b/scripts/audio/@audiorecorder/recordblocking.m	Thu Dec 30 11:20:10 2021 -0800
@@ -31,12 +31,16 @@
 ## @seealso{@audiorecorder/record, @audiorecorder/audiorecorder}
 ## @end deftypefn
 
-function recordblocking (varargin)
+function recordblocking (recorder, length)
 
   if (nargin != 2)
     print_usage ();
   endif
 
-  __recorder_recordblocking__ (struct (varargin{1}).recorder, varargin{2});
+  __recorder_recordblocking__ (struct (recorder).recorder, length);
 
 endfunction
+
+
+## No tests possible for this function
+%!assert (1)
--- a/scripts/audio/@audiorecorder/resume.m	Wed Dec 29 18:17:53 2021 -0800
+++ b/scripts/audio/@audiorecorder/resume.m	Thu Dec 30 11:20:10 2021 -0800
@@ -32,10 +32,10 @@
 
 function resume (recorder)
 
-  if (nargin < 1)
-    print_usage ();
-  endif
-
   __recorder_resume__ (struct (recorder).recorder);
 
 endfunction
+
+
+## No tests possible for this function
+%!assert (1)
--- a/scripts/audio/@audiorecorder/set.m	Wed Dec 29 18:17:53 2021 -0800
+++ b/scripts/audio/@audiorecorder/set.m	Thu Dec 30 11:20:10 2021 -0800
@@ -38,31 +38,31 @@
 ## @seealso{@audiorecorder/get, @audiorecorder/audiorecorder}
 ## @end deftypefn
 
-function properties = set (varargin)
+function properties = set (recorder, varargin)
 
-  if (nargin < 1 || nargin > 3)
+  if (nargin > 3)
     print_usage ();
   endif
 
-  recorder = struct (varargin{1}).recorder;
+  hrecorder = struct (recorder).recorder;
 
   if (nargin == 1)
-    properties.SampleRate = {};
-    properties.Tag = {};
-    properties.UserData = {};
+    properties = struct ("SampleRate", {{}}, "Tag", {{}}, "UserData", {{}});
+
   elseif (nargin == 2)
-    for [value, property] = varargin{2}
-      setproperty (recorder, property, value);
+    for [value, property] = varargin{1}
+      setproperty (hrecorder, property, value);
     endfor
+
   elseif (nargin == 3)
-    if (iscell (varargin{2}))
+    if (iscell (varargin{1}))
       index = 1;
-      for property = varargin{2}
-        setproperty (recorder, char (property), varargin{3}{index});
+      for property = varargin{1}
+        setproperty (hrecorder, char (property), varargin{2}{index});
         index += 1;
       endfor
     else
-      setproperty (recorder, varargin{2}, varargin{3});
+      setproperty (hrecorder, varargin{1}, varargin{2});
     endif
   endif
 
@@ -70,15 +70,52 @@
 
 function setproperty (recorder, property, value)
 
-  switch (property)
-    case "SampleRate"
+  switch (lower (property))
+    case "samplerate"
       __recorder_set_fs__ (recorder, value);
-    case "Tag"
+    case "tag"
       __recorder_set_tag__ (recorder, value);
-    case "UserData"
+    case "userdata"
       __recorder_set_userdata__ (recorder, value);
     otherwise
-      error ("@audiorecorder/set: no such property or the property specified is read-only");
+      error ('@audiorecorder/set: "%s" is not a valid property name or is read-only', property);
   endswitch
 
 endfunction
+
+
+## Tests of audiorecorder must not actually record anything.
+%!testif HAVE_PORTAUDIO; audiodevinfo (1) > 0
+%! recorder = audiorecorder ();
+%! set (recorder, "SampleRate", 8800);
+%! set (recorder, "Tag", "mytag");
+%! ## Also test case insensitivity
+%! set (recorder, "USERdata", [1, 2; 3, 4]);
+%! assert (recorder.SampleRate, 8800);
+%! assert (recorder.Tag, "mytag");
+%! assert (recorder.UserData, [1, 2; 3, 4]);
+
+%!testif HAVE_PORTAUDIO; audiodevinfo (1) > 0
+%! recorder = audiorecorder ();
+%! set (recorder, {"SampleRate", "Tag", "UserData"},
+%!                {8800, "mytag", [1, 2; 3, 4]});
+%! assert (recorder.SampleRate, 8800);
+%! assert (recorder.Tag, "mytag");
+%! assert (recorder.UserData, [1, 2; 3, 4]);
+
+%!testif HAVE_PORTAUDIO; audiodevinfo (1) > 0
+%! recorder = audiorecorder ();
+%! props = set (recorder);
+%! props.SampleRate = 8800;
+%! props.Tag = "mytag";
+%! props.UserData = [1, 2; 3, 4];
+%! set (recorder, props);
+%! assert (recorder.SampleRate, 8800);
+%! assert (recorder.Tag, "mytag");
+%! assert (recorder.UserData, [1, 2; 3, 4]);
+
+## Test input validation
+%!testif HAVE_PORTAUDIO; audiodevinfo (1) > 0
+%! recorder = audiorecorder ();
+%! fail ('set (recorder, "foobar", 1)', "not a valid property name"); 
+%! fail ('set (recorder, "Running", 1)', "is read-only"); 
--- a/scripts/audio/@audiorecorder/stop.m	Wed Dec 29 18:17:53 2021 -0800
+++ b/scripts/audio/@audiorecorder/stop.m	Thu Dec 30 11:20:10 2021 -0800
@@ -33,10 +33,10 @@
 
 function stop (recorder)
 
-  if (nargin < 1)
-    print_usage ();
-  endif
-
   __recorder_stop__ (struct (recorder).recorder);
 
 endfunction
+
+
+## No tests possible for this function
+%!assert (1)
--- a/scripts/audio/@audiorecorder/subsasgn.m	Wed Dec 29 18:17:53 2021 -0800
+++ b/scripts/audio/@audiorecorder/subsasgn.m	Thu Dec 30 11:20:10 2021 -0800
@@ -31,7 +31,7 @@
 ## @seealso{@audiorecorder/audiorecorder}
 ## @end deftypefn
 
-function value = subsasgn (recorder, idx, rhs)
+function recorder = subsasgn (recorder, idx, rhs)
 
   if (nargin != 3)
     print_usage ();
@@ -44,9 +44,20 @@
   if (strcmp (idx(1).type, "."))
     field = idx.subs;
     set (recorder, field, rhs);
-    value = recorder;
   else
     error ("@audiorecorder/subsasgn: invalid subscript type");
   endif
 
 endfunction
+
+
+%!testif HAVE_PORTAUDIO; audiodevinfo (1) > 0
+%! recorder = audiorecorder (44100, 16, 2);
+%! recorder.Tag = "mytag";
+%! assert (get (recorder, "Tag"), "mytag");
+
+## Test input validation
+%!testif HAVE_PORTAUDIO; audiodevinfo (1) > 0
+%! recorder = audiorecorder (44100, 16, 2);
+%! fail ("recorder(1).Tag = 5", "invalid subscript type");
+%! fail ("recorder{1}.Tag = 5", "invalid subscript type");
--- a/scripts/audio/@audiorecorder/subsref.m	Wed Dec 29 18:17:53 2021 -0800
+++ b/scripts/audio/@audiorecorder/subsref.m	Thu Dec 30 11:20:10 2021 -0800
@@ -49,3 +49,15 @@
   endif
 
 endfunction
+
+
+%!testif HAVE_PORTAUDIO; audiodevinfo (1) > 0
+%! recorder = audiorecorder (44100, 16, 2);
+%! set (recorder, "Tag", "mytag");
+%! assert (recorder.Tag, "mytag");
+
+## Test input validation
+%!testif HAVE_PORTAUDIO; audiodevinfo (1) > 0
+%! recorder = audiorecorder (44100, 16, 2);
+%! fail ("recorder(1).Tag", "invalid subscript type");
+%! fail ("recorder{1}.Tag", "invalid subscript type");