• 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 KamitoriGrass · Aug 05, 2014 at 02:41 AM · javascriptwav

translating a audio exporting script from c# to javascript

Excuse me, I'm trying to translate the script in the link: http://forum.unity3d.com/threads/exporting-audio.157693/

or as below:

 static public void SaveAudioClipToWav(AudioClip audioClip, string filename)
 {
     FileStream fsWrite = File.Open(filename, FileMode.Create);
  
     BinaryWriter bw = new BinaryWriter(fsWrite);
  
     Byte[] header = { 82, 73, 70, 70, 22, 10, 4, 0, 87, 65, 86, 69, 102, 109, 116, 32 };
     bw.Write(header);
  
     Byte[] header2 = { 16, 0, 0, 0, 1, 0, 1, 0, 68, 172, 0, 0, 136, 88, 1, 0 };
     bw.Write(header2);
  
     Byte[] header3 = { 2, 0, 16, 0, 100, 97, 116, 97, 152, 9, 4, 0 };
     bw.Write(header3);
  
     float[] samples = new float[audioClip.samples];
     audioClip.GetData(samples, 0);
     int i = 0;
  
     while (i < audioClip.samples)
     {
         int sampleInt = (int)(32000.0 * samples[i++]);
  
         int msb = sampleInt / 256;
         int lsb = sampleInt - (msb * 256);
  
         bw.Write((Byte)lsb);
         bw.Write((Byte)msb);
     }
  
     fsWrite.Close();
  
 }

It's a script help me to save a audioclip as a audio file. I've actually translated it to js like this:

 function SaveAudioClipToWav(clip:AudioClip)
 {
     if(clip == null)
     {
         
     }
     else
     {
         var fsWrite:FileStream = new FileStream("test.wav", FileMode.Create, FileAccess.ReadWrite);    
            var bw :BinaryWriter = new BinaryWriter(fsWrite);
  
         var intHeader: int[] = [82, 73, 70, 70, 22, 10, 4, 0, 87, 65, 86, 69, 102, 109, 116, 32];
         var intHeader2: int[] = [16, 0, 0, 0, 1, 0, 1, 0, 68, 172, 0, 0, 136, 88, 1, 0];
         var intHeader3: int[] = [2, 0, 16, 0, 100, 97, 116, 97, 152, 9, 4, 0 ];
     
         var header = new byte[16];
         var header2 = new byte[16];
         var header3 = new byte[12];
     
         for(var j:int=0;j<intHeader.Length;j++)
         {
             header[j] = intHeader[j];
         }
         for(var k:int=0;k<intHeader2.Length;k++)
         {
             header2[k] = intHeader2[k];
         }
         for(var l:int=0;l<intHeader3.Length;l++)
         {
             header3[l] = intHeader3[l];
         }
         bw.Write(header);
         bw.Write(header2);
         bw.Write(header3);
 
         var samples:float[] = new float[clip.samples];
 
         clip.GetData(samples, 0);
 
         var i:int = 0;
 
         while (i < clip.samples)
         {
             var sampleInt:int = (32000.0 * samples[i++]);
      
             var msb:int = sampleInt / 256;
             var lsb:int = sampleInt - (msb * 256);
          
             bw.Write(lsb);
             bw.Write(msb);
         
         }
      
     fsWrite.Close();
 
     }
 }

There is no any errors and it can export a "test.wav" file. However, the file always turns out to be the same file with some noise instead of the sound I record. I can sure the audioclip as parameter is not empty.

Could anyone please teach me how to correct the problem? Thank you very much!

p.s.

I found it will recording the sound of my clicking mouse at first...

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

0 Replies

· Add your reply
  • Sort: 

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

2 People are following this question.

avatar image avatar image

Related Questions

How I can prevent music from stopping after changing a scene? 3 Answers

Setting Scroll View Width GUILayout 1 Answer

Can someone help me fix my Javascript for Flickering Light? 6 Answers

how do you make an object rotate on ios with your finger 3 Answers

Read objects like in Penumbra 1 Answer


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