How to read the data from AudioClip using PCMReaderCallback when the former is created by Microphone.Start in...












1















How can I change the code below to use the PCMReaderCallback to read data from the audio clip created by Microphone.Start?



using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class NewBehaviourScript : MonoBehaviour
{
private string m_deviceName = null;
private AudioClip m_audioClip;

void Start()
{
bool loop = true;
int lengthSec = 10;
int frequency = 44100;

m_audioClip = Microphone.Start(m_deviceName, loop, lengthSec, frequency);
}

void Stop()
{
Microphone.End(m_deviceName);
m_audioClip = null;
}
}









share|improve this question





























    1















    How can I change the code below to use the PCMReaderCallback to read data from the audio clip created by Microphone.Start?



    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;

    public class NewBehaviourScript : MonoBehaviour
    {
    private string m_deviceName = null;
    private AudioClip m_audioClip;

    void Start()
    {
    bool loop = true;
    int lengthSec = 10;
    int frequency = 44100;

    m_audioClip = Microphone.Start(m_deviceName, loop, lengthSec, frequency);
    }

    void Stop()
    {
    Microphone.End(m_deviceName);
    m_audioClip = null;
    }
    }









    share|improve this question



























      1












      1








      1








      How can I change the code below to use the PCMReaderCallback to read data from the audio clip created by Microphone.Start?



      using System.Collections;
      using System.Collections.Generic;
      using UnityEngine;

      public class NewBehaviourScript : MonoBehaviour
      {
      private string m_deviceName = null;
      private AudioClip m_audioClip;

      void Start()
      {
      bool loop = true;
      int lengthSec = 10;
      int frequency = 44100;

      m_audioClip = Microphone.Start(m_deviceName, loop, lengthSec, frequency);
      }

      void Stop()
      {
      Microphone.End(m_deviceName);
      m_audioClip = null;
      }
      }









      share|improve this question
















      How can I change the code below to use the PCMReaderCallback to read data from the audio clip created by Microphone.Start?



      using System.Collections;
      using System.Collections.Generic;
      using UnityEngine;

      public class NewBehaviourScript : MonoBehaviour
      {
      private string m_deviceName = null;
      private AudioClip m_audioClip;

      void Start()
      {
      bool loop = true;
      int lengthSec = 10;
      int frequency = 44100;

      m_audioClip = Microphone.Start(m_deviceName, loop, lengthSec, frequency);
      }

      void Stop()
      {
      Microphone.End(m_deviceName);
      m_audioClip = null;
      }
      }






      c# unity3d audio






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 19 '18 at 17:22









      Programmer

      76.2k1086151




      76.2k1086151










      asked Nov 19 '18 at 14:36









      Adriano Di GiovanniAdriano Di Giovanni

      407519




      407519
























          1 Answer
          1






          active

          oldest

          votes


















          1














          You cannot use the Microphone API with PCMReaderCallback. This is because PCMReaderCallback is registered by passing the PCMReaderCallback function to the AudioClip.Create function which returns a new AudioClip. Unity's Microphone.Start function returns AudioClip but it doesn't take PCMReaderCallback as argument.



          If this is not a Microphone but a simple audio clip then you could with the example below:



          public int lengthSamples = 400;
          public int channels = 2;
          public int frequency = 16000;

          AudioSource adSource;

          void Start()
          {
          adSource = GetComponent<AudioSource>();
          AudioClip clip = AudioClip.Create("clip", lengthSamples, channels, frequency, true, OnAudioRead);
          adSource.clip = clip;
          adSource.Play();
          }

          //PCMReaderCallback callback
          void OnAudioRead(float data)
          {

          }




          If you just want to read the audio data from the Mic, you can still use OnAudioFilterRead or the AudioSource.clip.GetData function. Below is an example for OnAudioFilterRead.



          string m_deviceName = null;
          AudioSource adSource;

          void Start()
          {
          adSource = GetComponent<AudioSource>();
          bool loop = true;
          int lengthSec = 10;
          int frequency = 44100;

          AudioClip clip = Microphone.Start(m_deviceName, loop, lengthSec, frequency);
          adSource.clip = clip;
          while (!(Microphone.GetPosition(null) > 0)) { }

          adSource.Play();
          }

          void OnAudioFilterRead(float data, int channels)
          {

          }





          share|improve this answer
























          • Thanks for sharing. Two more questions. Why while (!(Microphone.GetPosition(null) > 0)) { }? How to avoid the microphone input to be played back?

            – Adriano Di Giovanni
            Nov 19 '18 at 17:36











          • Without while (!(Microphone.GetPosition(null) > 0)) there can be a bug in your code. For example, trying to access the data from mic may return 0. This is a problem I see on this site all the time so you have to wait until the pos is >1. You may not run into it but many that will use your app will. Also that can cause audioclip to not even play at-all. For your second question, see my other answer.

            – Programmer
            Nov 19 '18 at 17:40











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53376891%2fhow-to-read-the-data-from-audioclip-using-pcmreadercallback-when-the-former-is-c%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          1














          You cannot use the Microphone API with PCMReaderCallback. This is because PCMReaderCallback is registered by passing the PCMReaderCallback function to the AudioClip.Create function which returns a new AudioClip. Unity's Microphone.Start function returns AudioClip but it doesn't take PCMReaderCallback as argument.



          If this is not a Microphone but a simple audio clip then you could with the example below:



          public int lengthSamples = 400;
          public int channels = 2;
          public int frequency = 16000;

          AudioSource adSource;

          void Start()
          {
          adSource = GetComponent<AudioSource>();
          AudioClip clip = AudioClip.Create("clip", lengthSamples, channels, frequency, true, OnAudioRead);
          adSource.clip = clip;
          adSource.Play();
          }

          //PCMReaderCallback callback
          void OnAudioRead(float data)
          {

          }




          If you just want to read the audio data from the Mic, you can still use OnAudioFilterRead or the AudioSource.clip.GetData function. Below is an example for OnAudioFilterRead.



          string m_deviceName = null;
          AudioSource adSource;

          void Start()
          {
          adSource = GetComponent<AudioSource>();
          bool loop = true;
          int lengthSec = 10;
          int frequency = 44100;

          AudioClip clip = Microphone.Start(m_deviceName, loop, lengthSec, frequency);
          adSource.clip = clip;
          while (!(Microphone.GetPosition(null) > 0)) { }

          adSource.Play();
          }

          void OnAudioFilterRead(float data, int channels)
          {

          }





          share|improve this answer
























          • Thanks for sharing. Two more questions. Why while (!(Microphone.GetPosition(null) > 0)) { }? How to avoid the microphone input to be played back?

            – Adriano Di Giovanni
            Nov 19 '18 at 17:36











          • Without while (!(Microphone.GetPosition(null) > 0)) there can be a bug in your code. For example, trying to access the data from mic may return 0. This is a problem I see on this site all the time so you have to wait until the pos is >1. You may not run into it but many that will use your app will. Also that can cause audioclip to not even play at-all. For your second question, see my other answer.

            – Programmer
            Nov 19 '18 at 17:40
















          1














          You cannot use the Microphone API with PCMReaderCallback. This is because PCMReaderCallback is registered by passing the PCMReaderCallback function to the AudioClip.Create function which returns a new AudioClip. Unity's Microphone.Start function returns AudioClip but it doesn't take PCMReaderCallback as argument.



          If this is not a Microphone but a simple audio clip then you could with the example below:



          public int lengthSamples = 400;
          public int channels = 2;
          public int frequency = 16000;

          AudioSource adSource;

          void Start()
          {
          adSource = GetComponent<AudioSource>();
          AudioClip clip = AudioClip.Create("clip", lengthSamples, channels, frequency, true, OnAudioRead);
          adSource.clip = clip;
          adSource.Play();
          }

          //PCMReaderCallback callback
          void OnAudioRead(float data)
          {

          }




          If you just want to read the audio data from the Mic, you can still use OnAudioFilterRead or the AudioSource.clip.GetData function. Below is an example for OnAudioFilterRead.



          string m_deviceName = null;
          AudioSource adSource;

          void Start()
          {
          adSource = GetComponent<AudioSource>();
          bool loop = true;
          int lengthSec = 10;
          int frequency = 44100;

          AudioClip clip = Microphone.Start(m_deviceName, loop, lengthSec, frequency);
          adSource.clip = clip;
          while (!(Microphone.GetPosition(null) > 0)) { }

          adSource.Play();
          }

          void OnAudioFilterRead(float data, int channels)
          {

          }





          share|improve this answer
























          • Thanks for sharing. Two more questions. Why while (!(Microphone.GetPosition(null) > 0)) { }? How to avoid the microphone input to be played back?

            – Adriano Di Giovanni
            Nov 19 '18 at 17:36











          • Without while (!(Microphone.GetPosition(null) > 0)) there can be a bug in your code. For example, trying to access the data from mic may return 0. This is a problem I see on this site all the time so you have to wait until the pos is >1. You may not run into it but many that will use your app will. Also that can cause audioclip to not even play at-all. For your second question, see my other answer.

            – Programmer
            Nov 19 '18 at 17:40














          1












          1








          1







          You cannot use the Microphone API with PCMReaderCallback. This is because PCMReaderCallback is registered by passing the PCMReaderCallback function to the AudioClip.Create function which returns a new AudioClip. Unity's Microphone.Start function returns AudioClip but it doesn't take PCMReaderCallback as argument.



          If this is not a Microphone but a simple audio clip then you could with the example below:



          public int lengthSamples = 400;
          public int channels = 2;
          public int frequency = 16000;

          AudioSource adSource;

          void Start()
          {
          adSource = GetComponent<AudioSource>();
          AudioClip clip = AudioClip.Create("clip", lengthSamples, channels, frequency, true, OnAudioRead);
          adSource.clip = clip;
          adSource.Play();
          }

          //PCMReaderCallback callback
          void OnAudioRead(float data)
          {

          }




          If you just want to read the audio data from the Mic, you can still use OnAudioFilterRead or the AudioSource.clip.GetData function. Below is an example for OnAudioFilterRead.



          string m_deviceName = null;
          AudioSource adSource;

          void Start()
          {
          adSource = GetComponent<AudioSource>();
          bool loop = true;
          int lengthSec = 10;
          int frequency = 44100;

          AudioClip clip = Microphone.Start(m_deviceName, loop, lengthSec, frequency);
          adSource.clip = clip;
          while (!(Microphone.GetPosition(null) > 0)) { }

          adSource.Play();
          }

          void OnAudioFilterRead(float data, int channels)
          {

          }





          share|improve this answer













          You cannot use the Microphone API with PCMReaderCallback. This is because PCMReaderCallback is registered by passing the PCMReaderCallback function to the AudioClip.Create function which returns a new AudioClip. Unity's Microphone.Start function returns AudioClip but it doesn't take PCMReaderCallback as argument.



          If this is not a Microphone but a simple audio clip then you could with the example below:



          public int lengthSamples = 400;
          public int channels = 2;
          public int frequency = 16000;

          AudioSource adSource;

          void Start()
          {
          adSource = GetComponent<AudioSource>();
          AudioClip clip = AudioClip.Create("clip", lengthSamples, channels, frequency, true, OnAudioRead);
          adSource.clip = clip;
          adSource.Play();
          }

          //PCMReaderCallback callback
          void OnAudioRead(float data)
          {

          }




          If you just want to read the audio data from the Mic, you can still use OnAudioFilterRead or the AudioSource.clip.GetData function. Below is an example for OnAudioFilterRead.



          string m_deviceName = null;
          AudioSource adSource;

          void Start()
          {
          adSource = GetComponent<AudioSource>();
          bool loop = true;
          int lengthSec = 10;
          int frequency = 44100;

          AudioClip clip = Microphone.Start(m_deviceName, loop, lengthSec, frequency);
          adSource.clip = clip;
          while (!(Microphone.GetPosition(null) > 0)) { }

          adSource.Play();
          }

          void OnAudioFilterRead(float data, int channels)
          {

          }






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 19 '18 at 17:19









          ProgrammerProgrammer

          76.2k1086151




          76.2k1086151













          • Thanks for sharing. Two more questions. Why while (!(Microphone.GetPosition(null) > 0)) { }? How to avoid the microphone input to be played back?

            – Adriano Di Giovanni
            Nov 19 '18 at 17:36











          • Without while (!(Microphone.GetPosition(null) > 0)) there can be a bug in your code. For example, trying to access the data from mic may return 0. This is a problem I see on this site all the time so you have to wait until the pos is >1. You may not run into it but many that will use your app will. Also that can cause audioclip to not even play at-all. For your second question, see my other answer.

            – Programmer
            Nov 19 '18 at 17:40



















          • Thanks for sharing. Two more questions. Why while (!(Microphone.GetPosition(null) > 0)) { }? How to avoid the microphone input to be played back?

            – Adriano Di Giovanni
            Nov 19 '18 at 17:36











          • Without while (!(Microphone.GetPosition(null) > 0)) there can be a bug in your code. For example, trying to access the data from mic may return 0. This is a problem I see on this site all the time so you have to wait until the pos is >1. You may not run into it but many that will use your app will. Also that can cause audioclip to not even play at-all. For your second question, see my other answer.

            – Programmer
            Nov 19 '18 at 17:40

















          Thanks for sharing. Two more questions. Why while (!(Microphone.GetPosition(null) > 0)) { }? How to avoid the microphone input to be played back?

          – Adriano Di Giovanni
          Nov 19 '18 at 17:36





          Thanks for sharing. Two more questions. Why while (!(Microphone.GetPosition(null) > 0)) { }? How to avoid the microphone input to be played back?

          – Adriano Di Giovanni
          Nov 19 '18 at 17:36













          Without while (!(Microphone.GetPosition(null) > 0)) there can be a bug in your code. For example, trying to access the data from mic may return 0. This is a problem I see on this site all the time so you have to wait until the pos is >1. You may not run into it but many that will use your app will. Also that can cause audioclip to not even play at-all. For your second question, see my other answer.

          – Programmer
          Nov 19 '18 at 17:40





          Without while (!(Microphone.GetPosition(null) > 0)) there can be a bug in your code. For example, trying to access the data from mic may return 0. This is a problem I see on this site all the time so you have to wait until the pos is >1. You may not run into it but many that will use your app will. Also that can cause audioclip to not even play at-all. For your second question, see my other answer.

          – Programmer
          Nov 19 '18 at 17:40


















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53376891%2fhow-to-read-the-data-from-audioclip-using-pcmreadercallback-when-the-former-is-c%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          How to change which sound is reproduced for terminal bell?

          Can I use Tabulator js library in my java Spring + Thymeleaf project?

          Title Spacing in Bjornstrup Chapter, Removing Chapter Number From Contents