How to Detect Users Microphone in Unity | Microphone Don't Scream Detection Tutorial

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello and welcome to today's video in today's video we're going to be working on a mechanic from a new game that's just come out called don't scream where the game detects your microphone input and if you scream or shout the game is over so in the mechanic here we can see that if I speak loud like this and our volume goes above this bar here a scream has been detected if we whisper if we whisper really quietly it doesn't get detected and it's when we shout it gets detected so full disclosure the kind of basis of this tutorial is based on this one here this was a 13 minute video um from a guy who mainly does VR tutorials in this and I use this just to figure out within Unity how you actually detect someone's microphone and also determine the loudness of it this kind of ends just being able to get like a loudness volume from it and it goes into showing you how in VR you could like use it to control mouth movement it's a really good video uh really concise um and then obviously I've took this and then adapted it further to get our little project here where we can choose a different microphone Source we can calibrate it we've got the slider and the detection so obviously all of that's custom but I just thought you know it'd be nice to acknowledge that uh some of the initial code even though I've tweaked it came from this video which I will link in the description below and for those of you who don't know don't scream is a game where if you scream you restart so if you scream you restart I don't real five short horror experience made by two Indie devs the challenge explore the forest for 18 minutes without screaming the catch time only ticks when you move is the perfect game to scare the pants off your friends and family and a microphone is required so this came out to great success one of the developers on Twitter has been talking about how much money it's made and how many wish lists it got and apparently it's done really well for them uh reviews are Fallen to mixed now and it's had some really good YouTuber pickup and support probably the biggest of which was Mark Player did a video on it as well so that's what don't scream is with that said let's get straight into programming the actual mechanics we're going to see here so I've just opened a new project in unity this is just a 2d project using the built-in render pipeline I just I just picked 2D core and we're using editor version 20 22. 3.11 F1 okay so the first thing is we need to be able to detect uh audio obviously from the microphone so the way we're going to do that is we're going to make a new game object and we're going to call this the audio loudness detector and to this we're going to add a component with the same name so audio loudness detector and we will open this up here so in this script we going to have one main method and then a couple of other bits and pieces in it but the main method is going to be a public float and it's going to be called get get loudness from audio clip and this is going to take in an integer called clip position as well as an audio clip and we can just call that clip so the first thing we need to do is we need to create a start position um and we can do that by doing uh int start position and this is equal to our clip position minus the sample window I'm actually going to make the sample window up here so we'll do public in Sample window and we'll set this just equal to 64 so what sample window is is if we have a clip position say 30 seconds into a clip we need to take a sample of data from before that clip position and we can add all of the loudness in that sample window together and then we can get a uh mean sort of clip loudness and then we'll work forward to our original clip position then next we want to check whether our start position is now less than zero because if it is there'll be an error because we'll be prior to the clip having started and there'll be nothing to sample from um so here we can just return zero um and the loudness will be zero because it's just there's no data to actually sample from so now we can make a float array and we can call this wave data and we can set it equal to a new float array and it'll have a size of sample window so we're going to get 64 in this case um floats in an array and then we can fill each of those floats with with a sample of data from the waveform and the way we do that is we go into our audio clip and we can use the get data function which is a built-in and you can see here it just says fills an array with sample data from the clip what we need to do here is we can take is this takes in our new array which we've just made so we can do wave data and then we have to pass in a uh offset samples and we can just use our start position for that so this is going to go from our start position and it'll work forward to our clip position and it'll fill this wave data float array with data from the clip then to this we can do uh float total loudness equals zero and then we want to do for each variable sample in wave data we can say total loudness is plus equal to MAF f. ABS sample so looking at a wave form you can see that you know you've got the central line which in this case will be zero and then anything above the line will go up to one and anything below the line will go to minus one and minus one doesn't mean like negative quietness obviously that's not a thing it's just part of the waveform data with the Peaks and troughs so we just need to get the absolute value here so if we are at minus one we'll actually add one to our total loudness and then we can just return total loudness divided by our samp Le window and this will give us the uh mean loudness as a float so to check this is working what we want to do is we want to go back into unity and now we can just create a 2d object and I'll use a Sprite and we can just use a circle for this I'll rename this to scale with loudness and to this I'm going to add a script called scale from audio clip so what we want to do here is depending on how loud the audio clip is and how loud we're speaking or shouting we're going to lurp between a minimum scale and a maximum scale so um to do that we need to get an audio source and this will be our source and that's so we can pass in the clip to the get loudness from audio clip fun that we've just made on our loudness detector we want a public Vector 3 and we're going to do um Min scale and we can do Max scale and we need a public audio loudness detector and we can just call that detector so that's a reference to this script here and then in update we can say float loudness is equal to detector. getet loudness from audio clip and then we're going to put in um source. time samples as our first uh as our first parameter which is clip position if we look at what time samples is we say it's the playback position in PCM samples here and it also needs the actual clip for from our source as well so we can do source. clip now with that we can uh lurp between the minimum scale and the maximum Scale based on this loudness so we can do transform. local scale equals Vector 3 dot lurp we do Min scale Max scale and loudness and we should be able to see that working so if we go into um our project here we need to add an audio source to this game object so we can drag that into audio source for our Min scale we could do 111 for our Max scale we could do 555 we need to drag in the audio loudness detector and now for audio Source here we need a clip so you can use any clip here that you want I've just used some finger snapping that I recorded in Adobe Audition I'll provide a link to this if you want to use this exact clip you'll be able to find it for free on my patreon um so I'll just import that now so this clip is just me clicking my fingers look various uh volumes so to scale with loudness we can drag in our finger snapping clip and that should work if we hit play based on the waveform of our finger snpp in this will scale up and down um according to how loud we are snapping you see that happening here if we click loop on our audio Source just so it keeps going round you see it's not going to the full uh 5x5 and that's because loudness gets presented uh quite as a as a quite small number this loudness it doesn't ever really go near uh one which is what if if it was one we'd be at Max scale at zero we're at Min scale anywhere in between that you know that's what a lurp is so we can multiply this by um sort of a sensitivity to make it a bigger number um basically boosting the loudness artificially um just for the purposes of the lurp so we can make a public float loudness sensibility variable we'll set that as 100f and then we can also make a threshold so public float threshold I'm going to set that equal to 1f now to sort of demonstrate this I'm going to do debug. log and we'll print out our loudness value we'll say if loudness is less than our threshold we'll set it to zero so it's not um being detected and jumping around even at small sort of thresholds you don't need this bit it's just a nice kind of little buffer so it's not picking up like weird stuff in the background and then we can actually just times this by our loudness Sensibility so we can see that working there uh that was scaling up and down quite nicely if I set this to Loop uh actually we'll have to come out play set that to Loop and we can play with these values so you can see them working more so we set our threshold to 0.5 it's not actually going to scale unless this number down here goes above 0.5 and that's based on how loud it is obviously and we can change the sort of sensibility to scale this number so if we just had that at one you can see it's 0.0 there's a lot of uh trailing or preceding zeros so we can just set that to 100 just to kind of give it a nice boost and we can see that going from one to five up here now when it's actually scaling okay so next we want to be able to actually take in our microphone audio and have it scaling based on our microphone not based on this finger snapping sound so what I'm going to do is I'm going to just move this over to the left and then duplicate it I'll move that one over to the right and I'll rename this actually to scale with clip and I'll rename this one to scale with microphone and we'll change it to a square just so we can see the bit of a difference in them and I'm going to get rid of these two scripts we don't need these now to this one so scale with microphone I'm going to create a new script called scale from microphone and we'll open this up and I'm going to copy from our scale from audio clip uh method going to get the interior of this and let's just copy it and paste it over into scale from microphone so this we don't need a audio source so the rest can be the same so instead of get loudness from audio clip we're going to go over to our audio loudness detector file and we're going to add um a bit more functionality to this which we can then go back to scale from microphone to implement so let's make a new um variable here and we'll call this public AUD this can be a private audio clip and we can call this microphone clip and then we can make a private void uh function and we can call this microphone to audio clip and this is going to take in uh an integer which we'll use use a little bit later on but we'll put it in now um called microphone index so the way microphones are stored in unity is we've got a microphone class and this has an array of devices which is just an array of strings and that's their names so what we can do here is do four each and let's say um name in microphones do devices debug.log name and if we just go over here let's actually comment out um this function so we can actually get it to compile because I need to show you uh what I mean by these devices so private start let's just call uh microphone to audio clip and we'll pass in zero for now as an index so if I go over to Unity and I'm actually going to just disable scale of clip for now if I hit play you can see here in our console we've got microphone Yeti classic which is the microphone that I'm recording on we've got microphone Logitech streamcam which is my plugged in uh USB uh webcam and then we've got steam streaming microphone as well so these are the microphone devices that I've got installed within Windows what you want to do is make a note of which microphone now just in this debug period while we're setting everything up make sure that so if you're using your webcam microphone but it's showing as like the third one in so if I wanted to use steam streaming microphone as the microphone for this test of when we try to get this working that'll be index two so we'll have 0o 1 and two um is our device IDs in the array and what I mean by that is if we type in here so string microone name is equal to microphone. devices and this has to um this is an array and we need to give it an index so if I want it to use my Yeti microphone I'll have to put in zero if I want it to use my webcam I'd have to put in one and if I want it to use the steam streaming microphone to detect uh me speaking I'd have to put in two so for me my Yeti microphone is the one I want to use I know from this that it's the first one in the array which is index zero so for the purposes of setting this up before we have this nice drop down CU remember we're going to eventually have a drop down here where we can pick which microphone we're actually using from uh the mold that are selected here I'm going to put microphone index that'll be the one that we're choosing from our devices and I'm hardcoding zero into our store store I'm hardcoding zero into our start function because that's the one I want to be the default I want it to be my Yeti microphone if you have if the microphone that you want to use again if the microphone that you want to use is in a different position then add that into uh that start function so if you wanted to use this one it's the second one in the list put one here but for me I want to use the first one which is my Yeti so I'm going to put zero now we've got our microphone name based on this index we are going to make a new clip and assign it to this microphone clip uh variable so we'll do microphone clip is equal to microphone dostart and this takes in the device name so it doesn't take in an index that's why we've had to get uh the name here so we can start our microphone name which for me again is the Blue Yeti then it takes in uh whether it should Loop and carry on looping your microphone so we want that to be true so we want it to always be uh listening out for our microphone we can set a um length in seconds you want a high number here so we'll do 20 uh this doesn't really matter because it's looping it'll just be like a rolling record but we just want 20 to give it enough um of a buffer so we can actually sample it and take loudness from it and then frequency is another integer and this is set in our audio settings in Project settings so we can just do audio settings dot output sample rate so again that's set within Unity you don't have to know what it is just put audio settings. output sample rate and I'm going to get rid of this for each Loop cuz we don't actually need it that was just for demonstration purposes and now I can make make a public float function called get loudness from microphone loudness from microphone so here we just want to return our get loudness from audio clip uh function and we need to pass in a clip position so we can get this from our microphone so we can do microphone. get position and this takes in a device name and we've got our device name here what we want to do is we actually want to change this and store this outside of um the function so we can come up here and do private string microphone name if we get rid of string here so now we're setting microphone name and good coding practices we should have a underscore before microphone name right so here we can pass in our microphone name and then the actual clip is our microphone clip so here we're turning our microphone into an audio clip it's going to be rolling record based on what we're saying to it and how loud we're talking we can then pass our microphone and the clip into the function that we made earlier which gets a loudness and now we are returning that in this new function called get loudness from microphone so if we go back to our scale for microphone class and then we uncomment this out here we don't need any of this we can just say get loudness from microphone and then times that by our loudness sensibility we go back into Unity we've got our game object here scale with microphone uh we need to set this back up so we can do 111 555 drag in our detector so now when we start speaking when we hit play the cube will go bigger based on how loud we're talking if we whisper it'll be smaller I've got a lot of background noise um that the microphone picks up so I can change the sensibility or the threshold if I want so if I want to cut out some of the background noise we can just drag up the threshold to like 1.27 and if I don't say anything see it's not actually getting picked up so my kind of like background noise doesn't go above like0 2 but when I speak it does go above this threshold and we can see that here based on my voice okay so this is kind of where that original video that I mentioned uh ended or at least where I jumped off that was kind of as as far as I needed if I didn't explain any of that properly I'd go and recommend just watching that video um just so you can kind of understand he explains it a little bit better uh probably than I did um but from this point on this is all me baby this is stuff to actually work on getting uh the don't scream functionally working um so we can actually just hide this scale microphone now we know that it's working and we going to make a new UI canvas uh so we'll go UI canvas and we can set this to uh scale with screen size just to get rid of this check boox this like warning I'm just going to click vertex color always in gamma for the reference re for the reference resolution I'm going to do 1080p and we can to this we're going to add a image and it's going to be a fill bar so um we'll do Unity UI so we will right click UI image and we want to set our image we need to Sprite first so we'll pick UI Sprite I'm going to set it to filled and I want it to go horizontally and fill from the left so we can set this here now this the buil-in UI Sprite it's a bit rubbish they're very blurry and um if we go to look at our UI it stretches weirdly so in the same folder that we had the finger snapping at the same Link in the description I have included a plain Square here which is just being set up to um you know it scales nicely it's not like Blurry or anything so we can make this as big or as little as we want and now the fill amount if we're at zero it's gone if we're at one it's full and now you can as you'll expect that loudness uh variable instead of luring between a scale of like 1 to five we're going to use it to set our slider so as we talk it will go up and down like this and when we shout it'll go to the top and when when we speak quietly it'll hopefully stay down here and we'll see it doing this so let's set this up um so what I'm going to do is I'm going to go into our W transform I'll hold shift and ALT and we'll click the middle one just to set it to the middle of our screen we'll set it to one so we can see it and then I'm just going to hold alt and drag on the right hand side just to fill it uh just to set the fill kind of to fill up more of our screen so we can see it as we talk it'll be doing this now and we'll be able to see the actual loudness a bit better on like a bigger scale and I'll just move this down here and so with this I'm going to right click create empty parent and I'll uh do audio loudness meter and then I'm going to duplicate our image here instead it is a child of this top one and we'll call this uh threshold indicator and I'm going to hold alt drag this in just make it a little bit bigger and change the color to like red so now as we set this the idea is that once we go past this threshold that is when a scream has been detected and we'll get the kind of conditions being met that a scream has been detected and to this top level object audio loudness meter we're going to add a new script called fill from microne so now again from our scale from microphone clip we're going to copy and paste this and you'll notice that we're copy and pasting a lot uh a better way to do this is so instead of inheriting um from mono Behavior we could maybe make a new class with some of this shared information so like the detect and stuff um put this into its own function and make it a virtual function so we can override it but that's kind of out of the scope of this video for now we'll just do the old copy and paste because we'll only be really using this script from now on anyway uh they were just like test scripts to just get it working and we'll paste that in here but we need to make some changes to obviously make it work with our fill bar so we don't actually need the minimum scale and maximum scale and we're not going to be doing this uh lurp for the scale here so we can get rid of those there what we will need is a public image using uh Unity engines UI so using Unity engine. UI and we can put this as our audio bar we'll call it that so with that image audio bar we can then come down here and we do audio bar do fill amount is equal to loudness we'll go back into Unity we'll assign our loudness detector we'll assign our image here with the fill amount and we'll hit play and hopefully as we speak we'll see that that audio bar goes up and down if we're quiet it's right at the bottom but when we speak it goes above our threshold here and obviously I'm speaking quite loud here so that is is working we're going hit play and I'm going to get rid of this debug.log actually cuz it's a little bit annoying so we'll get rid of that debug.log and instead of setting loudness to zero which means that the bar completely disappears I'm going to set it to 0.1 F so it'll always have a little bit of fill in it um just so the player can see that there is a bar there um I think it just looks a little bit nicer um so next we want to add a slider which we can use to uh calibrate the actual microphone because we're just hardcoding a sensibility here um every player's microphone's going to sound and different and pick up different noises uh have more like static built into it for example so we can give a give the player a little bit of control over their microphone calibration um sort of based on their surroundings so with our audio loudness mirror here I'm going to right click create UI and we can choose slider and now the slider is really small um so what we need to do is we just need to reposition this and we can make it bigger what I'll do is I'll set it like sort of this size and then move it over so we can have some text here um and I want to change this handle and we can pick our plane Square here so now we have this slider which we can move up and down doesn't obviously do anything just yet and while we're at it let's just add a bit of text so we're going do text text mes text text mesh Pro we'll import TMP Essentials we'll just drag this up here and we can say this is the microne calibration and I will set that just to be capitals just in case we want to type anything else in there and for my demo project here I was using this VCR kind of font which I got from defont I will include a link to it uh in the description below I'll import that into this project now once you've got whichever font you want imported we can right click create text mesh Pro font asset and then on our text here we can just drag in our VCR font that just makes it look a bit nicer um I'm going to change it to Auto size and set the alignment to sort of align with the middle of the box so this slider currently doesn't do anything so we need to make a function that we can use to get the value of our slider and then do something with that so let's go back over to our um script The Fill from microphone script and I'm going to make a public void set loudness sense sensibility and this will take in a float which we can just call T now instead of this loudness sensibility here I'm going to rename this to um current loudness sensibility and then make two more floats we'll do public float Min uh minimum sensibility we'll set that to to 100 and then we'll have public float maximum sensibility and we can set that to 1,000 and then here in our public set loudness sensibility method we can say current loudness sensibility is equal to math f. lurp between minimum and maximum sensibility with a value of T so if the sliders all the way at right we'll get a value of one so that'll be our maximum sensibility if it's in the middle we'll get 0.5 which will be in between minimum and maximum sensibility and if we get zero that'll just be our minimum sensibility here and we might want to make it so we can go even lower than 100 maybe for some people 100 won't be low enough but then we can change this in the inspector anyway and for current loudness I'm actually going to set that to 500 and then we want to get a reference to our slide slider so we'll do public slider sensitivity slider and I'm going to make a start function and we'll do uh we'll say if the sensitivity slider is equal to null then let's just return because we're about to try to change some bits on it and we'll do sensitivity dos slider. value equal to 0.5f and then we'll use set loudness sensibility to sensitivity slider. value so we're setting this kind of halfway and then we're actually setting the sensibility to the value so it matches kind of directly from the start we'll go back into Unity come up to our audio loudness meter and we'll drag our slider into the sensitivity slider here and I'm just going to rename this to calibration title okay so we can see our current L sensibility here when we hit play we're going to set it to 05 which is 550 so that's 550 is halfway between 100 and 1,000 and you see that's quite high for our sensitivity so if we want to calibrate our microphone we can actually make this lower and as we drag this down you can see nothing happens because of course it doesn't because we forgot to in our slider where it says on value changed we need to drag our audio loudness meter into this and then choose the function that we've just made so now when we click play and we go back to our audio loudness mirror as we change this we can see the value here of current loudness sensibility changing so if we drag this all the way down this calibrates it so it's really quiet now so you can see this isn't when I don't talk this is the kind of the background sound I can basically boost that so I go all the way to the top it's always full uh cuz my background sound is now multiplied by a th so a player will want to calibrate their microphone so if they whisper if they whisper it doesn't go above it obviously I'm having to whisper loud so you guys can hear me but if I shout it goes above the red and if I'm quiet it doesn't always go above the red so we might actually want to make minimum sensibility something like 0.1 for example and then if we drag that down see now I'm speaking quite loud and it's not really going above it um so this gives a lot more finer control to the player to actually calibrate their microphone okay so we're nearly done um on my main camera I'm just going to go to background and we'll change it to that kind of nice VCR blue that we saw in the test project and I'm just going to move this stuff up um just slightly so now we're going to make a drop- down list so we can actually choose the microphone in case the player's microphone isn't now we don't want to hard Code Zero cuz the first microphone in that list might not be the microphone that the player wants to use um so that is quite easy to do um what we want to do is create a dropdown so what we can do is right click our canvas go to UI and we'll do uh drop down text mesh Pro we see that comes in here and it's quite small so let's sort of scale this up we go into drop down we'll go on to our rect tool here and we can just drag this out to kind of fill this space we'll make it a bit bigger for the label we can click Auto size make it always um capitals and we'll choose our VCR font uh we might want to just give it a bit of kind of breathing room here you might also want to make the arrow a bit bigger to match kind of the scale of the UI and in our template we'll have to just expand this so we can actually see the drop-down list here so this is what it'll look like when we have some options uh you can see this is quite small so we'll want to go to um template viewport content item and then we can make the check mark a bit bigger go to our label just drag that in a bit and then again we want to set our VCR font choose Auto size make it always capitals then we can make this option just a bit bigger so we can actually see that easier in the drop down list and then we can hide uh template make sure you re-hide that dropdown I'm going to rename this to uh microphone selector and let's add a script to this and we can call this microphone selector and open that up in your IDE so we'll zoom in here I'm going to get rid of uh I'll get rid of the update function I'm going to get rid of this comment as well so we're going to make uh going to get reference to the drop down that we just made so we can do TMP dropdown and we'll call this uh the microphone Source or Source drop down and we want to keep track of the uh chosen device index and I'll set that to Zero by default and in start we'll make a function called populate Source dropdown and that'll just be a private void method so you can see here that back in Unity on our dropdown we've got this here options option a option b option C so when we call when this so basically this uh method of populate Source dropdown is going to actually fill it with correct text so it doesn't say option a option b uh for example so what we need to do here is we can just make a generic variable called options and we will set this equal to a new list and it's going to be a type of uh something called option data but you can see there's a few here there's Unity engine. ui. dropdown and TMP pr. TMP dropdown option data we want to be the TMP variety so we'll select that and the way that looks up here is we have to be using TMP Pro which we are anyway because of this Source dropdown and then just make sure you do TMP dropdown do option data here to make sure it's using the correct version of option data then in we're going to do a for each Loop so we say for each variable uh microphone in microphone. devices we're going to make a new piece of option data and then add it to this list so we have to say TMP dropdown do option data and we can just call that option data is equal to new TMP dropdown do option dropdown do option data and we can pass in um into the Constructor our microphone name and it wants a Sprite Which we're just going to say null cuz we don't have a Sprite to give it then once we've got our option data we can do options. add option data so we're going to make a new piece of option data which is going to just have a string which is our microphone name and then we're going to add it to this to then we're going to add it to this options list here and then we need to get our Source dropdown which is this here we're going to do Source dropdown do options and we'll set that list equal to the list of options that we've just made so that will now populate the drop-down list with the correct data and then we can make another method called public void choose microne which will take in an option index and we can say chosen device index is equal to our option index so let's go back over to Unity that's the wrong one go over to Unity in on value change we'll hit plus and we want to drag in our microphone selector and choose the choose microphone uh method so when we hit play we'll see our options change to our microphone names no we won't because we have forgot to drag drop down into our microphone selection script so we'll hit play and now we should see them change so we see options here microphone Yeti classic which is the string and then we passed in null for a Sprite so there is no Sprite and when we choose one of the different microphones here we can see that working and our chosen device index changes based on our device name there's a bit of overlap here in our um drop- down list that doesn't look quite right so back to template let's just enable that again and if we just give a bit more room to content I believe that should fix the issue it did not so maybe we need to just make the label um a bit smaller so we'll hold alt and just resize that slightly and maybe move it up does that give it a bit more room there we go so that's got a bit more room there so we can there's no overlap so we've got our we can now choose which microphone we're actually going to use for this loudness or at least we can pick the name of it we actually need to plug that into uh the detector so what we can do for that is back in our microphone selection script we want to make a new static Unity action so we'll say public static [Music] Unity action which will give an in parameter which is going to be our chosen device index and we can call this on microphone Choice changed and then when we actually choose a new microphone we can invoke this uh event so we do on microphone Choice changed question mark invoke chosen device index now over in our audio loudness detector so this is where we're actually um picking which microphone to use for our audio clips we can make an on enable function so private void on enable and we're going to listen for that event that we've just made so we'll do microphone Source uh microphone selector do on microphone Choice changed and we'll assign a new method that we're going to make called microphone uh change microphone source and then in undisable we'll have to make sure we unsubscribe from that as well to not throw up any errors it's always best to un subscribe from these events as well and we can make this new method and it's going to be a private void change microphone source which will take in an integer which we can just call device index and then here we can call microphone to audio clip again but instead of hardcoding zero we will pass in our device index which will call this method and it will overwrite the clip with a new clip based on our new microphone and it will you know select that properly based on our index so let's take our calibration title duplicate it we'll drag it under microphone selector and we'll move it up and we'll say microphone source and I actually want this sort of the size of this to match microphone calibration so calibration title is currently at 37.8 so if we set this Max to 36 and then calibration title to 36 they should match we could just rename this to microphone source so I'm going to take both of those and just shift everything up slightly right so let's hit play and we can see if this is all working so here I'm using my Yeti classic if I pick steam streaming microphone I'm not currently using that microphone it's like a virtual device based on Steam um I think it's there from when I had a VR headset but if we choose this it won't work um it's not getting any loudness it's not picking up anything so we're at zero we can choose our Logitech stream cam which is the webcam um that's really loud all the time so we can just drag the kind of calibration down see if I'm quiet it is picking it up but it's being a bit weird so we might actually want to go even smaller on the calibration with that um but my Yeti classic is the good one so that is how uh a user can now choose which microphone they want to use again for our um ah see the minimum Sensibility reset cuz I must have changed it in play mode so if I put that back to 0.1 and hit play and select My Stream Cam and drag this down see I've now calibrated that where if I'm quiet there's not much input registered but as soon as I start speaking it goes back over and and that's going to be different to my Yeti classic which will need a different sort of calibration so that is how um um that works so just make sure minimum sensibility drop that down to something lower give it a bigger range for people to calibrate their microphones differently um because each one will be different and that's the whole point of the calibration so now we just need some sort of thing something to say that a scream has been detected so we're going to come out of play and I'm just going to make a new bit of text so UI text mesh Pro I'm going to hold down shift and all and we'll choose the bottom under the anchor then we can move that up slightly I'll type in Scream detected choose our VCR font I'm going to make it red Center it uh I'm going to choose Auto size and we'll just scale the Box up and give that a Max of 100 so let's just bump everything up slightly we'll move the text down rename this to scream detected text so now this is obviously just for UI purposes um but from once we've done this from that point on you can just then implement this into your game to trigger anything if the person screams or speaks too loud so let's go back over to our script we'll go to our fill from microphone script and here I'm just going to make a new public game object reference called scream text and in update when we fill our fill amount bar we can say if loudness is greater than 0.5f so we'll know that the bar is at least half full which is where we've put our red line so if loudness is greater than 0.5f and the scream text is not active in hierarchy so active in hierarchy so if it's not active and we've shouted we need to switch on that text so we can do scream text do set active to true and then conversely if um loudness is less than or equal to 0.5 F and the scream text is active in hierarchy then we want to turn it off so set active false and in our order audio loudness Mee and don't forget to select our scream option uh scream text hit play now when it goes above the Red Bar the word scream detected come on if we turn this down and if I stay quiet and we're below the Red Bar it won't show scream detected but as soon as's a shout a scream has being detected so the game should be over based on the rules of the game don't scream so from that point on this is completely up to you how you want to uh handle this if you're actually um making a game with this mechanic what you could do is make an event so public static Unity action on Scream detected and here we can just say if loudness goes above 0.5f then we'll say on Scream detected if anything has subscribed to it which is what the question mark means we can just invoke that and then you can obviously in your game manager listen out for this event and then end the game if the player screams so there we have it that is everything obviously in this project here I added in a bit of postprocessing just make it look a little bit nicer um but yeah that's kind of out of the scope of this tutorial the postprocessing but this is our final kind of look here we've got our microphone that we can choose between we've got our different microphones and if we speak above this threshold a scream has been detected now you could obviously move this a bit higher and you know change the threshold of a scream from0 five to 75 or whatever um but this kind of Middle Ground is a nice place to to kind of calibrate to and we can drag this down so as I speak it doesn't actually detect a scream so I can speak quite nicely here like this and if scream hasn't been detected we can calibrate it so as I speak a scream gets detected hopefully you liked this tutorial if you did it'd be great if you could let me know by giving the tutorial video a thumbs up subscribe for more content like this in the future and if you want the project files for this video they will be over on my patreon which is patreon.com dpos but in the meantime thanks for watching and I will catch you guys in the next one bye
Info
Channel: Dan Pos
Views: 919
Rating: undefined out of 5
Keywords: unity tutorial, game dev, indie dev, game design, unity 3d, made with unity
Id: GAHMreCT4SY
Channel Id: undefined
Length: 55min 35sec (3335 seconds)
Published: Fri Nov 03 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.