Showing the changes to python/lib (2 of 28 files). Show all
Unified · Split
| @@ -101,6 +101,8 @@ def parser_add_subcommand_notes(subparsers): · 100 unmodified lines | |||
| 101 | help='estimate midi-like notes (monophonic)') | 101 | help='estimate midi-like notes (monophonic)') |
| 102 | subparser.add_input() | 102 | subparser.add_input() |
| 103 | subparser.add_buf_hop_size() | 103 | subparser.add_buf_hop_size() |
| 104 | subparser.add_silence() | ||
| 105 | subparser.add_release_drop() | ||
| 104 | subparser.add_time_format() | 106 | subparser.add_time_format() |
| 105 | subparser.add_verbose_help() | 107 | subparser.add_verbose_help() |
| 106 | subparser.set_defaults(process=process_notes) | 108 | subparser.set_defaults(process=process_notes) |
| @@ -207,6 +209,12 @@ class AubioArgumentParser(argparse.ArgumentParser): · 100 unmodified lines | |||
| 207 | action="store", dest="silence", default=-70, | 209 | action="store", dest="silence", default=-70, |
| 208 | help="silence threshold") | 210 | help="silence threshold") |
| 209 | 211 | ||
| 212 | def add_release_drop(self): | ||
| 213 | self.add_argument("-d", "--release-drop", | ||
| 214 | metavar = "<value>", type=float, | ||
| 215 | action="store", dest="release_drop", default=10, | ||
| 216 | help="release drop threshold") | ||
| 217 | |||
| 210 | def add_minioi(self, default="12ms"): | 218 | def add_minioi(self, default="12ms"): |
| 211 | self.add_argument("-M", "--minioi", | 219 | self.add_argument("-M", "--minioi", |
| 212 | metavar = "<value>", type=str, | 220 | metavar = "<value>", type=str, |
| @@ -382,6 +390,10 @@ class process_notes(default_process): · 169 unmodified lines | |||
| 382 | def __init__(self, args): | 390 | def __init__(self, args): |
| 383 | self.parse_options(args, self.valid_opts) | 391 | self.parse_options(args, self.valid_opts) |
| 384 | self.notes = aubio.notes(**self.options) | 392 | self.notes = aubio.notes(**self.options) |
| 393 | if args.silence is not None: | ||
| 394 | self.notes.set_silence(args.silence) | ||
| 395 | if args.release_drop is not None: | ||
| 396 | self.notes.set_release_drop(args.release_drop) | ||
| 385 | super(process_notes, self).__init__(args) | 397 | super(process_notes, self).__init__(args) |
| 386 | def __call__(self, block): | 398 | def __call__(self, block): |
| 387 | return self.notes(block) | 399 | return self.notes(block) |
| @@ -502,7 +514,24 @@ class process_cut(process_onset): · 114 unmodified lines | |||
| 502 | 514 | ||
| 503 | def main(): | 515 | def main(): |
| 504 | parser = aubio_parser() | 516 | parser = aubio_parser() |
| 505 | args = parser.parse_args() | 517 | if sys.version_info[0] != 3: |
| 518 | # on py2, create a dummy ArgumentParser to workaround the | ||
| 519 | # optional subcommand issue. See https://bugs.python.org/issue9253 | ||
| 520 | # This ensures that: | ||
| 521 | # - version string is shown when only '-V' is passed | ||
| 522 | # - help is printed if '-V' is passed with any other argument | ||
| 523 | # - any other argument get forwarded to the real parser | ||
| 524 | parser_root = argparse.ArgumentParser(add_help=False) | ||
| 525 | parser_root.add_argument('-V', '--version', help="show version", | ||
| 526 | action="store_true", dest="show_version") | ||
| 527 | args, extras = parser_root.parse_known_args() | ||
| 528 | if args.show_version == False: # no -V, forward to parser | ||
| 529 | args = parser.parse_args(extras, namespace=args) | ||
| 530 | elif len(extras) != 0: # -V with other arguments, print help | ||
| 531 | parser.print_help() | ||
| 532 | sys.exit(1) | ||
| 533 | else: # in py3, we can simply use parser directly | ||
| 534 | args = parser.parse_args() | ||
| 506 | if 'show_version' in args and args.show_version: | 535 | if 'show_version' in args and args.show_version: |
| 507 | sys.stdout.write('aubio version ' + aubio.version + '\n') | 536 | sys.stdout.write('aubio version ' + aubio.version + '\n') |
| 508 | sys.exit(0) | 537 | sys.exit(0) |
| @@ -1,9 +1,11 @@ | |||
| 1 | # -*- coding: utf-8 -*- | 1 | # -*- coding: utf-8 -*- |
| 2 | """ utilities to convert midi note number to and from note names """ | 2 | """ utilities to convert midi note number to and from note names """ |
| 3 | 3 | ||
| 4 | __all__ = ['note2midi', 'midi2note', 'freq2note'] | 4 | __all__ = ['note2midi', 'midi2note', 'freq2note', 'note2freq'] |
| 5 | 5 | ||
| 6 | import sys | 6 | import sys |
| 7 | from ._aubio import freqtomidi, miditofreq | ||
| 8 | |||
| 7 | py3 = sys.version_info[0] == 3 | 9 | py3 = sys.version_info[0] == 3 |
| 8 | if py3: | 10 | if py3: |
| 9 | str_instances = str | 11 | str_instances = str |
| @@ -63,10 +65,11 @@ def note2midi(note): · 53 unmodified lines | |||
| 63 | } | 65 | } |
| 64 | _valid_octaves = range(-1, 10) | 66 | _valid_octaves = range(-1, 10) |
| 65 | if not isinstance(note, str_instances): | 67 | if not isinstance(note, str_instances): |
| 66 | raise TypeError("a string is required, got %s (%s)" % (note, str(type(note)))) | 68 | msg = "a string is required, got {:s} ({:s})" |
| 69 | raise TypeError(msg.format(str(type(note)), repr(note))) | ||
| 67 | if len(note) not in range(2, 5): | 70 | if len(note) not in range(2, 5): |
| 68 | raise ValueError("string of 2 to 4 characters expected, got %d (%s)" \ | 71 | msg = "string of 2 to 4 characters expected, got {:d} ({:s})" |
| 69 | % (len(note), note)) | 72 | raise ValueError(msg.format(len(note), note)) |
| 70 | notename, modifier, octave = [None]*3 | 73 | notename, modifier, octave = [None]*3 |
| 71 | 74 | ||
| 72 | if len(note) == 4: | 75 | if len(note) == 4: |
| @@ -90,7 +93,8 @@ def note2midi(note): · 17 unmodified lines | |||
| 90 | if octave not in _valid_octaves: | 93 | if octave not in _valid_octaves: |
| 91 | raise ValueError("%s is not a valid octave" % octave) | 94 | raise ValueError("%s is not a valid octave" % octave) |
| 92 | 95 | ||
| 93 | midi = 12 + octave * 12 + _valid_notenames[notename] + _valid_modifiers[modifier] | 96 | midi = 12 + octave * 12 + _valid_notenames[notename] \ |
| 97 | + _valid_modifiers[modifier] | ||
| 94 | if midi > 127: | 98 | if midi > 127: |
| 95 | raise ValueError("%s is outside of the range C-2 to G8" % note) | 99 | raise ValueError("%s is outside of the range C-2 to G8" % note) |
| 96 | return midi | 100 | return midi |
| @@ -129,8 +133,10 @@ def midi2note(midi): · 32 unmodified lines | |||
| 129 | if not isinstance(midi, int_instances): | 133 | if not isinstance(midi, int_instances): |
| 130 | raise TypeError("an integer is required, got %s" % midi) | 134 | raise TypeError("an integer is required, got %s" % midi) |
| 131 | if midi not in range(0, 128): | 135 | if midi not in range(0, 128): |
| 132 | raise ValueError("an integer between 0 and 127 is excepted, got %d" % midi) | 136 | msg = "an integer between 0 and 127 is excepted, got {:d}" |
| 133 | _valid_notenames = ['C', 'C#', 'D', 'D#', 'E', 'F', 'F#', 'G', 'G#', 'A', 'A#', 'B'] | 137 | raise ValueError(msg.format(midi)) |
| 138 | _valid_notenames = ['C', 'C#', 'D', 'D#', 'E', 'F', 'F#', 'G', 'G#', | ||
| 139 | 'A', 'A#', 'B'] | ||
| 134 | return _valid_notenames[midi % 12] + str(int(midi / 12) - 1) | 140 | return _valid_notenames[midi % 12] + str(int(midi / 12) - 1) |
| 135 | 141 | ||
| 136 | def freq2note(freq): | 142 | def freq2note(freq): |
| @@ -153,5 +159,28 @@ def freq2note(freq): · 16 unmodified lines | |||
| 153 | >>> aubio.freq2note(220.1) | 159 | >>> aubio.freq2note(220.1) |
| 154 | 'A3' | 160 | 'A3' |
| 155 | """ | 161 | """ |
| 156 | from aubio import freqtomidi | 162 | nearest_note = int(freqtomidi(freq) + .5) |
| 157 | return midi2note(int(freqtomidi(freq))) | 163 | return midi2note(nearest_note) |
| 164 | |||
| 165 | def note2freq(note): | ||
| 166 | """Convert note name to corresponding frequency, in Hz. | ||
| 167 | |||
| 168 | Parameters | ||
| 169 | ---------- | ||
| 170 | note : str | ||
| 171 | input note name | ||
| 172 | |||
| 173 | Returns | ||
| 174 | ------- | ||
| 175 | freq : float [0, 23000[ | ||
| 176 | frequency, in Hz | ||
| 177 | |||
| 178 | Example | ||
| 179 | ------- | ||
| 180 | >>> aubio.note2freq('A4') | ||
| 181 | 440 | ||
| 182 | >>> aubio.note2freq('A3') | ||
| 183 | 220.1 | ||
| 184 | """ | ||
| 185 | midi = note2midi(note) | ||
| 186 | return miditofreq(midi) | ||