主要内容

start

Start playing and/or recording

Since R2026a

    Description

    start(as) starts playing and/or recording for the audiostreamer object, as. The behavior of start depends on the operating mode of as and whether audio is already streaming. For more information, see Algorithms.

    start(as,Name=Value) specifies options using one or more name-value arguments.

    example

    Examples

    collapse all

    Create an audiostreamer object with the "full-duplex" mode. Specify a sample rate of 96 kHz.

    Fs = 96e3;
    as = audiostreamer("full-duplex",SampleRate=Fs);

    Create three sinusoids of different frequencies. Sample each sinusoid for 2 seconds.

    t = 1:1/Fs:2-1/Fs;
    s1 = sin(pi*2*1e3*t');
    s2 = sin(pi*2*2e3*t');
    s3 = sin(pi*2*3e3*t');

    Write the sinusoids to the playback queue. Because you did not call start first, playing remains paused.

    write(as,s1)
    write(as,s2)
    write(as,s3)

    Play the samples. To only start playing, specify the "player" mode.

    start(as,Mode="player")

    As a best practice, release the audio device.

    release(as)

    Create an audiostreamer object with one recording channel. Specify the mode "recorder". The default sample rate is 44.1 kHz.

    as = audiostreamer("recorder",RecorderChannels=1);

    Start the recording.

    start(as)

    Get the recording. The recording length is in samples.

    x = read(as,2*44100);

    Stop the recording.

    stop(as)
    length(x)
    ans = 
    88200
    

    You can also record a predetermined length of audio using the start object function.

    as2 = audiostreamer("full-duplex",RecorderChannels=1);
    start(as2,SamplesToRecord=44100,Mode="recorder");
    pause(3)
    y = read(as2);
    stop(as2)
    length(y)
    ans = 
    44100
    

    As a best practice, release the audio devices.

    release(as)
    release(as2)

    Input Arguments

    collapse all

    The audiostreamer object to use for playing or recording.

    Name-Value Arguments

    collapse all

    Specify optional pairs of arguments as Name1=Value1,...,NameN=ValueN, where Name is the argument name and Value is the corresponding value. Name-value arguments must appear after other arguments, but the order of the pairs does not matter.

    Example: SamplesToRecord=1024,Mode="recorder"

    The mode to start, specified as "player", "recorder", or "both". If the operating mode of the audiostreamer object is "full-duplex", the default value of Mode is "both". Otherwise, the default value is identical to the operating mode.

    Specifying a mode is useful when the operating mode of the audiostreamer object is "full-duplex" and you want to only start playing or recording.

    Number of samples to record, specified as a positive integer. This name-value argument is valid only when the operating mode of as is either "full-duplex" or "recorder".

    Data Types: single | double

    Tips

    These pseudocode examples illustrate best practice when using the audiostreamer object functions start and write in different workflows.

    • Read and write in full-duplex mode.

      as = audiostreamer("full-duplex");
      
      start(as) % start rec., enable play
      
      for ii = 1:100
          x = read(as,1024); % blocks
          y = process(x);
          write(as,y);
      end
      
      stop(as)
      
    • Pre-buffer for measurement sequences.

      as = audiostreamer("full-duplex");
      swLatency = sweeptone(0.5,2);
      swMeasure = sweeptone(5,2);
      N1 = length(swLatency);
      N2 = length(swMeasure);
      
      % avoid creating temporary vector
      write(as,swLatency)
      write(as,swMeasure)
      write(as,swMeasure)
      start(as)
      
      recLatency = read(as,N1);
      recMeasure = read(as,N2*2);
      
      plot(impzest(x,recMeasure))
    • Play audio from file stream without blocking.

      as = audiostreamer;
      afr = dsp.AudioFileReader;
      
      while ~afr.isDone
          write(as,afr()); % pre-buffer
      end
      
      start(as)
      
    • Play audio without blocking (one shot).

      as = audiostreamer;
      
      % Pre-fill to avoid blocking
      write(as,x) % fills the output queue
      start(as)   % or play
      
    • Record a predetermined length of audio.

      as = audiostreamer("recorder",…
      RecorderChannels=1);
      
      % Start the recording (in samples)
      start(as,RecordingLength=5*44100)
      
      % Get the recording (N samples)
      x = read(as);
      

    Algorithms

    The behavior of start depends on the mode and whether audio is already streaming (because of calls to play, record, or a prior call to start).

    ModeBehavior
    "player"

    The stream is enabled and playback is un-paused/resumed.

    • If write is called before start: Pre-buffered audio starts playing.

    • If write is called after start: Audio plays immediately, after any audio that is already buffered.

    "recorder"

    • Starts recording indefinitely, unless you specify SamplesToRecord.

    • start throws a warning if already recording. The old recording is discarded.

    "both"

    • If streaming is paused or stopped, start synchronizes the start of recording and playback (of pre-buffered or callback audio).

    • If already playing but not recording, recording begins.

    • If already recording but not playing, playback begins.

    • If recording and playing have already started, there is no effect.

    Version History

    Introduced in R2026a

    See Also

    Objects

    Functions