Let's Build a Synth with Juce Part 0 - Oscillator

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey what's up everybody i'd like to welcome you to another juice tutorial and in this tutorial we're going to do something that has been in high demand for quite some time which is building an oscillator using the juice oscillator class in the dsp module so before we get started i just wanted to tell you about our audio programming community so it's a place where people can come and ask each other questions and we have audio programmers of all different levels and it's a great place to connect and tell people about your audio programming woes and successes so uh you can join on the audio programmer dot com forward slash community and that will give you access to our discord group so going back to the lesson this will be the first part of a series that we're going to do where we're going to build a synthesizer using juice and first thing is of course generating sound so we're what's better to generate sound than an oscillator so let's go ahead and start a new project so i have this new project here we'll build a basic plugin and i will just call this basic oscillator it will create the project i will just put this in this folder and now we have our source files and we need to add our dsp module to our set of modules here so we will just go to add a module lose choose global juice modules path and make sure that our dsp module is there and we will open it up in xcode so now here we are in xcode and what we will do is uh to speed this process up a bit i'm going to just build the plug-in in standalone mode so to do that just go up to your targets up here at the very top where you have all of your different targets you can build just for vst 3 or 8u and so on we're just going to build for standalone and now what i'm going to do is build that it's very possible if you're just working off of your laptop you may actually get an error here when you first build uh to fix that error the reason that it's happening is because uh it's expecting inputs for the standalone mode here so we see that here on line 18 it has this dot with input and this is set to true if we just change this to false i'm going to wait for it to compile here so it doesn't mess me up here um if we just set this to false this will fix any error any build error that you have when you first try to do this so we'll just do this again and while that's while that's doing its thing we see we got build succeeded there so we're fine we've got our hello world here so there's blake project and now we can just move to our plug-in processor header file and we can start creating our oscillator so let's go to our documentation here and see what we need to do to get this started so we see that we have this oscillator class this could be a little bit tough to set up if you're first starting off uh maybe tough isn't the word just challenging to figure out the right syntax but just follow along and everything will be okay so we need to set it up as dsp oscillator and then it has this sample type so we need to tell it what sort of samples that we're trying to process in this case we're trying to process floats so we'll just put float in there going down a little bit further we have two different constructors that we can declare for this we're going to use the second one here and we see that we have this const stood function numeric type function now what does that mean this is actually where we're going to put our equation that's going to calculate uh calculate our wave out okay so that is where we're going to do that do that and we're going to do it using a lambda function which is just a small function that we're putting in place that will actually calculate the values that we need for our wave okay we can also we also have this optional argument where we can use a lookup table so lookup table is a way to actually make this this oscillator more efficient and i'm not going to get too far into what that means but let's just have a look really quick uh while we're here and just take a look at the oscillator class so if you go to juice dsp into widgets i believe and then into juice oscillator we will see once again we see that this is a template class so that's the reason why we need to tell it what type of processing that we want to what type of data type that we're trying to process and in this case we have we're using floats and in here we see our alternative constructor here and we see that it creates an oscillator with a periodic input function between negative pi and pi and if the lookup table is not zero then the function will be approximated with a lookup table so uh so that's the optional argument here that we have and if we go down we see that this uh constructor calls an initialize function and then in here we have this uh table that could be that will be calculated uh that will actually approximate the um where your wave needs to be at a particular time as it processes through your audio callback okay so that's that and just just to give you a general a very high level overview of what's happening there so let's go ahead and just declare this oscillator so we could say juice and then in the dsp name space and then it's oscillator once again we need to declare what type of where of uh the data type we're processing which is float i will just call this ask and we will oh i'm not sure what that happened there and then uh what we will do is we will create this uh create our constructor here so we will declare that in line and once again we need to use a lambda so open and close square braces and then we will have this float x which will be our input and then what we will do is we will return and in this case we're going to start off using a sine wave so we could use the standard function stood sine we're just going to do stood sine of x and i think all of my um all of my braces are in the right place so hopefully this works and so we're fine there and we have built successfully okay so by the way i'm going to put all of this on our github afterwards as well so you can actually just download this or clone the repo and actually check this out if you're having trouble getting this to build so moving on to our plug-in processor.cpp as you may know from our past dsp tutorials normally in any dsp algorithm there will be some type of preparer method where we need to be able to give our dsp algorithm the sample size buffer size and number of channels that we're trying to process okay so this is important especially if we're using the juice dsp class but normally even if you're not using a the juice dsp module then there will still normally be some type of prepare method that where you need to pass in at least the sample rate okay so first thing that we need to do is going back to our documentation is we have this thing called process spec so i've gone over this before but just to review this process spec is the way that we can actually pass this information neatly into our oscillator so we can just create one of these inline so juice dsp process spec spec like that and then we can just declare all of these things so we got spec maximum block size equals samples per block so that's how big our buffer size is then we have spec dot sample rate equals sample rate spec dot num channels equals and then you have this get total num output channels and now i can just pass that into my oscillator by doing osc prepare and then you see it just wants you to pass a reference to the spec object and that's how i could do that okay so that's all good to go now we can move on to our process block where we actually do our processing here so i'm just going to get rid of all of this boilerplate commentary and i can actually get rid of this as well and so what we need to do as you may remember if you've seen any of the other dsp tutorials is that we have this audio buffer which is our audio buffer here and then what we need to do for any juice dsp module methods is that we need to pass it a audio block which is an alias for it's essentially the same thing as the audio buffer but it's what we would call an alias so we can just create one of those here by saying juice dsp once again because it's in the dsp module audio block and this is once again typefloat make sure all of these are the same and you're not mixing and matching data types and i will just call this audio block and if we go into our audio block documentation so here we are scrolling all the way back up we can construct an audio block in a number of different ways the one that we're using is this particular one where we're using constructing an audio block with our audio buffer we can see here from the documentation that it says down here audioblock does not copy nor own the memory pointed to by data to use so so that just means that it's not creating new data it's just an alias is just pointing towards our buffer so we can put our buffer in there as an argument and then what we can do is we can now process the oscillator through our buffer so osc dot process and now it expects this proc process context so just to review quickly process context replacing uh so you can do two types of processing either replacing or context nom replacing so process contacts replacing means that when we pass our buffer or our audio block into this uh into this process method the outcome is going to if we do replacing that uh the results of whatever happens in that process method is going to replace whatever was in the uh was in the audio buffer before whereas you have context non-replacing which is not going to replace that contents okay so that's that's what that's all about so now we just need to create a process context replacing object and we can do that in line just by throwing in our audio block in into this uh into this object so once again it's juice dsp process context replacing once again make sure all of your data types are the same they're all floats in this case and then in here i can say i can just put in my audio block okay so this is this is ready this will actually work but before we do that what i'm going to do is i'm going to actually create a gain so we can actually just adjust this make sure it's not so loud in your headphones okay so we have this juice dsp gain once again same sort of same type of uh same type of thing okay so we just say juice dsp gain once again gotta make it float and i will just call this gain and that's it since it's part of our dsp uh since it's part of the dsp module we've got to do gain dot uh uh oh sorry i need to go back up here to prepare to play and we need to do gain dot prepare where we pass it the where we pass it our process back and then another thing while i'm here is we need to set a uh a frequency for our oscillator so i could say set i could use the method called set frequency and then in here i'll put 220. and i know that i'm going through this a bit quick the reason is because if i if i go back and forth it'll make the tutorial a lot slower so please feel free to watch through this multiple times if you need to okay so now we're down here and now we want to process our uh our gain so we can just actually i think i could do just in the prepare to play i could say gain set gain linear so i'll just use this for now and i will just make this 0.01 okay make sure that you don't do uh that you don't mix up set gain linear and set gain decibels and get those mixed up and put in the wrong numbers because that will be very painful okay so make sure gain linear is between zero and one okay and now we will just do gain dot process and we just put uh once again we just create another juice dsp process context replacing that's type float and audio block again okay so uh so some people may be saying wow why are you putting audio block through there again remember this is process context replacing so what happens is osc process runs and then audio block once once this is finished running this method audio block is going to contain our sine wave information and then we have then we run gain dot process which is going to take all of those values and it's going to turn it down so we don't blow out our eardrums okay so let's go ahead and just build this really quick and see if this works and you should hear a sine wave there okay so i kept it very soft just in case you had your headphones quite loud so there you go uh one thing if you're building a standalone mode and you're not hearing anything make sure you go into your options audio and midi settings and make sure that your output is actually set to your speakers that you're listening on your headphones or wherever you're listening through okay so so that works great so that's a sine wave and now we've built an oscillator so this is cool uh one thing that we could do as well is we could try some different waves in here so i will just take this i will duplicate this very quickly and and down the road in uh very soon in our subsequent tutorials we'll organize this a lot better where we can actually switch between the different uh equations so we have let's make a saw wave so i have this uh equation that i actually got from so this is courtesy of the juice team in there i think they have an oscillator tutorial where they actually show um where they actually give a use for for the juice dsp oscillator class and let's see what have i oh this has to be you have to declare this as juice so there we go so this is now a saw wave okay and let me put these down here just in case you clone the repo uh and you're not sure where these are coming from so that would go like this so this is a sine wave and then here's a saw wave and a square wave okay just in case you want to experiment around and i highly encourage experimentation so yeah please feel free to experiment around so this should this should sound like a saw wave now cool we have a saw wave that sounds great and let's try a square wave now so let's just replace this really quick and this should be a square wave there we are and that is really how it works now if we wanted to add uh if we wanted to add a lookup table to make this more efficient uh maybe a good arbitrary number might be i don't know 200 points for our lookup table and you won't hear any difference really in the sound but it will be efficient it will be more efficient when you're when you're changing the frequency okay remember that our changing we're just changing the frequency in uh our prepare to play at the moment that's where we're setting the frequency so if you're looking for a cool exercise uh to do to extend off of this maybe think about setting getting sliders for your oscillator frequency and for your gain and uh and connecting those up so you can actually change your gain and your frequency so that's that's it for this tutorial and uh yeah that's that's how we create a basic oscillator and we're going to use this and build upon this to actually start building a synthesizer so i hope you enjoyed this tutorial and i will see you next time be sure to like and subscribe if you found this helpful and i'll see you soon
Info
Channel: The Audio Programmer
Views: 11,738
Rating: undefined out of 5
Keywords: juce, juce synth, oscillator, sine wave, square wave, saw wave, dsp, digital signal processing, juce dsp, build a synth, program a synth, synthesizer, synthesiser
Id: ADG6Rsd3ekg
Channel Id: undefined
Length: 19min 28sec (1168 seconds)
Published: Thu Dec 03 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.