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 Skeletim · Jul 23, 2013 at 03:36 AM · audioaudiosourcesync

Syncing Audio with GetOutputData

     using UnityEngine;
     using System.Collections;
     using System.Collections.Generic;
     using System;
     using System.IO;
     
     public class example : MonoBehaviour {
     
         AudioClip recordedAudioClip;
         float[] blockBuffer;
         List<float> clipBuffer = new List<float>();
         bool isRecording = false;
         int size = 680;
             
         void Start() {
             GameObject.Find("audiosource").audio.GetOutputData(blockBuffer,0);
         }
         
         void Update() {
             
             if((Input.GetKeyDown("r")) && (isRecording == false)) {
                 Debug.Log ("Start Recording");
                 isRecording = true;
                 
                 clipBuffer.Clear();
                 blockBuffer = new float[size];
                 recordedAudioClip = AudioClip.Create("output",441000,1,44100,false,false);
                }
             else if((Input.GetKeyDown("r")) && (isRecording == true)) {
                 Debug.Log ("Stop Recording");
                 isRecording = false;
                 recordedAudioClip.SetData(clipBuffer.ToArray(),0);
             }
             
             // Play Audio
             if(Input.GetKeyDown("p")) {
                 Debug.Log ("Play Recording");
                 AudioSource.PlayClipAtPoint(recordedAudioClip, Camera.main.transform.position);
             }
             
             // Increase size of sample buffer
             if(Input.GetKeyDown("]")) {
                 size=size+10;
                 Debug.Log (size);
             }
             
             if(Input.GetKeyDown("[")) {
                 size=size-10;
                 Debug.Log (size);
             }
             
             if(Input.GetKeyDown(".")) {
                 size=size+1;
                 Debug.Log (size);
             }
             
             if(Input.GetKeyDown(",")) {
                 size=size-1;
                 Debug.Log (size);
             }
             
             if(Input.GetKeyDown("=")) {
                 size=size+100;
                 Debug.Log (size);
             }
             
             if(Input.GetKeyDown("-")) {
                 size=size-100;
                 Debug.Log (size);
             }
     
             if (isRecording) {               
                 GameObject.Find("audiosource").audio.GetOutputData(blockBuffer,0);
                clipBuffer.AddRange(blockBuffer);
             }
         }
 }

I don't have any error checking on this code, it's only for testing my concept. Please take a look.

The code takes short samples of a particular audio clip and is suppose to stream it to a buffer until a person stops it, and then play that audio clip. I know nothing about audio processing, but I can't figure how to adjust the buffer sample to pull the right amount of audio. Right not it's static-laced. Too big of an array and it's slow. To small of an array it's fast. And based on the fact that this is called every frame, it's also dependent on how fast or slow someone's computer is. I also assume it's based on the outputSampleRate

Go ahead and try it out. Create a scene with an audio source called 'audiosource' and add some random clip to it. Attach this script to the camera for example. I would like the audio that is recorded to sound EXACTLY like the audio from the audio source, but I don't know how much access I'm granted to the sound buffer.

I can't use OnAudioFilterRead() because this isn't performed on audio that can be heard, but audio that can not be heard. (muted audio source)

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by Skeletim · Jul 24, 2013 at 02:52 AM

I'm documenting this for others.

Based on the code above GetOutputData executed every frame. The issue was that it didn't need to. It only needed to execute after each sample. So I had to get the time of the sample, which is the sample size / frequency of audio clip.

That gives you the length of the audio gathered from the sample. So a sample size of 8192 (32k) from an audio clip that is at 44100hz, is 8192 / 44100 = 0.185759637 or ~18ms

So you'll want to get the next sample after 18ms has passed. Since this is done in my code every frame, I just had to use Time.deltaTime and added up Time.deltaTime every frame, and check that against the time it takes to play the clip. If it exceeds that, then I grab another another sample, if not, then I pass it up.

Granted the math isn't perfect, and I don't know if it ever will be, so you'll still get small pops and cracks, but at least for my purpose, this is perfect, and shouldn't be noticeable.

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

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

15 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 avatar image

Related Questions

Sample-precise sync on multiple audio sources ? 1 Answer

How to make two audio tracks play in sync? 2 Answers

Particle System + Audio Source sync? (specifically sub effect) 0 Answers

Is there a way to create a random Audiosource loop? 2 Answers

Music Visualiser Troubles 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