Build a Sampler VST Plug-in with JUCE Part 1 - Loading and Playing

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 what we're going to do is create a very simple sampler so this sampler is kind of like the hello world of samplers it's only going to be able to load a file and you're going to be able to play the file back at different speeds so I have our finished product here I can just hit the load button what this does is this loads up a browser where I could pick an audio file so I'll just pick this one here and then when I hit many keys on my keyboard it will play back the sample at different really different speeds yeah so pretty simple but pretty effective so this will get you started if you're ever looking to create your own sampler and let's go ahead and get going so I will close out of all this close out of my previous code and out of this and we will start a fresh file so here we go and today we're going to create a audio plug-in I'll go call this hello sampler and I will hit create and as you can see it creates our for plug-in files here from our template here and I'm going to hit save and open an IDE just to get get this up in the next code so now I'm just gonna close all this out open up our source folder I'm also going to edit the scheme so that this opens up in Ableton so what I could do is I could just change the executable here and then go to applications and then into choosing Ableton or you can choose any da W and I uncheck debug executable make sure if you're on Mac that you uncheck that okay so this ensures that we will open up in ableton to be able to test and debug our plugin as we progress on through so I've actually created a little text file here just to show some of the classes that we're going to need or the synthesizer so we're going to need the synthesizer class okay so the synthesizer is referring to just any sort of base class that is like a musical instrument that plays notes okay so so even though it says synthesizers this applies to samplers as well then we have what's called a sampler sound class so it might be helpful if I actually show you these as I go through so we have the synthesizer class so that's the class for musical device that could play sounds so we could do loads of stuff with this voice stealing all kinds of stuff then we have the sampler sound class which represents our sampled audio clip so we'll load up an audio clip and the synthesizer sound represents that audio clip and as we can see sampler sound inherits from synthesizer sound sampler sound is a type of synthesizer sound so then we have sampler voice and we have sampler voice sampler voice our I think of more like the number of voices that a sampler can play so you know if you have like a Moog synthesizer can only play most of them can only play one voice at a time as opposed to like a junior weight which would be like a polyphonic synth where you can play where you can play like sixteen voices at once so the sampler voice class really kind of defines the number of voices that the sampler can play so that is what we're going to use for building the actual sampler itself then we have to think about file loading so we have to load a file to be able to play it and for that we're going to mainly use the file shoes or class which is loading the browser dialogue that you saw earlier where we can actually choose a file to play and then we have the file class which is the actual kind of file itself that we choose then we have this task of taking the file and converting it into a stream that we can actually play samples from so for that we have the audio format Manager class and that kind of registers our basic audio formats so that we're able to do stuff with formats like mp3 dot wave so on and so forth and then you have the audio format reader class which reads the actual samples from the audio stream so those are the main classes that we will be using today so I will put that aside and I will minimize this for now and I forgot to do one key thing so if we go back into our sampler our producer itself key thing that we need to do is go into the settings so you have the sprocket here up here at the very top left and key thing that we want to do is check in plugin is a synth and plug-in MIDI input if you don't have these two checked in then your plugin will not work properly okay so now we're opening back up in Xcode again and now we're back so the first thing that we're going to do is we are going to create a synthesizer so we got we can just do that by creating a synthesizer object and I will call this M sampler and that is I use the m2 to symbolize any sort of member variables that I have that are for the whole class so then going into the synthesizer class so let's just go in here and I'd like to show everybody how I find these methods so so one method that we want to use is we want to define how many voices that we want the since this is the I keep on saying synthesizer but it's actually a sampler how many voices we want the sampler to have so for this one let's do three voices so what I could do is I can go back to my header and here I will say Const int and I will call this um voices and I will set this to three and then what I could do is I could go back to my constructor and my CPP and I could say I can make a for loop I is less than M voices I plus plus and then we could say m sampler add voice and then here we will say will create a new sampler voice okay so that creates three sampler voices that we can call on within our sampler and we do that in the constructor we don't need to add voices at any other point in this in this project okay so so that's fine now next let's have a look at let's see here so let's have a look at file loading really quick okay so we have let me just pull this up here so we have the file loading so we need the file chooser class and the file class so what I could do is I could create a button here really quick so that I'm doing this super hyper quick right this is just the very simple example of how to do this so I will say I'll create a text button and I'll call this load button and what we could do is we can put the text of what we want on here so I'll just hit I'll just call it load so that's what's that's the actual text that's going to show up on the button itself then what we could do is we can I will make this 200 by 200 so that just sets the size of the background itself and then I will say M load button and then what I want to do is I want to define what happens when I click the load button okay and here we could use a lambda so this is just this is just a very small function that I'm going to call that just basically executes when I click on the button okay so it says when I click on the button execute this function that's essentially what I'm doing okay so at the moment I don't actually have a function that I've created for this so let's go back to our processor very quickly and I'm going to I think I think this is going to be fun I don't think that's going to be an error so I will create a a function called load file and then let's just put it on down here and load file okay now this is where we need to actually use our file chooser class okay so actually I'm gonna keep that open for now and let's go back to the API and let's go to the file chooser class here we go so here we have a constructor for file chooser so it has all these arguments but as we can see when it has these equals something in the arguments that means that they're optional right so all of these last all of these except for the first one are are optional so we could just make a string that it just says hey what do you want it to say when you're when you're file chooser comes up so I could just say file chooser and I'll just call this chooser and then I'll say please load a file okay now we have so now we want to search for a file right we want our dialog box to open up we want to choose a file so now we can just use this function called browse for file to open okay and this returns a boolean right so if so I could say if if chooser dot browse for file to open and then what I could say is now I want to get the result of that file right so now we need to go to the file class okay this is something that I'm kind of zip through but you you learn this over time okay you learn the API what sort of what sort of classes that you need to call on to do these things okay so going back to our file chooser I'm just gonna open that up in a different different window so file chooser okay so we have this method that's get result and it returns the last file that was chosen by the browse for methods okay so what I could say I could say auto file equals a chooser get result right so now we get a result and hopefully it's an audio file okay so now so now we got the result let's go back to our our little kind of overview here and now we have these two classes we have this audio format manager class registers basic audio formats so we're going to need that and then an audio format reader class okay so I'm going to do a audio format manager that man M format manager and then I'm gonna do I'm gonna create audio format an audio format reader as a pointer okay and the reason is and I'm gonna set it to null pointer to start off with and the reason that I'm doing that is because what if I want to choose another file well I don't want to if if I want to choose another file I don't want to delete the reader itself I just wanted to point somewhere else okay so I don't so I don't need to create another reader every time I want to choose a different file I just want the reader to to create I just want to create a reader for a different file every time that I want to load up a different file to play with my sampler okay so another thing that I could do is I go up to the destructor and then I say I'm gonna set this back to null pointer when the in my destructor when my when my plug-in gets destroyed or when it gets closed okay so now what I could do is once again I have this format man I am a an a ger okay that one thing that we want to do if we go back to the audio format manager and if you're not familiar with what I'm doing here I actually go into more detail in another tutorial on loading and playing a file so building a basic audio player and I will link that to you in the top right of this video so audio format manager we have this function that's called register basic formats and this is a method for registering the format's that come with juice so that's that way if I believe it does dot mp3 and we need to actually call this in the constructor okay so we got em format manager register basic formats so that just allows us to use dot waves dot mp3's so on and so forth in in our program to to actually stream audio okay so down here down at the bottom we can just say M format reader so remember that's a pointer an audio format reader pointer and what I could do is I could say M format manager and then I have this function that's quite handy called create reader for okay so it actually creates a reader for my file okay so let's go through this so we have a file chooser it opens a it opens up a browser dialog or we could choose a file then if we actually choose a file we want to get the result of that that's stored in this file and then what we do is we create a reader for the file okay so that's how we get that's how we get our actual file to load up in the sampler okay so now if we go back to our synthesizer class actually I'm going to create another tab here and we have let's go down here so if we go back to our overview here we see that we have a sampler sound class that represents the sampled audio clip that that we want to play right so it makes sense that we want to somehow associate the audio leave the audio clip from that we're going to control in the sample sampler sound class with the file that we want to load that we want to play different notes of right so that would go into sampler sound class and here we can just say sampler sound and we can actually declare some parameters here okay so this big integer one was a little bit confusing for me because I wasn't sure what to do and luckily the community from the disc Court actually helped me there now I'll explain that to you shortly so we have this sampler sound where we can actually put our audio format reader as one of the parameters and give it a name okay then we have our sampler voice so sampler voice is here and then where is it so this method is actually in the synthesizer class where we actually add the sounds so here is where we do this at so we have synthesizer sound so we have add sound here where we can say M sampler adds sound and then here is where we want to create a new sampler voice or simpler sound rather I always get those confused and then we need our parameters in here so I will just copy these for now piece them in here so we have a string name I will just call this sample then we have our audio format reader source so it takes a reference so what we need to do is dereference our M format reader which is currently a pointer so when it's a pointer and it needs a reference we need to use this star operator at the stop at the start rather and that will dereference and give us our reference to our format reader now this cost big integer MIDI notes actually confused me for a moment there's a there's actually what you have to do the biggest integer many notes is actually to give the range of many notes that you want the sound to be able to play across in this case we want it to be able to play across the whole range of the keyboard so what I could do is I could create a big integer called range and then we got range and then we have a function called set range and then I'm not going to get into what a big integer n' is it's essentially like a whole set of boolean so i believe it goes from 0 to 128 and so if I wanted the sound to only be able to play across a certain range I can actually define that here in between if I only wanted to play it from I think MIDI note 12 2 to 128 I could do that there I don't really know I confess I don't really know a whole lot about this or why this is the way it is but I actually had some people from the community that helped me out with that so big shout out to yell at Daniel and try liver I think his name is Trevor he helped out as well and Sanaa Chios from the community who have always been a big help so that's why we need range in there then we have mini note for normal pitch so that's what MIDI note it would you would want to play at for the pitch to be normal so normally that's C 3 which is MIDI note 60 and then you can set a an attack and release time so I'm just going to put this to zero point one and zero point one so this is just going to play as soon as I press the key down and stop as soon as I release the key and then we have max sample length seconds so I will put this to 10 seconds okay so so that's what we're that's what we're doing there so we're then so we're adding once we've chosen a file that will add the actual it will create a new sampler sound that contains our audio for our actual or actual audio file ok so that's how we do that now down here what we could do is in our process block I could just get rid of all this commentary ok and I can actually get rid of all this as well so we actually only need one method in here which is I think it's called render next block so the nice thing about this is that it actually parses through the MIDI for us it doesn't we don't need to worry about parsing through the MIDI buffer or anything we can actually just give it our output audio which is called buffer we give it the input MIDI which is MIDI messages so that's the incoming midi messages from our midi controller then we'll say start sample is from zero and then number of samples will be buffer getting them samples okay and there's one more thing that we need to do here which is quite important and if you don't do it you will get a crash or won't work properly so we have we have to tell our sampler the sample rate so we need to do this and prepare to play so prepare to play is whenever our our host is stopping getting ready to play it will call prepare to play or if some for some reason the buffer size changes or the sample rate changes then it will call prepare to play okay so we have to say sampler the set current playback sample rate and then we have this helper function and get the sample actually we don't need that here we could just say sample rate okay just give it the sample rate okay now let's go back to our editor and I didn't really finish what I was doing here in the on the UI side here so we have the button when we click on the button we want it to do something so we want it to call load file remember our load file method that we created earlier so we want that's what we want to call when we hit the load button right so we could say we have this reference to our processor here so the processor allows us to access all of our stuff all of our public all of our public information here from our plug-in processor okay so that's why we could just say processor and then load file okay make sure you put a semicolon there because it's inside a lambda and then after the line there and then what we need to do is make our load button a child component of of this component this all of this background component that we're in currently so so that's why we do that and now all we need to do is just actually make it visible on the screen so I could call mm load button set downs and I'll just do it super simple here so we have a 200 by 200 background so what I could do is I could say get width which gives us the width of our plugin I'll divide it by two which puts us in the middle of the screen and then I will say minus 50 and then I will do get height same thing divided by two which puts us in the middle of the screen minus 50 so that puts us minus 50 on the y-axis and then I will just make the width and height 100 and 100 okay so that should put our button right in the middle of the screen okay so let's go ahead and build and see what happens here it looks like I might have an error on that looks like we're good so here we go and while this is building I should have some some more merchandise coming soon so we have some hoodies t-shirts we have hats mugs if you want to drink your coffee and think about audio programming we have a whole bunch of stuff that's gonna be on its way so should be in by the end of the week and that will be coming soon so really cool stuff so here we are we're at a new file here I'm going to go down here and here is our plugin hello sampler and there is our box so now I could click in here that loads our dialogue and now I click load up a wav and now so so that place that plays fine right and you can actually play three of them ëthis you could either do three voices at the same time okay yeah that'll that overloads the audio a little bit but you get the idea so yeah so that is like the basic hello world of sampling and I'm going to do a couple future tutorials that are going to talk about ways that we can actually start to expand on this model so this is just getting us started on on the beginning of our sampler journey so I hope you enjoyed this be sure to give it a thumbs up if you liked it and subscribe and all that business and I will see you next time
Info
Channel: The Audio Programmer
Views: 16,802
Rating: undefined out of 5
Keywords: juce, juce framework, c++, audio, tutorial, audio programming, creative programming, creative coding, dsp, digital signal processing, vst, create vst, create plugin, audio plugin, oscillator, synthesizer, maximilian, openframeworks, software development, beginner, easy, max msp, cplusplus, sampler
Id: F-EkwKFftPY
Channel Id: undefined
Length: 29min 50sec (1790 seconds)
Published: Tue Feb 25 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.