Permalink
Browse files

First Commit

  • Loading branch information...
0 parents commit c842ec81edf190810c6d3aaff9c313fae10e9a08 @coryalder committed Nov 28, 2010
@@ -0,0 +1 @@
+*.pbxproj -crlf -diff -merge
@@ -0,0 +1,18 @@
+# xcode noise
+*.mode1v3
+*.pbxuser
+*.perspective
+*.perspectivev3
+*.pyc
+*~.nib/
+build/*
+
+# Textmate - if you build your xcode projects with it
+*.tm_build_errors
+
+# old skool
+.svn
+
+# osx noise
+.DS_Store
+profile
@@ -0,0 +1,22 @@
+//
+// SineWaverAppDelegate.h
+// SineWaver
+//
+// Created by Cory Alder on 10-11-28.
+// Copyright 2010 Davander Mobile. All rights reserved.
+//
+
+#import <UIKit/UIKit.h>
+
+@class SineWaverViewController;
+
+@interface SineWaverAppDelegate : NSObject <UIApplicationDelegate> {
+ UIWindow *window;
+ SineWaverViewController *viewController;
+}
+
+@property (nonatomic, retain) IBOutlet UIWindow *window;
+@property (nonatomic, retain) IBOutlet SineWaverViewController *viewController;
+
+@end
+
@@ -0,0 +1,88 @@
+//
+// SineWaverAppDelegate.m
+// SineWaver
+//
+// Created by Cory Alder on 10-11-28.
+// Copyright 2010 Davander Mobile. All rights reserved.
+//
+
+#import "SineWaverAppDelegate.h"
+#import "SineWaverViewController.h"
+
+@implementation SineWaverAppDelegate
+
+@synthesize window;
+@synthesize viewController;
+
+
+#pragma mark -
+#pragma mark Application lifecycle
+
+- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
+
+ // Override point for customization after application launch.
+
+ // Add the view controller's view to the window and display.
+ [self.window addSubview:viewController.view];
+ [self.window makeKeyAndVisible];
+
+ return YES;
+}
+
+
+- (void)applicationWillResignActive:(UIApplication *)application {
+ /*
+ Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
+ Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
+ */
+}
+
+
+- (void)applicationDidEnterBackground:(UIApplication *)application {
+ /*
+ Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
+ If your application supports background execution, called instead of applicationWillTerminate: when the user quits.
+ */
+}
+
+
+- (void)applicationWillEnterForeground:(UIApplication *)application {
+ /*
+ Called as part of transition from the background to the inactive state: here you can undo many of the changes made on entering the background.
+ */
+}
+
+
+- (void)applicationDidBecomeActive:(UIApplication *)application {
+ /*
+ Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
+ */
+}
+
+
+- (void)applicationWillTerminate:(UIApplication *)application {
+ /*
+ Called when the application is about to terminate.
+ See also applicationDidEnterBackground:.
+ */
+}
+
+
+#pragma mark -
+#pragma mark Memory management
+
+- (void)applicationDidReceiveMemoryWarning:(UIApplication *)application {
+ /*
+ Free up as much memory as possible by purging cached data objects that can be recreated (or reloaded from disk) later.
+ */
+}
+
+
+- (void)dealloc {
+ [viewController release];
+ [window release];
+ [super dealloc];
+}
+
+
+@end
@@ -0,0 +1,38 @@
+//
+// SineWaverViewController.h
+// SineWaver
+//
+// Created by Cory Alder on 10-11-28.
+// Copyright 2010 Davander Mobile. All rights reserved.
+//
+
+#import <UIKit/UIKit.h>
+#import <Foundation/Foundation.h>
+#import <AudioUnit/AudioUnit.h>
+
+@interface SineWaverViewController : UIViewController {
+ AudioComponentInstance audioUnit;
+ float fr1;
+ float fr2;
+ float fr3;
+ float fr4;
+ UILabel *LB1;
+}
+
+
+-(void)initAudio; // put this in init?
+
+
+-(void)start;
+-(void)stop;
+
+-(IBAction)valueChanged:(id)sender;
+
+@property (retain, nonatomic) IBOutlet UILabel *LB1;
+@property float fr1;
+@property float fr2;
+@property float fr3;
+@property float fr4;
+
+@end
+
@@ -0,0 +1,209 @@
+//
+// SineWaverViewController.m
+// SineWaver
+//
+// Created by Cory Alder on 10-11-28.
+// Copyright 2010 Davander Mobile. All rights reserved.
+//
+
+#import "SineWaverViewController.h"
+#import <AudioUnit/AudioUnit.h>
+#import <math.h>
+
+
+#define kOutputBus 0
+
+#define kSampleRate 44100
+//44100.0f
+#define kWaveform (M_PI * 2.0f / kSampleRate)
+
+OSStatus playbackCallback(void *inRefCon,
+ AudioUnitRenderActionFlags *ioActionFlags,
+ const AudioTimeStamp *inTimeStamp,
+ UInt32 inBusNumber,
+ UInt32 inNumberFrames,
+ AudioBufferList *ioData) {
+
+ SineWaverViewController *me = (SineWaverViewController *)inRefCon;
+
+ static int phase = 1;
+
+ for(UInt32 i = 0; i < ioData->mNumberBuffers; i++) {
+
+ int samples = ioData->mBuffers[i].mDataByteSize / sizeof(SInt16);
+
+ SInt16 values[samples];
+
+ float waves;
+ float volume=.5;
+
+ for(int j = 0; j < samples; j++) {
+
+
+ waves = 0;
+
+
+ waves += sin(kWaveform * me.fr1 * phase)*volume;
+ waves += sin(kWaveform * me.fr2 * phase)*volume;
+ waves += sin(kWaveform * me.fr3 * phase)*volume;
+ waves += sin(kWaveform * me.fr4 * phase)*volume;
+
+ waves *= 32500 / 4; // <--------- make sure to divide by how many waves you're stacking
+
+ values[j] = (SInt16)waves;
+ values[j] += values[j]<<16;
+
+ phase++;
+
+ }
+
+ memcpy(ioData->mBuffers[i].mData, values, samples * sizeof(SInt16));
+
+ }
+
+
+ return noErr;
+
+}
+
+
+@implementation SineWaverViewController
+
+@synthesize fr1, fr2, fr3, fr4, LB1;
+
+
+/*
+// The designated initializer. Override to perform setup that is required before the view is loaded.
+- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
+ self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
+ if (self) {
+ // Custom initialization
+ }
+ return self;
+}
+*/
+
+/*
+// Implement loadView to create a view hierarchy programmatically, without using a nib.
+- (void)loadView {
+}
+*/
+
+
+
+// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
+- (void)viewDidLoad {
+ [super viewDidLoad];
+ self.fr1 = 500;
+ self.fr2 = 400;
+ self.fr3 = 300;
+ [self initAudio];
+ [self performSelector:@selector(start) withObject:nil afterDelay:2];
+}
+
+-(IBAction)valueChanged:(id)sender {
+ switch ([sender tag]) {
+ case 1:
+ self.fr1 = [(UISlider *)sender value];
+ break;
+ case 2:
+ self.fr2 = [(UISlider *)sender value];
+ break;
+ case 3:
+ self.fr3 = [(UISlider *)sender value];
+ break;
+ case 4:
+ self.fr4 = [(UISlider *)sender value];
+ break;
+ default:
+ break;
+ }
+ NSLog(@"changing value of item %i to %0.1f", [(UISlider *)sender tag], [(UISlider *)sender value]);
+}
+
+/*
+// Override to allow orientations other than the default portrait orientation.
+- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
+ // Return YES for supported orientations
+ return (interfaceOrientation == UIInterfaceOrientationPortrait);
+}
+*/
+
+- (void)didReceiveMemoryWarning {
+ // Releases the view if it doesn't have a superview.
+ [super didReceiveMemoryWarning];
+
+ // Release any cached data, images, etc that aren't in use.
+}
+
+- (void)viewDidUnload {
+ // Release any retained subviews of the main view.
+ // e.g. self.myOutlet = nil;
+}
+
+
+-(void)initAudio {
+
+ OSStatus status;
+
+ AudioComponentDescription desc;
+ desc.componentType = kAudioUnitType_Output;
+ desc.componentSubType = kAudioUnitSubType_RemoteIO;
+ desc.componentFlags = 0;
+ desc.componentFlagsMask = 0;
+ desc.componentManufacturer = kAudioUnitManufacturer_Apple;
+
+ AudioComponent outputComponent = AudioComponentFindNext(NULL, &desc);
+
+ status = AudioComponentInstanceNew(outputComponent, &audioUnit);
+
+ UInt32 flag = 1;
+ status = AudioUnitSetProperty(audioUnit, kAudioOutputUnitProperty_EnableIO, kAudioUnitScope_Output, kOutputBus, &flag, sizeof(flag));
+
+ AudioStreamBasicDescription audioFormat;
+ audioFormat.mSampleRate = kSampleRate;
+ audioFormat.mFormatID = kAudioFormatLinearPCM;
+ audioFormat.mFormatFlags = kAudioFormatFlagIsSignedInteger | kAudioFormatFlagIsPacked;
+ audioFormat.mFramesPerPacket = 1;
+ audioFormat.mChannelsPerFrame = 1;
+ audioFormat.mBitsPerChannel = 16;
+ audioFormat.mBytesPerPacket = 2;
+ audioFormat.mBytesPerFrame = 2;
+
+ status = AudioUnitSetProperty(audioUnit, kAudioUnitProperty_StreamFormat, kAudioUnitScope_Input, kOutputBus, &audioFormat, sizeof(audioFormat));
+
+ AURenderCallbackStruct callbackStruct;
+ callbackStruct.inputProc = playbackCallback;
+ callbackStruct.inputProcRefCon = self;
+
+ status = AudioUnitSetProperty(audioUnit, kAudioUnitProperty_SetRenderCallback, kAudioUnitScope_Global, kOutputBus, &callbackStruct, sizeof(callbackStruct));
+
+ status = AudioUnitInitialize(audioUnit);
+
+}
+
+-(void)start {
+
+ OSStatus status;
+
+ status = AudioOutputUnitStart(audioUnit);
+
+}
+
+-(void)stop {
+
+ OSStatus status;
+
+ status = AudioOutputUnitStop(audioUnit);
+
+}
+
+-(void)dealloc {
+
+ AudioUnitUninitialize(audioUnit);
+
+ [super dealloc];
+
+}
+
+@end
Oops, something went wrong.

0 comments on commit c842ec8

Please sign in to comment.