Created
July 28, 2011 16:02
-
-
Save kybernetyk/1111821 to your computer and use it in GitHub Desktop.
automatic music composer with rand()
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// main.m | |
// iKurzweil | |
// | |
// Created by Jaroslaw Szpilewski on 28/7/11. | |
// Copyright 2011 Flux Forge. All rights reserved. | |
// | |
#import <Foundation/Foundation.h> | |
#define NOTE_A 440.0 | |
double ratios[] = { | |
(1.0/2.0), | |
(4.0/3.0), | |
(3.0/4.0), | |
(1.0/5.0), | |
(2.0/1.0), | |
}; | |
double give_funky_note(double seed) | |
{ | |
size_t s = sizeof(ratios)/sizeof(double); | |
int r = rand()%s; | |
return seed * ratios[r]; | |
} | |
NSString *note_name_for_frequency(double freq) | |
{ | |
NSString *ret = nil; | |
/* anders: | |
1. note rauskriegen mit modulo von grundnote | |
2. oktave mit div von grundoktave | |
*/ | |
int r = freq; | |
if (r == 88) | |
return @"F-2"; | |
if (r == 220) | |
return @"A-3"; | |
if (r == 247) | |
return @"B-3"; | |
if (r >= 260 && r <= 264) | |
return @"C-4"; | |
if (r == 293) | |
return @"D-4"; | |
if (r == 330) | |
return @"E-4"; | |
if (r == 371) | |
return @"F#4"; | |
if (r == 391) | |
return @"G-4"; | |
if (r == 440) | |
return @"A-4"; | |
if (r == 495) | |
return @"B-4"; | |
if (r == 521) | |
return @"C-5"; | |
if (r == 586) | |
return @"D-5"; | |
if (r == 660) | |
return @"E-5"; | |
if (r == 695) | |
return @"F-5"; | |
if (r == 782) | |
return @"G-5"; | |
if (r == 880) | |
return @"A-5"; | |
if (r == 990) | |
return @"B-5"; | |
if (r == 1042) | |
return @"C-6"; | |
if (r == 1173) | |
return @"D-6"; | |
if (r == 1320) | |
return @"E-6"; | |
return ret; | |
} | |
int main (int argc, const char * argv[]) | |
{ | |
@autoreleasepool { | |
srand(time(0)); | |
double seed_note = give_funky_note(NOTE_A); | |
for (int i = 0; i < 32; i++) { | |
printf("[%i]\t%.2f = %s\n",i, seed_note, [note_name_for_frequency(seed_note) cStringUsingEncoding: NSUTF8StringEncoding]); | |
for (;;) { | |
int prev = seed_note; | |
seed_note = give_funky_note(seed_note); | |
if ((int)seed_note != prev) // uncomment to disable n same notes in a row | |
break; | |
} | |
if (seed_note < NOTE_A/2.0) | |
seed_note = NOTE_A; | |
if (seed_note > NOTE_A*3.0) | |
seed_note = NOTE_A; | |
} | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment