Iphone Programming: Cs4347 Sound and Music Computing Zhou Yinsheng (Yzhou86@Comp - Nus.Edu - SG) Jan 22, 2011
Iphone Programming: Cs4347 Sound and Music Computing Zhou Yinsheng (Yzhou86@Comp - Nus.Edu - SG) Jan 22, 2011
CS4347 Sound and Music Computing Zhou Yinsheng (yzhou86@comp.nus.edu.sg) Jan 22nd, 2011
Acknowledgement: Some materials are borrowed from the course of iPhone Application Development in Stanford University.
Total 67 Pages 1
Xcode
Interface Builder
Mac
Instruments Simulator/device
Total 67 Pages 2
Total 67 Pages
MOGCLASS
MOGCLASS DEMO
Total 67 Pages
MOGCLASS
Do you want to create your own iPhone App or develop novel user interface for MOGCLASS?
Total 67 Pages
Total 67 Pages
There are a lot of materials about iphone online. What we will cover today is more oriented for your final project.
Total 67 Pages 7
http://developer.apple.com/devcenter/ios/index.action
Total 67 Pages
Core Services
Core OS
Outlines
Objective-C basics Building an Application Sensor programming (accelerometer, GPS, microphone) Audio Libraries
Total 67 Pages
10
OOP Vocabulary
Class: defines the grouping of data and code, the type of an object. Instance: a specific allocation of a class. Method: a function that an object knows how to perform. Instance Variable (or ivar): a specific piece of data belonging to an object.
Total 67 Pages
11
OOP Vocabulary
Encapsulation
Keep implementing private and separate from interface
Polymorphism
Different objects, same interface
Inheritance
Hierarchical organization, share code, customize or extend behaviors
Total 67 Pages
12
Objective-C
Strict superset of C
A very simple language, but some new syntax Single inheritance, classes inherit from one and only one superclass Protocols define behavior that cross classes Dynamic runtime Loosely typed, if youd like
Total 67 Pages 13
Mix C with ObjC Or even C++ with ObjC (usually referred to as ObjC++)
Total 67 Pages
14
Message Syntax
[receiver message]; [receiver message:argument]; [receiver message:arg1 andArg: arg2]
Total 67 Pages
15
Terminology
Message expression
[receiver method:argument]
Message
[receiver method:argument]
Selector
[receiver method:argument]
Method
The code selected by a message.
Total 67 Pages
16
Dot Syntax
Objective-C 2.0 introduced dot syntax Convenient shorthand for invoking accessor methods
float height = [person height]; float height = person.height; [person setHeight: newHeight]; person.height = newHeight;
Follows
the dots
Dynamically-typed object
id anObject
Statically-typed object
Person *anObject
Objective-C provides compile-time, not runtime, type checking Objective-C always uses dynamic binding
Total 67 Pages 18
Conceptually similar to function pointer Selectors include the name and all colons, for example:
Total 67 Pages
19
This sort of introspection and dynamic messaging underlies many Cocoa design patterns
-(void)setTarget:(id)target; -(void)setAction:(SEL)action;
Total 67 Pages 20
Total 67 Pages
22
-description
Objects represented in format strings using %@ When an object appears in a format string, it is asked for its description
[NSString stringWithFormat: @The answer is: %@, myObject];
Your custom subclasses can override description to return more specific information
Total 67 Pages 23
Foundation Framework
Foundation Classes
NSObject
String
NSString / NSMutableString
Collection
NSArray / NSMutableArray NSDictionary / NSMutableDictionary NSSet / NSMutableSet NSNumber
Others
NSData / NSMutableData NSDate / NSCalendarDate
Total 67 Pages 24
Total 67 Pages
25
Outlines
Objective-C basics Building an Application Sensor programming (accelerometer, GPS, microphone) Audio Libraries
Total 67 Pages
26
Anatomy of an Application
Compiled code
Your code Frameworks
Nib files
UI elements and other objects Details about object relations
App Lifecycle
Total 67 Pages
28
App Lifecycle
Main function UIApplicationMain which Info.plist to figure out what nib to load. MainWindow.xib contains the connections for our application. AppDelegate ViewController.xib View handle UI events
Total 67 Pages 29
Model-View-Controller
Total 67 Pages
30
Total 67 Pages
31
Model
Manages the app data and state Note concerned with UI or presentation Often persists somewhere Same model should be reusable, unchanged in different interfaces
Total 67 Pages
32
View
Present the Model to the user in an appropriate interface Allows user to manipulate data Does not store any data
Total 67 Pages
33
Controller
Intermediary between Model & View Updates the view when the model changes Updates the model when the user manipulates the view Typically where the app logic lives
Total 67 Pages
34
Outlines
Objective-C basics Building an Application Sensor programming (accelerometer, GPS, microphone, ) Audio Libraries
Total 67 Pages
35
Accelerometer
Total 67 Pages
36
Orientation-Related Changes
UIViewController class
interfaceOrientation property
Total 67 Pages
37
Shake Undo!
UIEvent type
@property(readonly) UIEventType type; @property(readonly) UIEventSubtype subtype; UIEventTypeMotion UIEventSubtypeMotionShake
Total 67 Pages
38
Class
UIAccelerometer UIAcceleration
Protocol
UIAccelerometerDelegate
Total 67 Pages 39
Low-pass filter
Isolates constant acceleration Used to find the device orientation
High-pass filter
Shows instantaneous movement only Used to identify user-initiated movement
Total 67 Pages
41
Total 67 Pages
42
Total 67 Pages
43
Applying Filters
#define FILTERFACTOR 0.1 Value = (newAcceleration * FILTERFACTOR) + (previousValue * (1.0 FILTERFACTOR)); previousValue = value;
lowpassValue = (newAcceleration * FILTERFACTOR) + (previousValue * (1.0 FILTERFACTOR)); previousLowPassValue = lowPassValue; highPassValue = newAcceleration lowPassValue;
Total 67 Pages
44
GPS
Classes
CLLocationManager CLLocation
Protocol
CLLocationManagerDelegate
Total 67 Pages 45
Getting a Location
-(void)initLocationManager{ CLLocationManager* locManager = [[CLLocationManager alloc] init]; locManager.delegate = self; [locManager startUpdatingLocation]; }
-(void)locationManager: (CLLocationManager*)manager didUpdateToLocation: (CLLocation*)newLocation fromLocation: (CLLocation*)oldLocation{ NSTimerInterval howRecent = [newLocation.timestamp timeIntervalSinceNow]; if(howRecent < -10) return;
if(newLocation.horizontalAccuracy > 100) return; //Use the coordinate data. double lat = newLocation.coordinate.latitude; double lon = newLocation.coordinate.longitude; }
Total 67 Pages 46
Getting a Heading
Geographic North
CLLocationDirection trueHeading
Magnetic North
CLLocationDirection magneticHeading
-(void)locationManager: (CLLocationManager *)manager didUpdateHeading:(CLHeading*)newHeading{ //Use the coordinate data. CLLocationDirection heading = newHeading.trueHeading; CLLocationDirection magnetic = newHeading.magneticHeading; }
Total 67 Pages
47
Microphone
Total 67 Pages
48
Outlines
Objective-C basics Building an Application Sensor programming (accelerometer, GPS, microphone) Audio Libraries
Total 67 Pages
49
Audio Libraries
System Sound API short sounds AVAudioPlayer ObjC, simple API Audio Session - Audio Toolbox Audio Queue - Audio Toolbox Audio Units OpenAL MediaPlayer Framework
Total 67 Pages
50
AVAudioPlayer
Play longer sounds (> 5 seconds) Locally stored files or in-memory (no network streaming) Can loop, seek, play, pause Provides metering Play multiple sounds simultaneously Cocoa-style API
Initialize with file URL or data Allows for delegate
Total 67 Pages
51
AVAudioPlayer
AVAudioPlayer *player; NSString *path = [[NSBundle mainBundle] pathForResource: ofType:]; NSURL *url = [NSURL fileURLWithPath:path]; player = [[AVAudioPlayer allow] initWithContentsOfURL:url]; [player prepareToPlay];
Total 67 Pages
52
Audio Sessions
Total 67 Pages
53
Audio Sessions
Total 67 Pages
54
Audio Queue
Audio File Stream Services & Audio Queue Services Supports wider variety of formats Finer grained control over playback
Streaming audio over network Cf: AVAudioPlayer(local)
Audio Units
For serious audio processing Graph-based audio
Rate or format conversion Real time input/output for recording & playback Mixing multiple streams VoIP (Voice over Internet Protocol).
Audio Units
Otherwise, look first at the Media Player, AV Foundation, OpenAL, or Audio Toolbox!
Total 67 Pages
57
Audio Units
Ex: Karaoke app in iPhone real-time input from mic Real-time output to speaker Audio Unit provides excellent responsiveness Audio Unit controls audio flow to do pitch tracking, voice enhancement, iPod equalization, and etc.
Total 67 Pages
58
OpenAL
Buffers: Container for Audio Sources: 3D point emitting Audio Listener: Position where Sources are heard
MediaPlayer Framework
Tell iPod app to play music Access to entire music library
Others
Accelerate Framework
C APIs for vector and matrix math, digital signal processing large number handling, and image processing vDSP programming guide
Bonjour and NSStream The Synthesis ToolKit in C++ (STK) and OSC
Total 67 Pages 61
Total 67 Pages
62
Total 67 Pages
63