From 6ce65791abfdfaa77cc435c6c5fcf2149ceccd55 Mon Sep 17 00:00:00 2001 From: fengbh <1953356163@qq.com> Date: Tue, 16 Jul 2024 00:25:13 +0800 Subject: [PATCH] =?UTF-8?q?=E7=94=A8=E4=BA=8E=E8=AE=A1=E7=AE=97=E5=AE=9A?= =?UTF-8?q?=E6=97=B6=E5=99=A8=E5=88=9D=E5=80=BC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- other/csv2json.py | 23 +++ other/note.py | 66 +++++++++ other/note_data.json | 342 +++++++++++++++++++++++++++++++++++++++++++ other/note_data.txt | 86 +++++++++++ 4 files changed, 517 insertions(+) create mode 100644 other/csv2json.py create mode 100644 other/note.py create mode 100644 other/note_data.json create mode 100644 other/note_data.txt diff --git a/other/csv2json.py b/other/csv2json.py new file mode 100644 index 0000000..3297ac1 --- /dev/null +++ b/other/csv2json.py @@ -0,0 +1,23 @@ +#coding=utf-8 +import csv +import json + +# 文件路径 +file_path = 'note_data.txt' + +# 读取文件并转换为 JSON +notes_dict = {} +with open(file_path, mode='r', encoding='utf-8') as file: + reader = csv.DictReader(file, delimiter='\t') + + for row in reader: + note, frequency, wave_length = row['note'], row['frequency'], row['wave_length'] + notes_dict[note] = {'frequency': float(frequency), 'wave_length': float(wave_length)} + +# 将数据转换为 JSON 并打印 +json_data = json.dumps(notes_dict, indent=4) +print(json_data) + +# 写入 JSON 文件 +with open('note_data.json', 'w', encoding='utf-8') as json_file: + json_file.write(json_data) \ No newline at end of file diff --git a/other/note.py b/other/note.py new file mode 100644 index 0000000..a7afe0c --- /dev/null +++ b/other/note.py @@ -0,0 +1,66 @@ +#coding=utf-8 +import json + +# 常量定义 +MCU_CLK = 1000000 # 12MHz,内部时钟12分频 +MCU_T = 1000000/MCU_CLK # 单位时间为1us +# 音符映射 +note_mapping = { + 1: 'C4', 2: 'D4', 3: 'E4', 4: 'F4', 5: 'G4', 6: 'A4', 7: 'B4' +} + +# 根据频率计算计数器初值 +def freq_to_num(freq): + T = 10**6/freq # 单位时间为1us + time0 = int(2**16 - (T/2) / MCU_T) + time0H = hex(time0>>8) + time0L = hex(time0&0xff) + print("freq = %0.2fHz, T = %dus, time0 = %d, time0H = %s, time0L = %s" % (freq, T, time0, time0H, time0L)) + return time0H, time0L + +# 根据音符名称获取频率 +def note_to_freq(note_name): + try: + # 读取 JSON 文件 + with open('note_data.json', 'r') as file: + notes = json.load(file) + + # 获取特定音符的频率 + frequency = notes[note_name]['frequency'] + return frequency + + except KeyError: + return None + except Exception as e: + print(f"An error occurred: {e}") + return None + +# 根据音符名称获取计数器初值 +def note_to_num(note_name): + frequency = note_to_freq(note_name.upper()) + if frequency is not None: + print(f"{note_name}的频率为{frequency}Hz。") + freq_to_num(frequency) + else: + print(f"{note_name} 不是有效的音符。") + + +# 转换函数 +def map_tones_to_notes(tone_array): + notes = [] + for tone in tone_array: + # 将音调映射到相应的音符 + note = note_mapping.get((tone - 1) % 7 + 1 if tone <= 7 else (tone - 1) % 7 + 8) + notes.append(note) + return notes + +# 原始数据:小星星歌曲 +soundtones = [ + 1,1,5,5,6,6,5,4,4,3,3,2,2,1, + 5,5,4,4,3,3,2,5,5,4,4,3,3,2, + 1,1,5,5,6,6,5,4,4,3,3,2,2,1 +] +if __name__ == '__main__': + mapped_notes = map_tones_to_notes(soundtones) + for note in mapped_notes: + note_to_num(note) diff --git a/other/note_data.json b/other/note_data.json new file mode 100644 index 0000000..b63f89d --- /dev/null +++ b/other/note_data.json @@ -0,0 +1,342 @@ +{ + "C0": { + "frequency": 16.35, + "wave_length": 2109.89 + }, + "C#0": { + "frequency": 17.32, + "wave_length": 1991.47 + }, + "D0": { + "frequency": 18.35, + "wave_length": 1879.69 + }, + "D#0": { + "frequency": 19.45, + "wave_length": 1774.2 + }, + "E0": { + "frequency": 20.6, + "wave_length": 1674.62 + }, + "F0": { + "frequency": 21.83, + "wave_length": 1580.63 + }, + "F#0": { + "frequency": 23.12, + "wave_length": 1491.91 + }, + "G0": { + "frequency": 24.5, + "wave_length": 1408.18 + }, + "G#0": { + "frequency": 25.96, + "wave_length": 1329.14 + }, + "A0": { + "frequency": 27.5, + "wave_length": 1254.55 + }, + "A#0": { + "frequency": 29.14, + "wave_length": 1184.13 + }, + "B0": { + "frequency": 30.87, + "wave_length": 1117.67 + }, + "C1": { + "frequency": 32.7, + "wave_length": 1054.94 + }, + "C#1": { + "frequency": 34.65, + "wave_length": 995.73 + }, + "D1": { + "frequency": 36.71, + "wave_length": 939.85 + }, + "D#1": { + "frequency": 38.89, + "wave_length": 887.1 + }, + "E1": { + "frequency": 41.2, + "wave_length": 837.31 + }, + "F1": { + "frequency": 43.65, + "wave_length": 790.31 + }, + "F#1": { + "frequency": 46.25, + "wave_length": 745.96 + }, + "G1": { + "frequency": 49.0, + "wave_length": 704.09 + }, + "G#1": { + "frequency": 51.91, + "wave_length": 664.57 + }, + "A1": { + "frequency": 55.0, + "wave_length": 627.27 + }, + "A#1": { + "frequency": 58.27, + "wave_length": 592.07 + }, + "B1": { + "frequency": 61.74, + "wave_length": 558.84 + }, + "C2": { + "frequency": 65.41, + "wave_length": 527.47 + }, + "C#2": { + "frequency": 69.3, + "wave_length": 497.87 + }, + "D2": { + "frequency": 73.42, + "wave_length": 469.92 + }, + "D#2": { + "frequency": 77.78, + "wave_length": 443.55 + }, + "E2": { + "frequency": 82.41, + "wave_length": 418.65 + }, + "F2": { + "frequency": 87.31, + "wave_length": 395.16 + }, + "F#2": { + "frequency": 92.5, + "wave_length": 372.98 + }, + "G2": { + "frequency": 98.0, + "wave_length": 352.04 + }, + "G#2": { + "frequency": 103.83, + "wave_length": 332.29 + }, + "A2": { + "frequency": 110.0, + "wave_length": 313.64 + }, + "A#2": { + "frequency": 116.54, + "wave_length": 296.03 + }, + "B2": { + "frequency": 123.47, + "wave_length": 279.42 + }, + "C3": { + "frequency": 130.81, + "wave_length": 263.74 + }, + "C#3": { + "frequency": 138.59, + "wave_length": 248.93 + }, + "D3": { + "frequency": 146.83, + "wave_length": 234.96 + }, + "D#3": { + "frequency": 155.56, + "wave_length": 221.77 + }, + "E3": { + "frequency": 164.81, + "wave_length": 209.33 + }, + "F3": { + "frequency": 174.61, + "wave_length": 197.58 + }, + "F#3": { + "frequency": 185.0, + "wave_length": 186.49 + }, + "G3": { + "frequency": 196.0, + "wave_length": 176.02 + }, + "G#3": { + "frequency": 207.65, + "wave_length": 166.14 + }, + "A3": { + "frequency": 220.0, + "wave_length": 156.82 + }, + "A#3": { + "frequency": 233.08, + "wave_length": 148.02 + }, + "B3": { + "frequency": 246.94, + "wave_length": 139.71 + }, + "C4": { + "frequency": 261.63, + "wave_length": 131.87 + }, + "C#4": { + "frequency": 277.18, + "wave_length": 124.47 + }, + "D4": { + "frequency": 293.66, + "wave_length": 117.48 + }, + "D#4": { + "frequency": 311.13, + "wave_length": 110.89 + }, + "E4": { + "frequency": 329.63, + "wave_length": 104.66 + }, + "F4": { + "frequency": 349.23, + "wave_length": 98.79 + }, + "F#4": { + "frequency": 369.99, + "wave_length": 93.24 + }, + "G4": { + "frequency": 392.0, + "wave_length": 88.01 + }, + "G#4": { + "frequency": 415.3, + "wave_length": 83.07 + }, + "A4": { + "frequency": 440.0, + "wave_length": 78.41 + }, + "A#4": { + "frequency": 466.16, + "wave_length": 74.01 + }, + "B4": { + "frequency": 493.88, + "wave_length": 69.85 + }, + "C5": { + "frequency": 523.25, + "wave_length": 65.93 + }, + "C#5": { + "frequency": 554.37, + "wave_length": 62.23 + }, + "D5": { + "frequency": 587.33, + "wave_length": 58.74 + }, + "D#5": { + "frequency": 622.25, + "wave_length": 55.44 + }, + "E5": { + "frequency": 659.25, + "wave_length": 52.33 + }, + "F5": { + "frequency": 698.46, + "wave_length": 49.39 + }, + "F#5": { + "frequency": 739.99, + "wave_length": 46.62 + }, + "G5": { + "frequency": 783.99, + "wave_length": 44.01 + }, + "G#5": { + "frequency": 830.61, + "wave_length": 41.54 + }, + "A5": { + "frequency": 880.0, + "wave_length": 39.2 + }, + "A#5": { + "frequency": 932.33, + "wave_length": 37.0 + }, + "B5": { + "frequency": 987.77, + "wave_length": 34.93 + }, + "C6": { + "frequency": 1046.5, + "wave_length": 32.97 + }, + "C#6": { + "frequency": 1108.73, + "wave_length": 31.12 + }, + "D6": { + "frequency": 1174.66, + "wave_length": 29.37 + }, + "D#6": { + "frequency": 1244.51, + "wave_length": 27.72 + }, + "E6": { + "frequency": 1318.51, + "wave_length": 26.17 + }, + "F6": { + "frequency": 1396.91, + "wave_length": 24.7 + }, + "F#6": { + "frequency": 1479.98, + "wave_length": 23.31 + }, + "G6": { + "frequency": 1567.98, + "wave_length": 22.0 + }, + "G#6": { + "frequency": 1661.22, + "wave_length": 20.77 + }, + "A6": { + "frequency": 1760.0, + "wave_length": 19.6 + }, + "A#6": { + "frequency": 1864.66, + "wave_length": 18.5 + }, + "B6": { + "frequency": 1975.53, + "wave_length": 17.46 + }, + "C7": { + "frequency": 2093.0, + "wave_length": 1.0 + } +} \ No newline at end of file diff --git a/other/note_data.txt b/other/note_data.txt new file mode 100644 index 0000000..2288841 --- /dev/null +++ b/other/note_data.txt @@ -0,0 +1,86 @@ +note frequency wave_length +C0 16.35 2109.89 +C#0 17.32 1991.47 +D0 18.35 1879.69 +D#0 19.45 1774.2 +E0 20.60 1674.62 +F0 21.83 1580.63 +F#0 23.12 1491.91 +G0 24.50 1408.18 +G#0 25.96 1329.14 +A0 27.50 1254.55 +A#0 29.14 1184.13 +B0 30.87 1117.67 +C1 32.70 1054.94 +C#1 34.65 995.73 +D1 36.71 939.85 +D#1 38.89 887.1 +E1 41.20 837.31 +F1 43.65 790.31 +F#1 46.25 745.96 +G1 49.00 704.09 +G#1 51.91 664.57 +A1 55.00 627.27 +A#1 58.27 592.07 +B1 61.74 558.84 +C2 65.41 527.47 +C#2 69.30 497.87 +D2 73.42 469.92 +D#2 77.78 443.55 +E2 82.41 418.65 +F2 87.31 395.16 +F#2 92.50 372.98 +G2 98.00 352.04 +G#2 103.83 332.29 +A2 110.00 313.64 +A#2 116.54 296.03 +B2 123.47 279.42 +C3 130.81 263.74 +C#3 138.59 248.93 +D3 146.83 234.96 +D#3 155.56 221.77 +E3 164.81 209.33 +F3 174.61 197.58 +F#3 185.00 186.49 +G3 196.00 176.02 +G#3 207.65 166.14 +A3 220.00 156.82 +A#3 233.08 148.02 +B3 246.94 139.71 +C4 261.63 131.87 +C#4 277.18 124.47 +D4 293.66 117.48 +D#4 311.13 110.89 +E4 329.63 104.66 +F4 349.23 98.79 +F#4 369.99 93.24 +G4 392.00 88.01 +G#4 415.30 83.07 +A4 440.00 78.41 +A#4 466.16 74.01 +B4 493.88 69.85 +C5 523.25 65.93 +C#5 554.37 62.23 +D5 587.33 58.74 +D#5 622.25 55.44 +E5 659.25 52.33 +F5 698.46 49.39 +F#5 739.99 46.62 +G5 783.99 44.01 +G#5 830.61 41.54 +A5 880.00 39.2 +A#5 932.33 37 +B5 987.77 34.93 +C6 1046.50 32.97 +C#6 1108.73 31.12 +D6 1174.66 29.37 +D#6 1244.51 27.72 +E6 1318.51 26.17 +F6 1396.91 24.7 +F#6 1479.98 23.31 +G6 1567.98 22 +G#6 1661.22 20.77 +A6 1760.00 19.6 +A#6 1864.66 18.5 +B6 1975.53 17.46 +C7 2093.00 1 \ No newline at end of file