python - json 文件只能采用整数或切片格式而不是字符串
问题描述
我在运行下面的代码时遇到问题,在 t one_freq =tone_map[tone_name 行它会带来错误,即列表索引必须是整数或切片而不是字符串
评论音调网站上的 Json 样本是
[
{
"432": "Note",
"434": "Frequency (Hz)",
"436": "Wavelength (cm)",
"438": "",
"440": "",
"442": "",
"444": "",
"446": ""
},
{
"432": "C0",
"434": "16.35",
"436": "2109.89",
"438": "",
"440": "",
"442": "",
"444": "",
"446": ""
},
{
"432": "C#0/Db0",
"434": "17.32",
"436": "1991.47",
"438": "",
"440": "",
"442": "",
"444": "",
"446": ""
},
用于合成音调以生成音乐的 python 代码是 this.i 已将http://www.phy.mtu.edu/~suits/notefreqs.html上的 html 表转换为正在使用但表示它有的 json 文件为整数或切片
import json
import numpy as np
import matplotlib.pyplot as plt
from scipy.io.wavfile import write
# Synthesize the tone based on the input parameters
def tone_synthesizer(freq, duration, amplitude=1.0, sampling_freq=44100):
# Construct the time axis
time_axis = np.linspace(0, duration, duration * sampling_freq)
# Construct the audio signal
signal = amplitude * np.sin(2 * np.pi * freq * time_axis)
return signal.astype(np.int16)
if __name__=='__main__':
# Names of output files
file_tone_single = 'generated_tone_single.wav'
file_tone_sequence = 'generated_tone_sequence.wav'
# Source: http://www.phy.mtu.edu/~suits/notefreqs.html
mapping_file = 'tone_mapping.json'
# Load the tone to frequency map from the mapping file
with open(mapping_file, 'r') as f:
tone_map = json.loads(f.read())
# Set input parameters to generate 'F' tone
tone_name = 'F'
duration = 3 # seconds
amplitude = 12000
sampling_freq = 44100 # Hz
# Extract the tone frequency
tone_freq = tone_map[tone_name]
# Generate the tone using the above parameters
synthesized_tone = tone_synthesizer(tone_freq, duration, amplitude, sampling_freq)
# Write the audio signal to the output file
write(file_tone_single, sampling_freq, synthesized_tone)
# Define the tone sequence along with corresponding durations in seconds
tone_sequence = [('G', 0.4), ('D', 0.5), ('F', 0.3), ('C', 0.6), ('A', 0.4)]
# Construct the audio signal based on the above sequence
signal = np.array([])
for item in tone_sequence:
# Get the name of the tone
tone_name = item[0]
# Extract the corresponding frequency of the tone
freq = tone_map[tone_name]
# Extract the duration
duration = item[1]
# Synthesize the tone
synthesized_tone = tone_synthesizer(freq, duration, amplitude, sampling_freq)
# Append the output signal
signal = np.append(signal, synthesized_tone, axis=0)
# Save the audio in the output file
write(file_tone_sequence, sampling_freq, signal)
解决方案
从python文档:
json.loads(s[, encoding[, cls[, object_hook[, parse_float[, parse_int[, parse_constant[, object_pairs_hook[, **kw]]]]]]]])
使用此转换表将 s(包含 JSON 文档的 str 或 unicode 实例)反序列化为 Python 对象。
如果 s 是 str 实例并且使用基于 ASCII 的编码而不是 UTF-8(例如 latin-1)进行编码,则必须指定适当的编码名称。不允许使用不基于 ASCII 的编码(例如 UCS-2),应首先将其解码为 unicode。
从错误中可以理解,tone_map
是一个列表。尝试这个:
for i in tone_map:
tone_freq = i[tone_name]
将tone_freq 存储在列表中并使用它
tone_frequencies = list()
for i in tone_map:
tone_frequencies.append(i)
推荐阅读
- r - 在 Shiny 中显示反应式 htmlTable 表
- swift4 - 警告:在 iPad 上调用 -[UIPrinterPickerController presentAnimated:completionHandler:]
- sql - 如何将任意数量的数字添加到字符串中或从 Postgresql 中的字符串中删除数字
- scala - scala中的可选默认参数
- java - 在运行时使用唯一名称为每个步骤命名屏幕截图
- php - html2text 结果删除了一些特殊字符
- c++ - 如何在单独的线程中创建多个任务?
- azure - Web App 上的 Azure 身份验证和对 Intranet Windows 身份验证 Web 服务的调用
- swift - Alamofire POST 应用程序/x-www-form-urlencoded 编码
- javascript - 背景滑块img上的下一个/上一个按钮