Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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
2
Question by It_is_hot_in_vegas · Oct 08, 2017 at 12:22 AM · c#script.sound

Is it possible to create sound with scripting?

I would like to know if it is possible create sound (a small "bleep" is good enough for now.) using only scripts (like how video games from the '70s/'80s/'90s did.), if so, how would it be scripted (C-sharp only!)?;

Yes, I am aware you can import sound files (.wav, .mp3, .ogg, .midi, etc.) from your computer into Unity, but I want to experiment with this and try to create a little song using only scripting.

If you know (or have some ideas on) how this could be made (possible), please let me know. Thank you.

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

4 Replies

· Add your reply
  • Sort: 
avatar image
7

Answer by Bunny83 · Oct 08, 2017 at 01:18 AM

Yes you can. You just need to create PCM samples yourself. So you would create an AudioClip as well as a float array of the desired length. Keep in mind that the usual sample frequency is 44 kHz (44000 samples per second).

Note that stereo sounds have two channels which are alternating. So the first value in the array belongs to the first channel, the second value to the second channel. The third value to the first channel and so on.

Once you have your array setup you just call SetData and pass the float array to the AudioClip.

You can use the trigonometric functions from Mathf to create a sine wave.

 int sampleFreq = 44000;
 float frequency = 440;
 
 float[] samples = new float[44000];
 for(int i = 0; i < samples.Length; i++)
 {
     samples[i] = Mathf.Sin(Mathf.PI*2*i*frequency/sampleFreq);
 }
 AudioClip ac = AudioClip.Create("Test", samples.Length, 1, sampleFreq, false);
 ac.SetData(samples, 0);

This creates a mono (single channel) audio clip of 1 second length with a beep sound of 440 Hz.

Comment
Add comment · Show 2 · 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 Bunny83 · Oct 08, 2017 at 01:24 AM 0
Share

Note that the hardest part would be to get smooth transitions between different frequencies. Sudden changes in frequency or phase would result in an annoying "click" sound. It's important to have a continuous signal (so no sudden jumps in the amplitude or jumps in the derivative of the signal). Even than you usually would need a transition section between two notes if you want to play them right next to each other.

avatar image Bunny83 · Oct 08, 2017 at 01:44 AM 0
Share

Of course you're not limited to sine waves. You can also create rectangle, triangle or sawtooth waves.

 // rectangle
 for(int i = 0; i < samples.Length; i++)
     samples[i] = ($$anonymous$$athf.Repeat(i*frequency/sampleFreq,1) > 0.5f)?1f:-1f;
 
 // sawtooth
 for(int i = 0; i < samples.Length; i++)
     samples[i] = $$anonymous$$athf.Repeat(i*frequency/sampleFreq,1)*2f - 1f;
 
 // triangle
 for(int i = 0; i < samples.Length; i++)
     samples[i] = $$anonymous$$athf.PingPong(i*2f*frequency/sampleFreq,1)*2f - 1f;

(ps: written from scratch and not tested.)

avatar image
1

Answer by PatrickPerron · May 22, 2019 at 03:52 PM

Hi, if you just want to add a Beep sound with a frequency and duration on a Windows system, I'm using this code (will try to find a way to do the same in Ubuntu):

Add your using entry:

 using System.Runtime.InteropServices;

Then in your main class, add this:

 [DllImport("kernel32.dll", SetLastError = true)]
 static extern bool Beep(uint dwFreq, uint dwDuration);
 
 static void PlayBeep(uint iFrequency, uint iDuration)
 {
     Beep(iFrequency, iDuration);
 }

You can then just call this in your code:

 PlayBeep(4000, 100);

This would play a beep sound at frequency 4000 for 100ms... I tested this code with a Windows 10 x64 machine and Unity 2018.3.11f.

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 Harinezumi · Jun 01, 2020 at 03:40 PM 0
Share

This will only work on Windows systems as you are importing the OS specific kernel32.dll. Bunny83's answer, although more complicated, is a generic solution.

avatar image
1

Answer by asashour · Jun 24, 2020 at 01:15 PM

The example in AudioClip.Create works.

You need to have ExampleClass attached to a GameObject, which also has an empty Audio Source component.

 using UnityEngine;
 using System.Collections;
 
 public class ExampleClass : MonoBehaviour
 {
     public int position = 0;
     public int samplerate = 44100;
     public float frequency = 440;
 
     void Start()
     {
         AudioClip myClip = AudioClip.Create("MySinusoid", samplerate * 2, 1, samplerate, true, OnAudioRead, OnAudioSetPosition);
         AudioSource aud = GetComponent<AudioSource>();
         aud.clip = myClip;
         aud.Play();
     }
 
     void OnAudioRead(float[] data)
     {
         int count = 0;
         while (count < data.Length)
         {
             data[count] = Mathf.Sin(2 * Mathf.PI * frequency * position / samplerate);
             position++;
             count++;
         }
     }
 
     void OnAudioSetPosition(int newPosition)
     {
         position = newPosition;
     }
 }
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 PaulRdy · Sep 05, 2018 at 12:21 PM

As of September 2018 (Unity 2018.2) this isn't working. Is there another possiblity to do this? AudioClip.Create has been marked as obsolete but I can't find a replacement.

Comment
Add comment · Show 6 · 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 myzzie · Sep 05, 2018 at 01:40 PM 0
Share

https://docs.unity3d.com/ScriptReference/AudioClip.Create.html

avatar image Harinezumi · Sep 05, 2018 at 02:16 PM 0
Share

To expand on myzzie's comment, you can see that there are newer versions of the mentioned functions, with somewhat different signatures. As far as I can see, the only differences is that they removed the bool _3D parameter.

Also, next time please create a new question and link to this one. One shouldn't post a new question as an answer.

avatar image PaulRdy · Sep 05, 2018 at 02:21 PM 0
Share

I tried the top most overload (pretty much using the exact example provided on the page, but without custom callbacks but SetData ins$$anonymous$$d). Didn't work.

 int sampleRate = 44100;
 float[] samples  = new float[44100];
 
 clip = AudioClip.Create("test", sampleRate, 1, sampleRate, false);
 for (int i = 0; i < samples.Length; i++)
 {
  samples[i] = $$anonymous$$athf.Sin($$anonymous$$athf.PI * 2 * 440 * i * / sampleRate);
 }
 
 clip.SetData(samples, 0);
 source.PlayOneShot(clip, 1.0f);


Edit: Also the example provided by Bunny. So you're saying I need to use the custom callbacks?

avatar image Harinezumi PaulRdy · Sep 05, 2018 at 02:40 PM 0
Share

I don't think the custom callbacks are needed, they are just to update the sound data and its position in 3D.
Your code seems to be correct in general, but I have a feeling that the problem is that samples is a local variable that goes out of scope, and I'm not sure AudioClip.SetData() makes a copy of it. $$anonymous$$aking the samples into a member variable would solve that.

BTW, what do you mean when you say "it doesn't work"? Does it make any noise, or none at all? Do you get any errors in the console?

avatar image PaulRdy Harinezumi · Sep 05, 2018 at 02:56 PM 0
Share

$$anonymous$$akes no noise and I don't get any errors. But the method is definitely getting called.

I also tried making the samples array a class member as you suggested. Unfortunately that didn't fix anything either.

Also the VS auto complete says that the AudioClip.Create overload I'm using is obsolete, contrary to what's said in the documentation. This is not a feature I desperately need so it's not terrible but yeah... I'm on Unity 2018.2.1f1 btw.

Edit:

just did:

 if (i % 40 == 0)
 Debug.Log(samples[i]);

samples array seems to be correct. So it just seems that array get's lost somehow. But it's in a $$anonymous$$onoBehaviour class and the function is being called via a trigger in the Update method to exclude any threading issues. Still doesn't work.

Show more comments

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

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

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

How to make sounds louder if I go faster 1 Answer

How can i set audio mixer groups volume to be used with ui slider when 0 is lower and 80 louder ? 1 Answer

How to stop sound on trigger exit? 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