15

I am trying to build a virtual piano in C#, and want a way to create a scale of musical notes from scratch.

I know that I can simply find or create a bunch of .wav files, 1 for each note, but this will create too much space on the User's Hard disk and not be very intuative for myself in the future.

So is there a way to create a proper sound - e.g B flat, in C# without using a .wav file, all in code thorugh c#, and if not, is there a way to do this in a different language - C++.

Thanks, I have tried to make it as clear as possible.

H Bellamy
  • 22,405
  • 23
  • 76
  • 114

5 Answers5

7

I think this looks like it should get you started:

http://blogs.msdn.com/b/dawate/archive/2009/06/24/intro-to-audio-programming-part-3-synthesizing-simple-wave-audio-using-c.aspx

EDIT : I should add that generating sounds without .wav samples and emulating a piano are at odds with each other.

If you want to get a 'real' piano sound, it's best to work with samples (hold them in memory if you don't want to keep them in the filesystem). If you just want a simple way of emitting accurate notes (which sound nothing like a real piano) then doing it programmatically is the way to go!

Widor
  • 13,003
  • 7
  • 42
  • 64
  • Thanks, that seems like the thing I need. I've got a long road ahead! – H Bellamy Nov 09 '11 at 17:11
  • @Dommer Thanks, one of mine too. Assuming you're referring to his most famous _Toccata_ of course! – Widor Nov 09 '11 at 17:14
  • @Widor I agree, this is mostly an experiment to see how to do this. 1 other point, could you use DirectSound? – H Bellamy Nov 09 '11 at 17:16
  • @JBellamy My answer to that would be "yes, you could" but not having done it personally, I'd have to suggest you ask a DirectSound-specific question to get some better answers! – Widor Nov 09 '11 at 17:19
4

If you want to emulate a piano specifically you only have two options, one of which is to use samples and the other is physical modelling. Physical modelling requires some pretty advanced knowledge of DSP (filtering, convolution, etc.) and a piano would be a challenging instrument to tackle but it has been done by the likes of Pianoteq

On the subject of samples, to create a piano that is anywhere near a convincing analogue you would ideally require more than one sample per note for different velocities with crossfades between them but you can probably get away with using a sample over a limited range of notes to reduce the total number of samples.

Steve Rowbotham
  • 2,868
  • 17
  • 18
0

You might want to take a look at MIDI.

There's a windows API you could wrap in C++, then expose a managed interface you can consume in C#.

DanDan
  • 10,462
  • 8
  • 53
  • 69
0

Maybe using a MIDI library could help? I'm not into the matter, but if you don't want to use audio files it's the only thing that comes to my mind. There's a question about it here.

Community
  • 1
  • 1
0

Here you find an article including source on how to create piano sound from scratch for at least one Octave worth of musical notes (it is extensible).

Yahia
  • 69,653
  • 9
  • 115
  • 144