forked from xiaozhi/xiaozhi-esp32
add scripts folder
This commit is contained in:
40
scripts/convert_audio_to_p3.py
Normal file
40
scripts/convert_audio_to_p3.py
Normal file
@@ -0,0 +1,40 @@
|
||||
# convert audio files to protocol v3 stream
|
||||
import librosa
|
||||
import opuslib
|
||||
import struct
|
||||
import sys
|
||||
import tqdm
|
||||
import numpy as np
|
||||
|
||||
def encode_audio_to_opus(input_file, output_file):
|
||||
# Load audio file using librosa
|
||||
audio, sample_rate = librosa.load(input_file, sr=None, mono=False, dtype=np.int16)
|
||||
|
||||
# Get left channel if stereo
|
||||
if audio.ndim == 2:
|
||||
audio = audio[0]
|
||||
|
||||
# Initialize Opus encoder
|
||||
encoder = opuslib.Encoder(sample_rate, 1, opuslib.APPLICATION_VOIP)
|
||||
|
||||
# Encode audio data to Opus packets
|
||||
# Save encoded data to file
|
||||
with open(output_file, 'wb') as f:
|
||||
sample_rate = 16000 # 16000Hz
|
||||
duration = 60 # 60ms every frame
|
||||
frame_size = int(sample_rate * duration / 1000)
|
||||
for i in tqdm.tqdm(range(0, len(audio) - frame_size, frame_size)):
|
||||
frame = audio[i:i + frame_size]
|
||||
opus_data = encoder.encode(frame.tobytes(), frame_size=frame_size)
|
||||
# protocol format, [1u type, 1u reserved, 2u len, data]
|
||||
packet = struct.pack('>BBH', 0, 0, len(opus_data)) + opus_data
|
||||
f.write(packet)
|
||||
|
||||
# Example usage
|
||||
if len(sys.argv) != 3:
|
||||
print('Usage: python convert.py <input_file> <output_file>')
|
||||
sys.exit(1)
|
||||
|
||||
input_file = sys.argv[1]
|
||||
output_file = sys.argv[2]
|
||||
encode_audio_to_opus(input_file, output_file)
|
||||
2
scripts/flash.sh
Executable file
2
scripts/flash.sh
Executable file
@@ -0,0 +1,2 @@
|
||||
#!/bin/sh
|
||||
esptool.py -p /dev/ttyACM0 -b 2000000 write_flash 0 ../releases/v0.9.9_bread-compact-wifi/merged-binary.bin
|
||||
50
scripts/release.py
Normal file
50
scripts/release.py
Normal file
@@ -0,0 +1,50 @@
|
||||
import sys
|
||||
import os
|
||||
import json
|
||||
|
||||
# 切换到项目根目录
|
||||
os.chdir(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
|
||||
|
||||
def get_board_type():
|
||||
with open("build/compile_commands.json") as f:
|
||||
data = json.load(f)
|
||||
for item in data:
|
||||
if not item["file"].endswith("main.cc"):
|
||||
continue
|
||||
command = item["command"]
|
||||
# extract -DBOARD_TYPE=xxx
|
||||
board_type = command.split("-DBOARD_TYPE=\\\"")[1].split("\\\"")[0].strip()
|
||||
return board_type
|
||||
return None
|
||||
|
||||
def get_project_version():
|
||||
with open("CMakeLists.txt") as f:
|
||||
for line in f:
|
||||
if line.startswith("set(PROJECT_VER"):
|
||||
return line.split("\"")[1].split("\"")[0].strip()
|
||||
return None
|
||||
|
||||
def merge_bin():
|
||||
if os.system("idf.py merge-bin") != 0:
|
||||
print("merge bin failed")
|
||||
sys.exit(1)
|
||||
|
||||
def zip_bin(board_type, project_version):
|
||||
if not os.path.exists("releases"):
|
||||
os.makedirs("releases")
|
||||
output_path = f"releases/v{project_version}_{board_type}.zip"
|
||||
if os.path.exists(output_path):
|
||||
os.remove(output_path)
|
||||
if os.system(f"zip -j {output_path} build/merged-binary.bin") != 0:
|
||||
print("zip bin failed")
|
||||
sys.exit(1)
|
||||
print(f"zip bin to {output_path} done")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
merge_bin()
|
||||
board_type = get_board_type()
|
||||
print("board type:", board_type)
|
||||
project_version = get_project_version()
|
||||
print("project version:", project_version)
|
||||
zip_bin(board_type, project_version)
|
||||
168
scripts/versions.py
Normal file
168
scripts/versions.py
Normal file
@@ -0,0 +1,168 @@
|
||||
#! /usr/bin/env python3
|
||||
from dotenv import load_dotenv
|
||||
load_dotenv()
|
||||
|
||||
import os
|
||||
import struct
|
||||
import zipfile
|
||||
import oss2
|
||||
import json
|
||||
|
||||
# 切换到项目根目录
|
||||
os.chdir(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
|
||||
|
||||
def get_chip_id_string(chip_id):
|
||||
return {
|
||||
0x0000: "esp32",
|
||||
0x0002: "esp32s2",
|
||||
0x0005: "esp32c3",
|
||||
0x0009: "esp32s3",
|
||||
0x000C: "esp32c2",
|
||||
0x000D: "esp32c6",
|
||||
0x0010: "esp32h2",
|
||||
0x0011: "esp32c5",
|
||||
0x0012: "esp32p4",
|
||||
0x0017: "esp32c5",
|
||||
}[chip_id]
|
||||
|
||||
def get_flash_size(flash_size):
|
||||
MB = 1024 * 1024
|
||||
return {
|
||||
0x00: 1 * MB,
|
||||
0x01: 2 * MB,
|
||||
0x02: 4 * MB,
|
||||
0x03: 8 * MB,
|
||||
0x04: 16 * MB,
|
||||
0x05: 32 * MB,
|
||||
0x06: 64 * MB,
|
||||
0x07: 128 * MB,
|
||||
}[flash_size]
|
||||
|
||||
def get_app_desc(data):
|
||||
magic = struct.unpack("<I", data[0x00:0x04])[0]
|
||||
if magic != 0xabcd5432:
|
||||
raise Exception("Invalid app desc magic")
|
||||
version = data[0x10:0x30].decode("utf-8").strip('\0')
|
||||
project_name = data[0x30:0x50].decode("utf-8").strip('\0')
|
||||
time = data[0x50:0x60].decode("utf-8").strip('\0')
|
||||
date = data[0x60:0x70].decode("utf-8").strip('\0')
|
||||
idf_ver = data[0x70:0x90].decode("utf-8").strip('\0')
|
||||
elf_sha256 = data[0x90:0xb0].hex()
|
||||
return {
|
||||
"name": project_name,
|
||||
"version": version,
|
||||
"compile_time": date + "T" + time,
|
||||
"idf_version": idf_ver,
|
||||
"elf_sha256": elf_sha256,
|
||||
}
|
||||
|
||||
def get_board_name(folder):
|
||||
basename = os.path.basename(folder)
|
||||
if basename.startswith("v0.2"):
|
||||
return "bread-simple"
|
||||
if basename.startswith("v0.3") or basename.startswith("v0.4") or basename.startswith("v0.5") or basename.startswith("v0.6"):
|
||||
if "ML307" in basename:
|
||||
return "bread-compact-ml307"
|
||||
elif "WiFi" in basename:
|
||||
return "bread-compact-wifi"
|
||||
elif "KevinBox1" in basename:
|
||||
return "kevin-box-1"
|
||||
if basename.startswith("v0.7") or basename.startswith("v0.8") or basename.startswith("v0.9"):
|
||||
return basename.split("_")[1]
|
||||
raise Exception(f"Unknown board name: {basename}")
|
||||
|
||||
def read_binary(dir_path):
|
||||
merged_bin_path = os.path.join(dir_path, "merged-binary.bin")
|
||||
data = open(merged_bin_path, "rb").read()[0x200000:]
|
||||
if data[0] != 0xE9:
|
||||
print(dir_path, "is not a valid image")
|
||||
return
|
||||
# get flash size
|
||||
flash_size = get_flash_size(data[0x3] >> 4)
|
||||
chip_id = get_chip_id_string(data[0xC])
|
||||
# get segments
|
||||
segment_count = data[0x1]
|
||||
segments = []
|
||||
offset = 0x18
|
||||
for i in range(segment_count):
|
||||
segment_size = struct.unpack("<I", data[offset + 4:offset + 8])[0]
|
||||
offset += 8
|
||||
segment_data = data[offset:offset + segment_size]
|
||||
offset += segment_size
|
||||
segments.append(segment_data)
|
||||
assert offset < len(data), "offset is out of bounds"
|
||||
|
||||
# extract bin file
|
||||
bin_path = os.path.join(dir_path, "xiaozhi.bin")
|
||||
if not os.path.exists(bin_path):
|
||||
print("extract bin file to", bin_path)
|
||||
open(bin_path, "wb").write(data)
|
||||
|
||||
# The app desc is in the first segment
|
||||
desc = get_app_desc(segments[0])
|
||||
return {
|
||||
"chip_id": chip_id,
|
||||
"flash_size": flash_size,
|
||||
"board": get_board_name(dir_path),
|
||||
"application": desc,
|
||||
}
|
||||
|
||||
def extract_zip(zip_path, extract_path):
|
||||
if not os.path.exists(extract_path):
|
||||
os.makedirs(extract_path)
|
||||
print(f"Extracting {zip_path} to {extract_path}")
|
||||
with zipfile.ZipFile(zip_path, 'r') as zip_ref:
|
||||
zip_ref.extractall(extract_path)
|
||||
|
||||
def upload_dir_to_oss(source_dir, target_dir):
|
||||
auth = oss2.Auth(os.environ['OSS_ACCESS_KEY_ID'], os.environ['OSS_ACCESS_KEY_SECRET'])
|
||||
bucket = oss2.Bucket(auth, os.environ['OSS_ENDPOINT'], os.environ['OSS_BUCKET_NAME'])
|
||||
for filename in os.listdir(source_dir):
|
||||
oss_key = os.path.join(target_dir, filename)
|
||||
print('uploading', oss_key)
|
||||
bucket.put_object(oss_key, open(os.path.join(source_dir, filename), 'rb'))
|
||||
|
||||
def main():
|
||||
release_dir = "releases"
|
||||
versions = []
|
||||
# look for zip files startswith "v"
|
||||
for name in os.listdir(release_dir):
|
||||
if name.startswith("v") and name.endswith(".zip"):
|
||||
tag = name[:-4]
|
||||
folder = os.path.join(release_dir, tag)
|
||||
if not os.path.exists(folder):
|
||||
os.makedirs(folder)
|
||||
extract_zip(os.path.join(release_dir, name), folder)
|
||||
info = read_binary(folder)
|
||||
target_dir = os.path.join("firmwares", tag)
|
||||
info["tag"] = tag
|
||||
info["url"] = os.path.join(os.environ['OSS_BUCKET_URL'], target_dir, "xiaozhi.bin")
|
||||
open(os.path.join(folder, "info.json"), "w").write(json.dumps(info, indent=4))
|
||||
# upload all file to oss
|
||||
upload_dir_to_oss(folder, target_dir)
|
||||
# read info.json
|
||||
info = json.load(open(os.path.join(folder, "info.json")))
|
||||
versions.append(info)
|
||||
|
||||
# sort versions by version
|
||||
versions.sort(key=lambda x: x["tag"], reverse=True)
|
||||
# write versions to file
|
||||
versions_path = os.path.join(release_dir, "versions.json")
|
||||
open(versions_path, "w").write(json.dumps(versions, indent=4))
|
||||
print(f"Versions written to {versions_path}")
|
||||
|
||||
# copy versions.json to server
|
||||
versions_config_path = os.environ.get('VERSIONS_CONFIG_PATH')
|
||||
if not versions_config_path:
|
||||
print("VERSIONS_CONFIG_PATH is not set")
|
||||
exit(1)
|
||||
ret = os.system(f'scp {versions_path} {versions_config_path}')
|
||||
if ret != 0:
|
||||
print(f'Failed to copy versions.json to server')
|
||||
exit(1)
|
||||
print(f'Copied versions.json to server: {versions_config_path}')
|
||||
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Reference in New Issue
Block a user