Music to spectrogram on iOS with the accelerate framework
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.
 

183 lines
4.8 KiB

//
// ViewController.m
// SpectogramPrototype
//
// Created by Joshua Moerman on 21/12/13.
// Copyright (c) 2013 Joshua Moerman. All rights reserved.
//
@import MediaPlayer.MPMediaPickerController;
#import "ViewController.h"
#import "RuledScrollView.h"
#import "Spectographer.h"
#import "Ruler.h"
typedef enum {
iPad,
iPod,
Simulator
} device;
// NOTE: the simulator has no media, so we cant use MPMediaPicker
// and the iPad has to use a popover, while the iPod/iPhone doesn't
// So that's why we check the device
device get_device(){
device device = iPad;
NSRange range = [[[UIDevice currentDevice] model] rangeOfString:@"Simulator"];
if(range.location != NSNotFound) {
device = Simulator;
}
if([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone){
device = iPod;
}
return device;
}
@interface ViewController () <MPMediaPickerControllerDelegate> {
Spectographer * spectographer;
UIPopoverController * pop;
NSMutableArray * slices;
}
- (void)openAudioFile:(NSURL*)filePath;
- (void)addSlice:(UIImageView*)view;
- (void)removeAllSlices;
@property (nonatomic, strong) UILabel* header;
@end
@implementation ViewController
@synthesize scrollView, contentView, header;
- (void)viewDidLoad{
[super viewDidLoad];
if(!contentView){
[[NSBundle mainBundle] loadNibNamed:@"ContentView" owner:self options:nil];
[scrollView.content addSubview:contentView];
contentView.autoresizingMask = UIViewAutoresizingNone;
scrollView.contentSize = contentView.bounds.size;
}
if(!slices){
slices = [[NSMutableArray alloc] init];
}
}
- (void)didReceiveMemoryWarning{
[super didReceiveMemoryWarning];
}
- (void)openAudioFile:(NSURL *)filePath{
[self removeAllSlices];
[self resetView:nil];
if(!spectographer)
spectographer = [[Spectographer alloc] init];
[spectographer openAudioFile:filePath];
[spectographer generate:^void(CGImageRef image){
CGImageRetain(image);
dispatch_async(dispatch_get_main_queue(), ^{
UIImage * newUIImage = [UIImage imageWithCGImage:image];
assert(newUIImage);
CGImageRelease(image);
UIImageView * view = [[UIImageView alloc] initWithImage:newUIImage];
[self addSlice:view];
});
}];
}
- (void)addSlice:(UIImageView *)view{
if(slices.count){
UIImageView * lastSlice = slices.lastObject;
CGFloat x = CGRectGetMaxX(lastSlice.frame);
view.frame = CGRectOffset(view.frame, x, 0);
} // else leave the origin at 0
[contentView addSubview:view];
[slices addObject:view];
view.layer.opacity = 0;
[UIView animateWithDuration:0.5 animations:^{
view.layer.opacity = 1;
}];
UIImageView * lastSlice = slices.lastObject;
scrollView.contentSize = CGSizeMake(CGRectGetMaxX(lastSlice.frame), CGRectGetMaxY(lastSlice.frame));
}
- (void)removeAllSlices{
for(UIView * view in slices){
[view removeFromSuperview];
}
[slices removeAllObjects];
scrollView.contentSize = CGSizeZero;
}
- (void)resetView:(id)__unused sender{
scrollView.zoomScale = 1.0;
UIImageView * lastSlice = slices.lastObject;
scrollView.contentSize = CGSizeMake(CGRectGetMaxX(lastSlice.frame), CGRectGetMaxY(lastSlice.frame));
}
- (void)mediaButtonPressed:(UIButton*)sender{
MPMediaPickerController * mppc = nil;
device device = get_device();
switch (device) {
case iPad:
mppc = [[MPMediaPickerController alloc] initWithMediaTypes:MPMediaTypeAnyAudio];
mppc.delegate = self;
mppc.allowsPickingMultipleItems = NO;
mppc.showsCloudItems = NO;
if(pop){
[pop dismissPopoverAnimated:YES];
pop = nil;
}
pop = [[UIPopoverController alloc] initWithContentViewController:mppc];
[pop presentPopoverFromRect:sender.frame inView:self.view permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
break;
case iPod:
mppc = [[MPMediaPickerController alloc] initWithMediaTypes:MPMediaTypeAnyAudio];
mppc.delegate = self;
mppc.allowsPickingMultipleItems = NO;
mppc.showsCloudItems = NO;
[self presentViewController:mppc animated:YES completion:nil];
break;
case Simulator:
NSLog(@"WARNING: No MPMediaPicker in Simulator, opening some file");
[self openAudioFile:[[NSBundle mainBundle] URLForResource:@"testMusic" withExtension:@".mp3"]];
break;
}
}
- (void)mediaPicker:(MPMediaPickerController *)mediaPicker didPickMediaItems:(MPMediaItemCollection *)mediaItemCollection {
for(MPMediaItem * mediaItem in mediaItemCollection.items){
NSURL * url = [mediaItem valueForProperty:MPMediaItemPropertyAssetURL];
[self openAudioFile:url];
}
[pop dismissPopoverAnimated:YES];
pop = nil;
[mediaPicker dismissViewControllerAnimated:YES completion:nil];
}
- (void)mediaPickerDidCancel:(MPMediaPickerController *)mediaPicker {
[pop dismissPopoverAnimated:YES];
pop = nil;
[mediaPicker dismissViewControllerAnimated:YES completion:nil];
}
- (void)tuningChanged:(UISlider*)sender{
[(VerticalRuler*)scrollView.leftRuler setTuning:sender.value];
}
@end