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.
 

110 lines
2.8 KiB

//
// ViewController.m
// SpectogramPrototype
//
// Created by Joshua Moerman on 21/12/13.
// Copyright (c) 2013 Joshua Moerman. All rights reserved.
//
#import <MediaPlayer/MPMediaPickerController.h>
#import <AudioToolbox/ExtendedAudioFile.h>
#import "ViewController.h"
#import "RuledScrollView.h"
#import "FFTTest.h"
@interface ViewController () <MPMediaPickerControllerDelegate> {
UIPopoverController * pop;
FFTTest * fft_handler;
}
- (void) openAudioFile:(NSURL*)filePath;
@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;
}
}
- (void)didReceiveMemoryWarning{
[super didReceiveMemoryWarning];
}
- (void)calculateButtonPressed:(id)sender{
if(!fft_handler)
fft_handler = [[FFTTest alloc] init];
unsigned int size = fft_handler.acceptedSize;
float * data = calloc(size, sizeof(float));
[fft_handler inPlaceFFT:data forSize:size];
free(data);
}
- (void)openAudioFile:(NSURL *)filePath{
OSStatus err;
ExtAudioFileRef audioFile = NULL;
err = ExtAudioFileOpenURL((__bridge CFURLRef) filePath, &audioFile);
assert(err == 0);
AudioBufferList bufferList;
bufferList.mNumberBuffers = 1;
bufferList.mBuffers[0].mNumberChannels = 2;
bufferList.mBuffers[0].mDataByteSize = 32768;
bufferList.mBuffers[0].mData = calloc(32768, 1);
while(true){
bufferList.mBuffers[0].mDataByteSize = 32768;
UInt32 nFrames = 32768 / 6;
err = ExtAudioFileRead(audioFile, &nFrames, &bufferList);
assert(err == 0);
if(!nFrames) break;
}
NSLog(@"Read some bits");
err = ExtAudioFileDispose(audioFile);
assert(err == 0);
}
- (void)mediaButtonPressed:(UIButton*)sender{
MPMediaPickerController * 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];
}
- (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;
}
- (void)mediaPickerDidCancel:(MPMediaPickerController *)mediaPicker {
[pop dismissPopoverAnimated:YES];
pop = nil;
}
@end