Archive for category Music

Musical Notes

Looking at a piano keyboard makes things easier to understand. When I learned how to play music on the violin I had no idea why the notes were named the way they were.

  • Standard is 88 Keys
  • Arranged from lowest frequency on the left side to highest frequency on the right side
  • White keys are normal notes
  • Black keys are sharps and flats, which are named in relation to the white keys
  • There are 12 keys in an octave
  • The octaves repeat and each note is double the frequency of the previous octave

piano

  1. C
  2. C #/ D♭
  3. D
  4. D # / E♭
  5. E
  6. F
  7. F # / G♭
  8. G
  9. G #/ A♭
  10. A
  11. A # / B♭
  12. B

The A above middle C is defined to be 440 Hz. Middle C is the 4th C from the left of the keyboard, which is why it it sometimes referred to as C4.

The frequencies are spaced such that 440 * 2^((i-1)/12), i = 1, …, 12. For i = 1, then you would get A, For i = 13, then it would hit A again in the next octave. MIDI has a way of defining numbers to notes,

MIDI = 69 + 12 log_2(f/440). This sets A440 to be MIDI number 69. Flipping the relation around, we get

f = 440*2^((MIDI – 69)/12)

People’s hearing ranges from 20 Hz to 20,000 Hz, so that’s MIDI 15 to MIDI 135. The piano ranges from MIDI 21 (27.5 Hz) to MIDI 108 (4186 Hz).

MIDI # Frequency (Hz)
15 19.445
16 20.602
17 21.827
18 23.125
19 24.500
20 25.957
21 27.500
22 29.135
23 30.868
24 32.703
25 34.648
26 36.708
27 38.891
28 41.203
29 43.654
30 46.249
31 48.999
32 51.913
33 55.000
34 58.270
35 61.735
36 65.406
37 69.296
38 73.416
39 77.782
40 82.407
41 87.307
42 92.499
43 97.999
44 103.826
45 110.000
46 116.541
47 123.471
48 130.813
49 138.591
50 146.832
51 155.563
52 164.814
53 174.614
54 184.997
55 195.998
56 207.652
57 220.000
58 233.082
59 246.942
60 261.626
61 277.183
62 293.665
63 311.127
64 329.628
65 349.228
66 369.994
67 391.995
68 415.305
69 440.000
70 466.164
71 493.883
72 523.251
73 554.365
74 587.330
75 622.254
76 659.255
77 698.456
78 739.989
79 783.991
80 830.609
81 880.000
82 932.328
83 987.767
84 1046.502
85 1108.731
86 1174.659
87 1244.508
88 1318.510
89 1396.913
90 1479.978
91 1567.982
92 1661.219
93 1760.000
94 1864.655
95 1975.533
96 2093.005
97 2217.461
98 2349.318
99 2489.016
100 2637.020
101 2793.826
102 2959.955
103 3135.963
104 3322.438
105 3520.000
106 3729.310
107 3951.066
108 4186.009
109 4434.922
110 4698.636
111 4978.032
112 5274.041
113 5587.652
114 5919.911
115 6271.927
116 6644.875
117 7040.000
118 7458.620
119 7902.133
120 8372.018
121 8869.844
122 9397.273
123 9956.063
124 10548.082
125 11175.303
126 11839.822
127 12543.854
128 13289.750
129 14080.000
130 14917.240
131 15804.266
132 16744.036
133 17739.688
134 18794.545
135 19912.127

No Comments

Old school Nokia ringtones

I found some really old code today. In the early days of the cellphone before middle schoolers and elementary schoolers had cellphones, the ringtones for Nokia phones were input using an ancient code.

Here’s the zelda theme, which I scrapped off a website a long time ago.

4#a1, 4.f1, 8#a1, 16#a1, 16c2, 16d2, 16#d2, 2f2, 8-, 8f2, 16.f2, 16#f2, 16.#g2,
2.#a2, 16.#a2, 16#g2, 16.#f2, 8.#g2, 16.#f2, 2f2, 4f2, 8#d2, 16#d2, 16f2, 2#f2,
8f2, 8#d2, 8#c2, 16#c2, 16#d2, 2f2, 8#d2, 8#c2, 8c2, 16c2, 16d2, 2e2, 4g2, 8f2,
16f1, 16f1, 8f1

Here’s some MATLAB/OCTAVE code

function nokia2wav (filename, tempo)
Fs = 44.1e3;
if (nargin == 0)
  filename = 'zelda.txt';
end
if (nargin < 2)
  tempo = 125;
end

% read in file and turn it into a long string
fid = fopen(filename);
notebuf = '';
while feof(fid) == 0
   notebuf = [notebuf fgetl(fid)];
end
fclose(fid);

% convert to wav
y = NOKIAtoWAV(notebuf, tempo, Fs);

% write wav file
[buf rem] = strtok(filename, '.');
wavfilename = [buf '.wav'];
wavwrite(y', Fs, wavfilename);

function y = NOKIAtoWAV (str, tempo, Fs)
%NOKIATOWAV converts a string of nokia text into a wavefile
%
% y = NOKIAtoWAV (str, tempo, Fs)
%
% input:
%   str - nokia text string
%   tempo - beats per minute
%   Fs - sampling frequency
%
% output:
%   y - waveform
%

% parse the string to individual NOKIA notes
notes = words(str,',');

% convert each NOKIA note into a frequency and duration
for m = 1:length(notes)
    [midi(m) t(m)] = analyzeNOKIAnote(notes(m,:), tempo);
end

% massage data
midi = midi';
t = t';

% assignment allocation
nend = length(midi);
y = 0;
p = 0;

% keep adding next note to waveform
for n = 1:nend
    y = [y generatenote(MIDItoFreq(midi(n)), t(n), p, Fs)];
    p = asin(y(length(y)));
end

%------------------------------------------------------------------------------
% String Tokenizer
%------------------------------------------------------------------------------

function allWords = words(inputString, delim)
%WORDS parses string by delimiter
%
% allWords = words(inputString, delim)
%
% input:
%   inputString - input string
%   delim - delimiters
% output:
%   allwords - array of parsed strings
%

    remainder = inputString;
    allWords = '';
while (any(remainder))
    [chopped,remainder] = strtok(remainder, delim);
    allWords = strvcat(allWords, chopped);
end

%-------------------------------------------------------------------------------
% Turn NOKIA string into freq and duration
%-------------------------------------------------------------------------------

function [midi t] = analyzeNOKIAnote(str, tempo)
%analyzeNOKIAnote
%
% [midi t] = analyzeNOKIAnote(str, tempo)
%
% input:
%   str - NOKIA note
%   tempo - beats per minute
%
% output:
%   midi - frequency
%   t - duration
%

% split duration and frequency
[duration note] = strtok(str,'abcdefg#-');
t0 = 4*60/tempo;

% compute duration
[duration onehalf] = strtok(duration,'.');
t = t0*1/str2double(duration);
midi = 100;
if(length(onehalf));
    t = 1.5*t;
end

% compute frequency
note = strtrim(note);
if strcmp(note,'c1')
    midi = 60;
elseif strcmp(note,'#c1');
    midi = 61;
elseif strcmp(note,'d1');
    midi = 62;
elseif strcmp(note,'#d1');
    midi = 63;
elseif strcmp(note,'e1');
    midi = 64;
elseif strcmp(note,'f1');
    midi = 65;
elseif strcmp(note,'#f1');
    midi = 66;
elseif strcmp(note,'g1');
    midi = 67;
elseif strcmp(note,'#g1');
    midi = 68;
elseif strcmp(note,'a1');
    midi = 69;
elseif strcmp(note,'#a1');
    midi = 70;
elseif strcmp(note,'b1');
    midi = 71;
elseif strcmp(note,'c2');
    midi = 72;
elseif strcmp(note,'#c2');
    midi = 73;
elseif strcmp(note,'d2');
    midi = 74;
elseif strcmp(note,'#d2');
    midi = 75;
elseif strcmp(note,'e2');
    midi = 76;
elseif strcmp(note,'f2');
    midi = 77;
elseif strcmp(note,'#f2');
    midi = 78;
elseif strcmp(note,'g2');
    midi = 79;
elseif strcmp(note,'#g2');
    midi = 80;
elseif strcmp(note,'a2');
    midi = 81;
elseif strcmp(note,'#a2');
    midi = 82;
elseif strcmp(note,'b2');
    midi = 83;
elseif strcmp(note,'-');
    midi = 0;
else
    note;
end
midi = midi;

function f = MIDItoFreq(MIDInum)
%MIDItoFreq gets the frequency of a MIDI note number
%
%   f = MIDItoFreq(MIDInum)
%
%   input:
%       MIDInum - MIDI note number
%
%   output:
%       f - frequency of MIDI note number
%

f = 440*(2^(1/12).^(MIDInum-69));

function y = generatenote(f, t, p, Fs)
%generatenote generates a wav of a certain frequency and duration
%
%   y = generatenote(f, t, p, Fs)
%
%   input:
%       f - frequency
%       t - duration
%       p - phase shift
%       Fs - sampling frequency (usually 44.1 kHz for audio)
%
%   output:
%       y - waveform
%

% t in seconds
t = 0:Fs*t;
y = sin(2*pi*f/Fs*t+p);
% add fade to eliminate pops
x0 = round(10e-3*Fs);
y(end-x0+1:end) = y(end-x0+1:end).*linspace(1,0,x0);

No Comments