Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by ecret · Mar 12, 2013 at 06:35 PM · audiomicrophonerecord

Unity Microphone downsampling audio

Hi, I am trying to downsample audio that comes from using Microphone.Start and OnAudioFilterRead.

It seems to always record in 44100hz stereo but I need 16000hz mono. I am able to merge the stereo channels to mono fairly easily but downsampling is harder. I tried using c# lib's like alvas but they do not work on unity due to other dependancies. I am likely going to need to write a low pass filter and then interpolate/decimate the audio to get my clean downsampled audio.

I was hoping if there is a easier way to do this in unity. I am attaching my code so far. It was inspired by some existing code I found on the unity forums.

 using UnityEngine;
 using System.Collections;
 using System.IO;
 using System.Collections.Generic;
 using System;
 public class LiveRec : MonoBehaviour {
     FileStream file;
     int microphoneMaxRecordingTime;
     
     void Start () 
     {
         StartWriting();
         microphoneMaxRecordingTime=1;
         audio.clip = Microphone.Start (null, true, microphoneMaxRecordingTime, AudioSettings.outputSampleRate);
          audio.loop = true;
         while (!(Microphone.GetPosition(null) > 0)) 
         {
             Debug.Log ("Microphone.GetPosition(null)");
         }
         audio.Play();
     }
      void OnAudioFilterRead(float[] data, int channels)
     {
         Debug.Log("channels=" + channels.ToString());    
         Debug.Log("floats=" + data.Length);    
         ConvertAndWrite(data);
         Array.Clear(data, 0, data.Length);
     }
     void ConvertAndWrite(float[] dataSource)
     {
 
         Int16[] intData = new Int16[dataSource.Length]; 
         //converting in 2 steps : float[] to Int16[], //then Int16[] to Byte[]
         Byte[] bytesData  = new Byte[dataSource.Length]; 
         //bytesData array is twice the size of 
         //dataSource array because a float converted in Int16 is 2 bytes.
         int rescaleFactor = 32767; //to convert float to Int16    
         
         for (int i = 0; i<dataSource.Length/2;i++)
         {
             //intData[i] = (short)(dataSource[i]*rescaleFactor);
             Int16 leftChannel = (short)(dataSource[i*2]*rescaleFactor);
             Int16 rightChannel = (short)(dataSource[i*2+1]*rescaleFactor);
             Byte[] byteArr = new Byte[2];
             short monoChannel = (short)Math.Round(((int)leftChannel + (int)rightChannel) / 2F);
             //byteArr = BitConverter.GetBytes(intData[i]);
             byteArr = BitConverter.GetBytes(monoChannel);
             byteArr.CopyTo(bytesData,i*2);
         }

         Debug.Log("bytes="+bytesData.Length.ToString());
         file.Write(bytesData,0,bytesData.Length); 
     }    
 
     //Create and start writing the file
     void StartWriting()
     {
         string filePath = Application.dataPath + "/zz.raw";
         file = new FileStream(filePath,FileMode.Create);    //Create or open our file if exists
     }
 
 }
 
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by gregzo · Mar 12, 2013 at 07:39 PM

Hi Elias,

Very inteesting question, which raises many different issues:

1) Getting Microphone data

No need to use OnAudioFilterRead there! As you've noticed, it will return data that contains the number of channels your project is set to, 2 in most cases. Plus, OnAudioFilterRead's data is post spatialisation, i.e. data from a gameObject far away from the scene's listener will be less loud. What you need is to grab the float[] directly from the audioclip Microphone.Start creates for you, through AudioClip.GetData(float[] data, int offset).

In your code, you are setting up Microphone.Start to create an audioclip of length microphoneMaxRecordingTime. This is a waste of ram, as Unity will allocate that clip however long it is. Better to setup a short microphoneClip (2 seconds), and to set it to loop. Simply grab the data in Update, being mindful of the special case when the clip loops. You'll grab mono data, and won't need to bounce OnAudioFilterRead stereo data to mono as you're doing here, which by the way incurrs a strong risk of saturating.

2) Frequency

Microphone.GetDeviceCaps allows you to check which recording frequencies are available for a particular recording device (Microphone.devices). If yu need to record at 16000 hz, and your mic allows it, no need to resample ! In line 14 of your script, you ask for a rec frequency equal to AudioSettings.outputSampleRate. This does not need to be the case.

If the mic doesn't support 16000 hz recording, then you'll have to record first, and resample after using a plugin. Can I ask why you'd want to do that? If the only reason is to save space, make sure through Microphone.GetDeviceCaps that the available mics don't support low frequencies first.

There's a lot to digest here, do let me know how you're faring!

Gregzo

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by willemsenzo · Dec 26, 2013 at 05:05 AM

Doesn't this work?

void Start { AudioSettings.outputSampleRate = 16000; //Rest of your code }

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image ranayahya87 · Feb 24, 2014 at 06:24 AM 0
Share

Hi, i want to make talking tom cat in unity. i used this script for this purpose.

var audioSource : AudioSource;

var power : float;

private var samples : float[];

private var audioClip : AudioClip;

function Start()

{

 Screen.sleepTimeout = SleepTimeout.NeverSleep;

 

 audioClip = $$anonymous$$icrophone.Start("", true, 1, 44100);

 

 audioSource.clip = AudioClip.Create("", 44100, 1, 44100, false, false);

 

 audioSource.Play();

 

 samples = new float[audioClip.samples * audioClip.channels];

}

function Update()

{

 audioClip.GetData(samples, 0);

 

 //if you want to do something with the voice, use this

 for (var i = 0; i < samples.length; i++)

 {

     samples[i] *= power; //for example this line will make the volume higher or lower (adjust the power value to change it)

 }

 

 audioSource.clip.SetData(samples, 0);

}

when i run it on my computer. it starts repeating after 2 seconds, it does not wait me to complete my long sentence. when run this same code on device. it repeats again and again and this repeating sound converts to noise. please help me. Thanks.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

14 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Guys i need help with audio! 0 Answers

will unity record a stereo microphone as two channels? 0 Answers

How to record audio in chunks and send through websocket? 0 Answers

Analyze Frequency from Microphone Jack only 0 Answers

Playing SuperCollider realtime through audio sources 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges