Music Synthesis In .Net Using DirectSound: Diving Deeper Than MIDI?

Sep 16, 2009

So I was working in the forums and on the side bar of some thread I saw this link: [URL]...It turned out to be almost entirely about MIDI, but it made me think that I really need to share some code I wrote a few months ago and see if perhaps there is reason to create what the above thread really asked for.Here's a little background on the following code:

Back in April or so of this year I was doing some spring cleaning and came across the old Yahama PSR-6 synthesizer keyboard I had received one year for Christmas when I was around eight. I remember I used to like the Bagpipe voice because at the low notes it made a good fart sound. For a Toys-R-Us/Radio Shack model keyboard, running on six D-Cell batteries, produced in the early to mid eighties, that was an impressive achievement. LOL Anyway, I always did want to learn to play piano (or keyboard) so I decided to literally dust the thing off, put batteries in it, and fiddle around a bit. A couple YouTube videos later and I was convinced that I wanted to do this. While I was waiting for the thing to ship and arrive, I thought that it really shouldn't be that hard to take a DirectSound SecondaryBuffer object and inject it with sound data which whas calculated based on the harmonic values of musical notes, thereby creating your own synthesizer.

First, I really had little idea what I was doing when I first set about doing this other than the general structure of calculating byte data for notes and stuffing them into a SecondaryBuffer object while it played. I had used direct sound a number of times in a number of ways, but always playing sound data that came from a source (file, stream, etc.). This would be my first attempt at generating sound data for the buffer.

Second, I'm a "code-as-you-learn-learn-as-you-code" kinda guy, so that means that I developed the initial project as I went. I would research a specific portion of the project (and there was a LOT of research to do! lol) and then code it and move on to the next. Of course, this eventually leads to a mess of a project in most cases where the foundation is a bit complex to begin with. So the code I'm posting is the second version of the concept. But even at that, I'm only going to post the primary user control at this point and hold back on the code for the test application I created with it (I will provide a download to the compiled app however). After getting the user control cleaned up (if not ideally redesigned), the second app became a "develop-as-you-go" project for the test application - so it is a bit of a mess now too; but mostly just to look at code-wise - it actually runs pretty well.

Third, there are still a few design issues that could be improved upon in the user control, and a few choices which were made for ease of coding that could be changed for performance. But because of this, other than for a couple of routines, most of the code should actually be pretty straight forward to anyone who's comfortable in VB already.

Ok, on to the example: we are going to create a MusicKeyboard control. This control will draw itself like a pinao keyboard and will render keys for the specified number of full octaves. Clicking a key will play the associated note in the specified voice. Voices will be created using a simple digital oscillator and optional even-noise generator on top of a sine wave form.

To get started, you'll first need to have the DirectX SDK installed. The latest one is available here:[URL]..displaylang=en however the example was created using the version from March 09. There should be no impact from using the newer version.

Once you have the SDK installed, create a new Windows Application and add a new User Control named MusicKeyboard to the project. Note that you could create a Windows Class Library project, but I personally like to develop in an Application for ease of testing and later convert to a class library.You'll need to drag a BackgoundWorker component from the toolbox onto the usercontrol and name it "SynthWorker". Set the worker's WorkerSupportsCancellation property to True.

Now you'll need to add the DirectX reference to the project. You can rightclick the project in the solution explorer and select Add Reference or go to My Project and the References Tab. From the list of .Net references,add Microsoft.DirectX.DirectSound.That should be all we need to get started. In the following posts we'll create a bunch of code files and get the keyboard noise-worthy.Reed Kimble - "When you do things right, people won't be sure you've done anything at all"

View 8 Replies


ADVERTISEMENT

[2008] Midi Note Fetcher (harping Midi)

Jan 10, 2009

There is this software called Harping midi. You can view it here [URL] I would very much like to recreate this software but extend on it. For example. I would like to be able to pick up the "midi notes" and then translate that to guitar tabs. I would like it to have the same features (channel filters, tempo control) those sort of things. Where would I start???

View 1 Replies

Speech Synthesis Speed(rate) Is Not Slowing

Apr 9, 2012

Imports System.Speech.Synthesis
Public Class Form1
Dim speaker As New SpeechSynthesizer()

[Code]....

rate 1 is fast.. is there any way to slow it down? but rate 1 is suppose to be slow.. when i set to rate 5, cant even understand what she is saying.

so did i miss something?and one more thing.. when my app is speaking, the GUI is hanged.. i understand as one function is running, i cant do anything else..

so is there any way to set speaker in backgrond so form can perform different functions?

View 3 Replies

Music Studio - Real Music Video Where Can Play Many Sound At Any Time

Nov 1, 2011

Hey guys, I'm a new guy in VB.NET. I am proposing my final year project a title called music studio. My version of music studio is like real music video where we can play many sound at any time. For example, the application that I want to build can create many button and also add an audio along with it. And then, when we hit the 1st button, it play 1st audio. while 1st audio play, we also can hit 2nd button and 2nd audio is playing. for example, 1st audio is sound of man walking, and then 2nd audio is the sound of glass break. lastly 3rd sound sound of police siren play. Is it cool? so, my question is, is it possible the I can build an application by using VB.NET?

View 6 Replies

Cant Get My Program To Write Into A File That Is Several Folder Deeper Into The Bin Folder?

Apr 29, 2010

the first problem is getting my program to load rss feeds from other websites mostly here:http:[url..... im trying to load it using the webbrowser. i want the rss feed to load right onto the form. im positive that its possible. i have the imports system.net in my code already for xml documents.the second is i cant get my program to write into a file that is several folder deeper into the bin folder.[code]......

ive tried playing with the code moving around the folder names, making sure there capitals are correct, etc. but nothing works. it creates the file in the wrong spot.

View 14 Replies

Directsound - How To Play Wavs

Sep 6, 2011

i am new to directsound. I was wondering if someone could point to a resource that explains how to load wav files and play them using directsound. I found one webpage that explains how to load wavs and play them but you load them by clicking a button and searching a window for the file; what I need is programmatic loading. I can't use simpler methods to play the wavs because I need multiple sounds playing at once and very fast execution times. If it isn't too difficult, perhaps someone could post some code.

View 4 Replies

DirectSound Capturing Microphone In VB

Dec 18, 2010

I'm currently trying to Capture through DirectSound, I've been trying toying around with WaveLib(Ported to VB) but this is very confusing. Anyway, I cant figure out how I am supposed to do this and would like some assistance. The end goal of this project is to: Record Microphone > Encode with Speex Codec > Send over UDP > Decode > Play on Speakers

View 3 Replies

VS 2010 Reference To DirectSound?

Jun 18, 2011

I am trying to follow some tutorials on using DirectSound in VB.NET and I need a reference to Microsoft.DirectX.DirectSound in my project. I have downloaded and installed the Microsoft DirectX SDK (June 2010) but after a restart I have no additional .NET references available to add to my project. I must be doing something really dumb

View 2 Replies

DirectSound Reading Data From Capture Buffer?

Jul 13, 2011

where I'm going wrong with my circular DirectSound Capture buffer here? If I just capture sound into a big non-circular buffer it works fine, but with a 1 second (44100 bytes) buffer and a 100ms timer to poll the read position it goes haywire. No error messages, just long blocks of zeros with short bursts of valid data. calculation of the number of bytes to read and/or the read position.

Here is the code. gBuffer is defined elsewhere as a big byte array. In this example I am just trying to copy the captured sound data from the small temporary byte array (tempBuffer) into the big gBuffer byte array. Obviously this must be done so that every byte is read and copied into the byte array with no gaps or duplication of values. Timer1 is set to 100mS:

Dim McaptureBuffer As CaptureBuffer
Dim MiLastReadPos As Integer
Dim MiBufferOffset As Integer

[Code].....

View 2 Replies

Playing Wave File From Resource Dll Using DirectSound?

Aug 6, 2011

I have used DirectSound to play a wave file in my application using the function CreateSoundBufferFromFile. It works perfectly.Now I want to play files from a resource dll file. I have created 1n2v32.dll file having nearly 100 small wave files named 1,2,3... I can play them using LoadLibraryEx, PlaySound & FreeLibrary.Now, I am trying to use CreateSoundBufferFromResource function from DirectSound api to play these files. I get Run-time error '432': File name or class name not found during Automation operation I used the following code to create the buffer.

Set LDSB = LDS.CreateSoundBufferFromResource(App.Path & "1n2v32.dll", "14", BufferDesc, gw)

View 6 Replies

VS 2010 DirectSound - Streaming / Circular Buffer?

Feb 8, 2012

I use the following code (with Imports Microsoft.DirectX.DirectSound and Microsoft.DirectX) to play sound using an 8 second static buffer:

Dim SD As Device = New Device
SD.SetCooperativeLevel(Me.Handle, CooperativeLevel.Priority)
Dim MS As New IO.MemoryStream(Gbuffer) ' Gbuffer is byte array with properly formatted WAV data
Dim waveFormat As New WaveFormat
waveFormat.SamplesPerSecond = 44100
[Code] .....

What I would like to do next is to change to a "circular" or "streaming" buffer so that I can write new data "on the fly". I've done a lot of Googling but can't quite see how to adapt my present code to use a circular buffer. I can get the current write and play positions using Msb.GetCurrentPosition(iPlayPos, iWritePos) but how do I actually insert new data into the buffer? I have tried "manually" writing new data into my Gbuffer byte array but that doesn't work - I guess once a sound has been played through once, the data is cached somewhere and the contents of Gbuffer are not used again.

View 7 Replies

Directsound Recording Sound Card Audio Output?

Nov 17, 2011

how to capture audio output with directsound? i'd want to be able to save it as .wav, .mp3, or .wma file format.

View 9 Replies

Microsoft.DirectX.DirectSound.SecondaryBuffer.Clone Not Working?

Apr 29, 2012

how can i create a buffer with BufferCaps.ControlEffects set to false in order to clone it? Here's the documentation

Public AudioData(7, 15) As SecondaryBuffer
Dim dev As New Device
Public Sub LoadAllData(ByVal ph As IntP

[code]....

View 2 Replies

[2005] Directsound Tutorial - Stream A WAV File, Not Just Play It?

Feb 14, 2009

I was wondering if anyone could point me to a really good tutorial on Directsound.I don't know if trying to digest an API is a good thing for a beginner but I really need something that has a beginner in mind.I especially liked some of the Microsoft video tutorials on VB.NET so perhaps they offer some on Directsound?The goal is to stream a WAV file, (not just "play it") and I want to LEARN the process.

View 3 Replies

Convert MIDI To .wav Or .wma?

Sep 15, 2010

Is it possible to convert MIDI files (.midi or .mid) into a .wav or .wma file in Visual Basic.NET (2010)? If so:

1: How would this be done?

2: If the output format can be .wma, how can one write the tags (such as artist, album, etc.) to the file in VB.NET?

I am saying .wav or .wma because I know that Microsoft does not have any rights to the MP3 file or something like that.

View 11 Replies

C++ - Animating A Midi File?

Aug 21, 2009

[URL] what tool did this person use to analyze the midi file? was it a midi file? how did they turn it into this kind of animation?is it possible to make an application like this in vb.net?

View 2 Replies

Midi CallbackOnCollectedDelegate Detected?

Feb 23, 2012

Hi to all, i really need some help with a piece of code. Im developing an app, which receive midi TIMECODE (MTC) and shows it on a textbox. when i start the app, it runs ok, and shows the mtc on the textbox, but after a few seconds it crashes and shows an error:
"A callback was made on a garbage collected delegate of type 'MidiTesting!MidiTesting.MidiIn_Callback::Invoke'. This may cause application crashes, corruption and data loss. When passing delegates to unmanaged code, they must be kept alive by the managed application until it is guaranteed that they will never be called."

[Code]...

View 16 Replies

MIDI Input To Run Vb Commands?

Jun 29, 2011

So for the past 3 days I've been perusing the internet for a way to run a command when a MIDI signal is sent into VB - where/how should I start? As a matter of fact, I haven't found any way to enable MIDI input, period.

View 3 Replies

Closing Question For MIDI Child?

Apr 26, 2010

I have setup a dialog result to ask a question on closing, if they would like to close then the child can close, if the child is needed to remain open then the child needs to stay open.

Private Sub BugAManagerTimerForm_FormClosed(ByVal sender As System.Object, ByVal e As System.Windows.Forms.FormClosedEventArgs) Handles MyBase.FormClosed
If StopButtonPressed = False Then

[code]....

View 2 Replies

VS 2008 : Write MIDI To String?

Feb 18, 2012

Is it possible to load a content of a MIDI file into a string and then save the string back to another file? I have been trying for a while, but no luck...Although I have no error, and the new file, view with notepad, looks the same as the original, but it does not want to play..

Dim urlpath As String = (Application.StartupPath & "UserMIDILibrary"
Dim fname As String = Path.GetFileNameWithoutExtention(pathload)
fname = fname & ".mid"

[code]....

View 3 Replies

VS 2010 How To Make A Midi Creator

Aug 4, 2011

How would I go about programming a Midi Creator?

View 2 Replies

[2008] Midi Parser Console

Jan 23, 2009

I have found a midi parser, it WILL work, but first, I need to create a file called consol.I havent got a clue what language this will be in, or is in. I really don't know.

View 10 Replies

Midi In - MidiInOpen And Callback / Delegate Functions?

Jan 4, 2006

I'm using vb.net and have got the midi out functions working properly. I'm now looking to add midi in functionality to my project and I am confused about the use / syntax of MidiInOpen and the associated midiInProc placeholder function. I think I need to understand and use a Delegate function to point to my Midi in handling procedure but nothing works for me and I can't find a suitable example anywhere.

Does anyone have any advice or examples as to how to implement Midi In in vb.net using the Windows multimedia API please?

View 5 Replies

MIDI Sound In After Pausing & Resuming, It Sounds Different?

Nov 19, 2011

I'm trying to play a MIDI file in my VB.NET (VB 2010 Express) and things work well with the code from this other question here on Stack Overflow, which I translated from C to VB.However, I also need to PAUSE, while that code is only for open and stop. I edited the code like this:

Imports System.Runtime.InteropServices
Imports System.IO
''' <summary>

[code]...

View 1 Replies

Play The Usual MIDI Sounds In A VB Program?

Jan 14, 2010

I am looking for a way to play the usual MIDI sounds in a VB program. Similar to the beep program (ie console.beep, leading to timer1.start etc.)but instead of an annoying bleep, have a MIDI sound (like a flute, violin, clarinet, etc.). Is this possible in VB 2008?

View 10 Replies

Record From The Microphone, Then Convert It Into A Midi File?

Feb 3, 2011

i am wondering if it is possible to make a program with visual basic that records from the microphone, then converts it into a midi file.

View 2 Replies

Sending Data To MIDI Device On Computer

Jul 1, 2009

I need some sample code for sending data to the MIDI device on the computer? I'm not quite sure what the hardware's address is though.

View 2 Replies

Call For The Internal Sound Cards And/or Midi Synthesizer?

Jun 22, 2009

I just started programming in vb 2008 Using vb studio 2008 and vb 4.0 Running Windows XP Home Edition My question concerns the capabilities of vb to call for the internal sound cards and/or midi synthesizer.Can a module call the sound card for it to play MULTIPLE notes at the same time, notes of fixed frequency and duration?

I have tried programming vb 4 AND vb2008 for them to modify the default BEEP sound: using a code i found on internet:private declare Function Beep lib "kernel32" (byval soundFrequency as int32, byval soundDuration as int32) as int32

[Code]...

View 2 Replies

MIDI Audio Analysis (for A Guitar Hero Game)?

Feb 26, 2009

In Guitar Hero the notes are saved on a .mdi file how can read the file ( analysis not audio playback ) ?

Attached File(s)
American_Idiot_MIDI.zip (1K)
Number of downloads: 97

View 7 Replies

Place Other Form(child) At The Center Of Midi Parent?

Jun 30, 2011

how can i place other form(child) at the center of midi parent in vb.net windows application i m gettin it at the top left position

View 3 Replies







Copyrights 2005-15 www.BigResource.com, All rights reserved