Тёмный

Unity3D analog style Synthesizer Tutorial 

Dano Kablamo
Подписаться 523
Просмотров 26 тыс.
50% 1

video (c) 2016 Prismic Studios LLC
Check out my game on Steam!
store.steampowe...
some code from: www.develop-onl...

Опубликовано:

 

12 сен 2024

Поделиться:

Ссылка:

Скачать:

Готовим ссылку...

Добавить в:

Мой плейлист
Посмотреть позже
Комментарии : 86   
@mattl7599
@mattl7599 7 лет назад
Such a great tutorial. Your way of teaching is really clear and concise. You should look into doing a full unity tutorial series from beginner to advanced.
@AdamMelvins
@AdamMelvins Год назад
I'm here in 2022 - this is by far the best tutorial I've seen about audio generation, every darn line of code still works in Unity 2021 and I learned a truckload! :) I wish to add random beeps to my game now, and wish I had seen this when I was working on my game projects at uni! :) Thank you for making this video and sharing so much information :)
@batman3698
@batman3698 2 года назад
i think you wanna use phase -= Mathf.PI*2 instead of reseting to 0 to get a smoother sine wave. Nice tutorial however. This opened my eyes to making my own synth
@TheVonWeasel
@TheVonWeasel Год назад
If anyone is getting a glitchy sound instead of a smooth sine wave: change the code at 3:50 from "phase = 0.0" to "phase -= Mathf.PI * 2" That way it maintains a smooth wave
@brandondorey7830
@brandondorey7830 7 лет назад
This is a wonderful, simple tutorial for something I have always been curious about! Thank you so much!
@DanoKablamo
@DanoKablamo 7 лет назад
Thanks! I hope you make some cool stuff with it.
@vincristine7035
@vincristine7035 2 года назад
I need to come back and watch this whole thing when I have more time. Man this was fascinating to actually find a guide for.
@massimomagrini4452
@massimomagrini4452 5 лет назад
if (phase > Mathf.PI*2) phase = phase - Mathf.PI*2;
@JorgePereira-ne6ie
@JorgePereira-ne6ie 6 лет назад
Awesome! Thank you a lot. It's not what I was looking for, but it's good to find some non basic unity tutorials.
@TimBuhrs
@TimBuhrs 3 года назад
Fantastic tutorial! I would love to see a second tutorial or possibly the project files on git to further study and find out how the synth works exactly! Thank you!
@triplestandart7613
@triplestandart7613 2 года назад
Wow I've wanted to do something like this for so long! Thank you so much. I'll try and implement FM...Let's see how this goes :D
@DaveHoskinsCG
@DaveHoskinsCG 7 лет назад
Why are you resetting the phase? Plus, you should be wrapping it properly by subtracting 2*PI from the value instead if making it zero.
@DanoKablamo
@DanoKablamo 7 лет назад
That's a good idea!
@attilahchristanov4603
@attilahchristanov4603 5 лет назад
it sounds much better
@Temophor
@Temophor 6 лет назад
That was awesome, thank you! I'm gonna play around with all sorts of sound creation in unity now :)
@MuseumOfReality
@MuseumOfReality 5 лет назад
Great tutorial, thanks! It really helped me understand OnAudioFilterRead a little better and I'll definitely be using that knowledge in my own project :)
@thebricktop
@thebricktop 5 лет назад
is there a way to send the generated audio to the unity audio mixer? or add effects or 3d attenuation? edit: ok it is, thats awesome.
@Crisisdarkness
@Crisisdarkness 6 лет назад
Wow man this is really awesome, thank you
@nikharrison
@nikharrison 4 года назад
Thanks Dano for this very concise tutorial!
@Beatsbasteln
@Beatsbasteln 4 года назад
hey man. cool video! i'm currently trying to code a script that enabled me to process every sample of an audio source differently. do you have a video or can you tell me how to go about that?
@See.history.24
@See.history.24 3 года назад
Beautiful
@guyclegg
@guyclegg 5 лет назад
How would I make it not monophonic?
@valia
@valia 5 лет назад
Very informative video. Thanks for the great work!
@veaarthur3655
@veaarthur3655 7 лет назад
Fantastic! Please make more like this!
@polherverolland
@polherverolland 6 лет назад
Great tutoriel however the résult sound like you can hear small scratch noise all the time, why? What is the problem with sound engine of unity
@mkgamesartvisuals
@mkgamesartvisuals 3 года назад
Wonderful, thanks!
@douglasvanhoffen8453
@douglasvanhoffen8453 4 года назад
Super great tutorial! I have tried extending it so one game object can play multiple notes by adding the sin waves for each note (i.e. wave[i] += (the maths bit) in a loop through all active frequencies) , but this sounds pretty bad. What's the best way to add 2 or more sound waves together?
@shimster13
@shimster13 5 лет назад
Thank you for this tutorial! It's the best of the best!
@chevn9260
@chevn9260 2 года назад
Thank you, nice
@nahid3003
@nahid3003 3 года назад
good job
@jwlee9791
@jwlee9791 7 лет назад
This is awesome!! Thank you!
@SPTX.
@SPTX. 6 лет назад
Is it possible to use OnAudioFilterRead to change tempo? Any idea how to do it? I want to make a music's tempo proportional to an event without messing with the pitch but I don't know shit about audio and it's difficult to find documentation on that matter, nevermind accessible documentation.
@willia_music
@willia_music 5 лет назад
Great Tutorial
@guillermo4730
@guillermo4730 Год назад
Great tutorial! I have a question. If i want to make a piano where you can press 2 or more notes at same time, is necessary create another oscillator to make the other sound?
@wakeupxav
@wakeupxav 9 месяцев назад
me too... can anyone help?
@md.abdullahal-mamun5936
@md.abdullahal-mamun5936 3 года назад
Great
@AbdurRahim-oh2lp
@AbdurRahim-oh2lp 3 года назад
Nice work
@coma_flotante
@coma_flotante 3 года назад
you have 440 suscribers rn, coincidence? I THINK NOT!
@MrZzm88
@MrZzm88 3 года назад
I don't understand what you were talking about until I move the frequency bar.
@rumyhumy
@rumyhumy 2 года назад
That's super cooooool!!!!! 1! 1! 1
@CertifiedDoc
@CertifiedDoc Год назад
Do you have a solution to the popping sound when you press and release the spacebar? You can hear it right around 6:30, and I get exactly the same artifacting when I run the code myself.
@CertifiedDoc
@CertifiedDoc Год назад
I found a solution. Popping is caused by sudden, sharp changes to the gain. Going from 0 to 0.1 gain in one sample has the chance to create a sharp ridge in the waveform, which is very likely to be audible. I solved this issue by attenuating the change in gain over a few milliseconds. Smoothing the volume in this way removes the artifacts from the samples without perceptably change the sound.
@ginodp
@ginodp 3 года назад
thank you thank you thank you!!!
@mfl9903
@mfl9903 4 года назад
Can one create a polyphony synth using the same approach? If not, how to create one in Unity3D? Thanks for the great tutorial!
@vvortex157
@vvortex157 4 года назад
how to use it? make a notes with different length for examaple?
@DavidBoura
@DavidBoura 3 года назад
Please make more!
@MarionetteKazuko
@MarionetteKazuko 7 лет назад
Very nice video
@nks98968
@nks98968 5 лет назад
Hey , In this example, Are you playing external synthesizer ? As I am also trying something like this. Can we produce and playback same sound inside unity3d that is played in any external synthesizer. Thanks in Advance.
@nikharrison
@nikharrison 4 года назад
You could use Keijiro's MIDI Jack if you wanted to use a MIDI keyboard to play the synthesizer (or anything else) in Unity > github.com/keijiro/MidiJack
@prest0n755
@prest0n755 4 года назад
I'v actually met you at a game dev convention once didn't realize this was you until I seen Gnomes and Fairies
@chevn9260
@chevn9260 2 года назад
How to generate a triangle waveform?
@walney2008
@walney2008 3 года назад
hello, on this site (www.visipiano.com/midi-to-json-converter/) I can convert a midi file to json, how can I see the lyrics of the song and their durations to play them? at unity? has how to convert midi to json directly by unity?
@joshuanewth9707
@joshuanewth9707 5 лет назад
Can you do an overview of the different waveforms you generate?
@finnliry3839
@finnliry3839 3 года назад
So. How would you do a saw wave?
@Palavence
@Palavence 7 лет назад
Awesome tutorial, first of all. I'm getting an error though. When I had to try incrementing the frequency on every Spacebar usage, on " frequency = frequencies[thisFreq];" I got this error: "IndexOutOfRangeException: Array index is out of range." My array frequencies is clearly not working . May you help me?
@Palavence
@Palavence 7 лет назад
np solved
@Palavence
@Palavence 7 лет назад
will you do a tutorial for the graphics, though?
@DanoKablamo
@DanoKablamo 7 лет назад
Maybe, but for now put this in your scene: using UnityEngine; using System.Collections; public class AudioDataReader : MonoBehaviour { public static AudioDataReader reader; //AudioListener audioS; float[] spectrum; float[] spectrumDbL; float[] spectrumDbR; public float refValue = 0.1f; public float dbL; public float dbR; public float maxV = 0; public int maxN = 0; public float threshold = 0.02f; public float deg = 0.3515625f; public float fSample = 0f; public double pitchValue; private float[] freqData; private int nSamples = 1024; private float fMax; public LineRenderer lr; public bool circle; float height; public int lrLength = 1024; void Awake() { reader = this; } void Start() { spectrum = new float[lrLength]; spectrumDbL = new float[lrLength]; spectrumDbR = new float[lrLength]; freqData = new float[nSamples]; fMax = AudioSettings.outputSampleRate / 2; fSample = AudioSettings.outputSampleRate; } public static void SphericalToCartesian(float radius, float polar, float elevation, out Vector3 outCart) { float a = radius * Mathf.Cos(elevation); outCart.x = a * Mathf.Cos(polar); outCart.y = radius * Mathf.Sin(elevation); outCart.z = a * Mathf.Sin(polar); } public static void CartesianToSpherical(Vector3 cartCoords, out float outRadius, out float outPolar, out float outElevation) { if (cartCoords.x == 0) cartCoords.x = Mathf.Epsilon; outRadius = Mathf.Sqrt((cartCoords.x * cartCoords.x) + (cartCoords.y * cartCoords.y) + (cartCoords.z * cartCoords.z)); outPolar = Mathf.Atan(cartCoords.z / cartCoords.x); if (cartCoords.x < 0) outPolar += Mathf.PI; outElevation = Mathf.Asin(cartCoords.y / outRadius); } void Update() { spectrum = AudioListener.GetSpectrumData(lrLength, 0, FFTWindow.BlackmanHarris); spectrumDbL = AudioListener.GetOutputData(lrLength, 0); spectrumDbR = AudioListener.GetOutputData(lrLength, 1); int i = 1; while (i < lrLength - 1) { if (!circle) { Vector3 pos = new Vector3(2, (Mathf.Log(spectrum[i - 1])) + 10, i - 1 - 512); lr.SetPosition(i - 1, new Vector3(2, Mathf.Log(spectrum[i - 1]) + 10, (i - 1) * 2 - 50f)); //lr.SetPosition(i - 1, pos); } if (circle) { Vector3 pos = Vector3.zero; height = Mathf.Lerp(height, (deg * dbL), Time.deltaTime * 0.007f); pos.x = Mathf.Log(spectrum[i - 1]) * Mathf.Cos((float)(i - 1) * height); pos.y = Mathf.Log(spectrum[i - 1]) * Mathf.Sin((float)(i - 1) * height); pos.z = 0; lr.SetPosition(i - 1, new Vector3(i - 1, Mathf.Log(spectrum[i - 1]) + 10, 2)); lr.SetPosition(i - 1, pos); // Vector3 p = Vector3.zero; // float a = (i / 1023) * (2f * Mathf.PI); // float r = Mathf.Log(spectrum[i - 1]) + 50f; // p.x = p.x + r * Mathf.Cos( a ); // p.y = p.y + r * Mathf.Sin( a ); // lr.SetPosition(i - 1, p); } // Debug.DrawLine(new Vector3(i - 1, spectrum[i] + 10, 0), new Vector3(i, spectrum[i + 1] + 10, 0), Color.red); // Debug.DrawLine(new Vector3(i - 1, Mathf.Log(spectrum[i - 1]) + 10, 2), new Vector3(i, Mathf.Log(spectrum[i]) + 10, 2), Color.cyan); // Debug.DrawLine(new Vector3(Mathf.Log(i - 1), spectrum[i - 1] - 10, 1), new Vector3(Mathf.Log(i), spectrum[i] - 10, 1), Color.green); // Debug.DrawLine(new Vector3(Mathf.Log(i - 1), Mathf.Log(spectrum[i - 1]), 3), new Vector3(Mathf.Log(i), Mathf.Log(spectrum[i]), 3), Color.yellow); i++; } // int i = 1; // while (i < 1023) { // Vector3 pos = new Vector3(i - 1, (Mathf.Log(spectrum[i - 1])) + 10, 2); // //pos = Vector3.Slerp(pos); // lr.SetPosition(i - 1, pos); // //Vector3 pos = new Vector3(i - 1, (Mathf.Log(spectrum[i - 1])) + 10, 2); // //lr.SetPosition(i - 1, new Vector3(i - 1, (Mathf.Log(spectrum[i - 1])) + 10, 2)); // // Debug.DrawLine(new Vector3(i - 1, spectrum[i] + 10, 0), new Vector3(i, spectrum[i + 1] + 10, 0), Color.red); // Debug.DrawLine(new Vector3(i - 1, Mathf.Log(spectrum[i - 1]) + 10, 2), new Vector3(i, Mathf.Log(spectrum[i]) + 10, 2), Color.cyan); // Debug.DrawLine(new Vector3(Mathf.Log(i - 1), spectrum[i - 1] - 10, 1), new Vector3(Mathf.Log(i), spectrum[i] - 10, 1), Color.green); // Debug.DrawLine(new Vector3(Mathf.Log(i - 1), Mathf.Log(spectrum[i - 1]), 3), new Vector3(Mathf.Log(i), Mathf.Log(spectrum[i]), 3), Color.yellow); // i++; // } int d = 1; float sumL = 0; float sumR = 0; while (d < lrLength - 1) { sumL += spectrumDbL[d] * spectrumDbL[d]; sumR += spectrumDbR[d] * spectrumDbR[d]; d++; } float quotientL = sumL / lrLength; float quotientR = sumR / lrLength; float rmsL = Mathf.Sqrt(quotientL); float rmsR = Mathf.Sqrt(quotientR); dbL = 20 * Mathf.Log10(rmsL / refValue); if (dbL < -160) dbL = -160; dbR = 20 * Mathf.Log10(rmsR / refValue); if (dbR < -160) dbR = -160; for (i = 0; i < lrLength - 1; i++) { if (spectrum[i] > maxV && spectrum[i] > threshold) { maxV = spectrum[i]; maxN = i; } } double freqN = maxN; if (maxN > 0 && maxN < lrLength - 1) { float dL = spectrum[maxN - 1] / spectrum[maxN]; float dR = spectrum[maxN + 1] / spectrum[maxN]; freqN += 0.5 * (dR * dR - dL * dL); } pitchValue = freqN * AudioSettings.outputSampleRate / lrLength; //Debug.Log (pitchValue + " hz"); } public float BandVol(float fLow, float fHigh, int channel) { fLow = Mathf.Clamp(fLow, 20, fMax); // limit low... fHigh = Mathf.Clamp(fHigh, fLow, fMax); // and high frequencies AudioListener.GetSpectrumData(freqData, channel, FFTWindow.BlackmanHarris); int n1 = (int)Mathf.Floor(fLow * nSamples / fMax); int n2 = (int)Mathf.Floor(fHigh * nSamples / fMax); float sum = 0; for (var i = n1; i transform.localScale.y) { rend.material.SetColor("_EmissionColor", thisColor); transform.localScale = new Vector3(targetScale.x, level * multiplier, targetScale.z); } else { rend.material.SetColor("_EmissionColor", Color.Lerp(rend.material.GetColor("_EmissionColor"), Color.black, Time.deltaTime * responseSpeed)); setScale = new Vector3(targetScale.x, level * multiplier, targetScale.z); transform.localScale = Vector3.Lerp(transform.localScale, setScale, Time.deltaTime * responseSpeed); } } }
@kivancgumus4066
@kivancgumus4066 7 лет назад
Hey man great video! Gotta ask a question though. After I have built this I wanted to add buttons that played some beat loops in the background for me to jam with it, but it seems that OnAudioFilterRead blocks any audio that is not from this synthesizer. Glad to hear some help:):):):)
@DanoKablamo
@DanoKablamo 7 лет назад
Kivanc Gumus did you make sure your sounds are playing on a different audio source than the synth?
@kivancgumus4066
@kivancgumus4066 7 лет назад
Yeah figured it out man... Thanks for the quick response and good luck with your fantastic work :)))
@quinny8544
@quinny8544 4 года назад
How could I go about turning each sound into its own clip once its played?
@CertifiedDoc
@CertifiedDoc Год назад
The data parameter in OnAudioFilterRead is a buffer of individual raw audio samples. You could write that buffer to a WAV file every time the function is called, keeping in mind that samples for each channel are interlaced.
@sabrango
@sabrango 4 года назад
thx!
@bulalaish
@bulalaish 7 лет назад
wow
@andreadipietro8888
@andreadipietro8888 6 лет назад
I'm using your sine waveform formula to create triangle, square and other waveforms by adding every sinusoid in the data[i] = ... expression, but it doesn't come as clear as your triangle formula, should I just add more sinusoids?
@DanoKablamo
@DanoKablamo 6 лет назад
I used Mathf.pingpong for triangle instead of mathf.sin.
@andreadipietro8888
@andreadipietro8888 6 лет назад
Dano Kablamo I know, I can't do that though because I want different sound sources to play at the same time, and that resulted in a buggy mess where the second one wouldn't play
@DanoKablamo
@DanoKablamo 6 лет назад
Oh You need to use a second tone generator to have two tones at once..
@andreadipietro8888
@andreadipietro8888 6 лет назад
Dano Kablamo that's the problem! the second one isn't playing any sound at all! I'm pretty sure it receives input too, it just doesn't play anything (i am using vive headset to create a vr analog synth, maybe it has an audio listener somewhere bugging it all?)
@DanoKablamo
@DanoKablamo 6 лет назад
I can't remember how I did it in this tutorial, but if you are using AudioListener.OnAudioFilterRead, switch that to AudioSource.OnAudioFilterRead or something like it to use multiple at once.
@anastasia9963
@anastasia9963 7 лет назад
thank you
@jilmerjohncariaso6825
@jilmerjohncariaso6825 5 лет назад
What is the formula for piano sound wave?
@douglasvanhoffen8453
@douglasvanhoffen8453 4 года назад
Real instruments are pretty tricky, they're sounds are made up of multiple different pitched tones alongside the main tone, as well as other differences in the shape of the wave caused by how the instrument behaves in the real world.
@mamurny
@mamurny 7 лет назад
How did you visualize the waves?
@DanoKablamo
@DanoKablamo 7 лет назад
Magic! Jk it's a line renderer and you use OnAudioFilterRead, except you don't change the data, you read it. It's complicated AF. I'll make a tutorial for it too.
@mamurny
@mamurny 7 лет назад
I would be thankful for that. Here is a sub. Thx
@tonynacho
@tonynacho 4 года назад
Hey @@DanoKablamo Did you ever make a tutorial for the line renderer?
@AlexTornado
@AlexTornado 3 года назад
@Antony Nasce Check out a demo scene in ProBuilder that visualizes soundwaves.
@prest0n755
@prest0n755 4 года назад
When we met you almost drank rubbing alcohol while we were talking if you can remember that.
@Bleyedpe
@Bleyedpe 4 года назад
u are my new god
@Massive-3D
@Massive-3D 3 года назад
One freakin downvote?!
@ivanandreev1958
@ivanandreev1958 4 года назад
Thank you!
Далее
C# Creating an Audio Synthesizer - Part 1
30:30
Просмотров 38 тыс.
Code-It-Yourself! Sound Synthesizer #1 - Basic Noises
28:50
Making Procedural Music in Unity
10:39
Просмотров 30 тыс.
Unity3D How To: Audio Visualizer With Spectrum Data
13:11
Audio Helm for Unity - Native Synth Tutorial
15:20
Просмотров 15 тыс.
This one mistake is killing my game
14:02
Просмотров 189 тыс.
An introduction to Shader Art Coding
22:40
Просмотров 971 тыс.
Watching Neural Networks Learn
25:28
Просмотров 1,3 млн