tags:

views:

1512

answers:

6

Hi,

I am newbei in creating such piano based UI application. I am developing an application in which i have to use Piano. How to create piano UI in Iphone? How to create it using Interface builder ? or is there any other way to create the piano?

I am in urgent need to create such UI,If any body has any solution or any useful code or any Link,which would be appreciated.

Thanks, Niraj

+5  A: 

Version 1 Easier to code, not so elegant.

Start a view-based project in xcode, and use interface builder, editing MyProjectViewController.xib, to put together a series of buttons in shapes and colors that resemble an on-screen keyboard.

Edit MyProjectViewController.h and .m to add a bunch of methods with signatures like this:

- (IBAction) playMiddleC: (id) sender;
- (IBAction) playMiddleD: (id) sender;
- (IBAction) playMiddleE: (id) sender;

Now you can go back to interface builder, and right-click-drag each button to the File's Owner object (of type MyProjectViewController). When you do this, a menu pops up asking for which method to attach the button to - choose the appropriate method for that key.

Now add a sound file for each of the keys you want to play. For example, let's say you have a file called midC.caf (encoded in, say, linear PCM format, as one of the valid sound file formats). Copy that file into your project directory, and add it to the project by right-clicking (in xcode) on your Resources folder, and choosing Add > Existing Files.. and then choosing your sound file.

Next you need to add the AudioToolbox library as a dependency. Do this by right-clicking on the Frameworks folder, and choosing Add > Existing Frameworks.. and then browsing for the AudioToolbox library and choosing it.

Meanwhile, back in MyProjectViewController.m, add code like this:

#import <AudioToolbox/AudioServices.h>

// (Later...)

- (IBAction) playMiddleC: (id) sender {
  NSString* path = [[NSBundle mainBundle]
                    pathForResource:@"midC" ofType:@"caf"];
  NSURL* url = [NSURL URLWithString:path];
  SystemSoundID midC;
  AudioServicesCreateSystemSoundID((CFURLRef)url, &midC);
  AudioServicesPlaySystemSound(midC);
}

Hit Command-Enter to compile and run, and make some music!

Version 2 Slightly more elegant.

Programmatically construct a keyboard UI with one array for white keys, and another for black (since they'll have slightly different coordinates). I recommend using custom UIButton types with images for pressed and nonpressed versions of each key type (black, C/F, B/E, and A/D/G, which are middle white keys - I'm account for key indents here).

This would allow you to store pointers to these buttons in an array, and have a single method that could play the sound for any key, by also storing sound ID's in a corresponding array.

You might think you'd want to create the sounds programmatically, as they are somewhat mathematical in nature. But the AudioQueue services, which you would use to do so, are significantly more work to deal with than the above code, so I'd actually recommend just using sound bytes, even in an "elegant" version. It might even sound better that way, anyway.

By the way, your question makes it sound like you're thinking of this as a quicky and easy starter app, but I'm not so sure. Learning to develop iPhone apps is fun, but takes some serious time and effort to do well. If you're interested in starter app ideas, I'd suggest looking for things that exercise one new programming skill at a time, such as learning to work with the ubiquitous UITableView, or a quick app that lets you take a picture and attach it to an email, for example (that's easy because the iPhone SDK has great support for photo-taking and email-composing).

Have fun!

Tyler
Given that he says he has an urgent need, I don't think that he is doing this as a starter app just to learn or have fun. I would guess that someone has assigned him the task to go and create a piano app. Probably someone in marketing said, "hey, how hard can it be? I hear people make apps in just a few days and Niraj is smart, I'm sure he can do it in even less time!" :-)
mahboudz
If I had to scope out the work involved for a good piano app, including getting the graphics to look decent to using nice sounds for key hits, I would estimate no less than 3 months and possibly 6 months, from start to beta (double that if the lone programmer is doing this as their first iPhone app). For example, playing back a small sound clip is one thing, but to loop it well for an indefinite keydown to get a nice sustain of the note, is entirely different (ADSR envelope: http://en.wikipedia.org/wiki/ADSR_envelope).
mahboudz
Tyler
+1  A: 

One way would be to take picture(s) of a piano keyboard and use the images as the starting point.

Another way would be to draw the keys in Photoshop or other app, in both the default and the key-pressed positions, and use those as the base.

Do a search on the App store and look at the different ways piano keyboards have been implemented. There are a few, and some look more realistic than others.

Doing a good representation of a piano keyboard isn't going to be easy, especially if you try to get the glossy look of the keys and create realistic movement via animation, but it is doable. At the very least, you should study Quartz 2D and possibly some CAAnimation.

mahboudz
A: 
Niraj
finally i managed to get my piano UI working. Thanks for the help.
Niraj
A: 

Hi Niraj. I am trying to do a similar musical app. Can you help me? Maybe send me some example code to orientate me?

Rafael Munoz
sry for late response. i can help you, u want to create piano keyboard UI?
Niraj
similar ... my first step is just an array of buttons (13x3) that plays one note each one. i have move forward a little but i have issues everywhere, mostly in the sound code (I am trying to use RemoteIO)
Rafael Munoz
A: 

I did something very similar. I used normal buttons with the round corners and made each of them play an WAV sound. It works nicely!

Tristan Seifert
A: 

Niraj, can you help with the problem where pressing a Black key which is layered above the White keys ends up playing multiple sounds instead of the single correct key?

Chris