[Tutor] zero crossing pitch detection
Steven D'Aprano
steve at pearwood.info
Sun May 29 18:14:59 CEST 2011
김태윤 wrote:
> hello
> I made a simple recording / pitch detecting program
> but it seems work weird way
> when I record "a~~~~~" then
> this program printing
>
> (I am not a singer and I can't go up and down G0 and C3)
I'm afraid that most of your post just went over my head. I have no idea
what these G0 and C3 things are! But I wanted to make a couple of
comments about your Python code:
> for i in range(0, framelength):
There is no need to say range(0, framelength) because range defaults to
starting at zero. Instead, you should say range(framelength)
> twobyte = [ord(j) for j in data[i:i+2]]
> if twobyte[0]==0x10 and twobyte[1]==0x0:
A better way of making that test is with:
if twobyte == [0x10, 0x0]:
[...]
> note = "none"
> if f2hz <16.835 and f2hz >15.865: note = 'C0'
> if f2hz <17.835 and f2hz >16.805: note = 'CS0orDb0'
> if f2hz <18.9 and f2hz >17.8: note = 'D0'
You have a long series of tests like this. For the sake of readability
and maintainability, you should factor that out into a separate function.
Also, once you have found the correct note, surely there is no need to
continue testing? Use if...elif...elif... rather than if... if... if...
Also, you can simplify the range tests by writing them like this:
if 15.865 < f2hz < 16.835: note = 'C0'
elif 16.805 < f2hz < 17.835: note = 'CS0orDb0'
elif 17.8 < f2hz < 18.9: note = 'D0'
elif ...
Notice that the ranges overlap. Is that intended? I would imagine not,
but perhaps I'm wrong.
--
Steven
More information about the Tutor
mailing list