20230411 将数组转成十六进制字符串
array = [12, 34, 56, 78, 90]
hex_string = ''.join(['{:02x}'.format(x) for x in array])
print(hex_string)
20230409 变声
用python写一个变声器,要求导入mp4或者mp3文件,将视频中的声音变成女声
from pydub import AudioSegment
from pydub.effects import *
import os# 获取音频文件名
file_name = input("请输入音频文件名:")# 判断文件类型
file_type = os.path.splitext(file_name)[1]
if file_type == ".mp3":sound = AudioSegment.from_mp3(file_name)
elif file_type == ".mp4":sound = AudioSegment.from_file(file_name, format="mp4")
else:print("不支持的文件类型")exit()# 变声(把声音变成女生)
changed_sound = sound.apply(lambda x: effects.pitch_shift(x, 1200))# 保存变声后的音频文件
changed_sound.export("changed_sound" + file_type, format=file_type)
print("变声成功!")
import pyaudio
import numpy as np# 配置声音参数
CHUNK = 1024
FORMAT = pyaudio.paInt16
CHANNELS = 1
RATE = 44100# 初始化声音输入
p = pyaudio.PyAudio()
stream = p.open(format=FORMAT,channels=CHANNELS,rate=RATE,input=True,output=True,frames_per_buffer=CHUNK)# 定义变声函数
def change_voice(data, gender, age, pitch):# 做一些变声的处理# 这里省略具体实现return data# 读取声音并进行变声
while True:data = stream.read(CHUNK)gender = 'male' # 声音性别,可设置为 'male' 或 'female'age = 30 # 声音年龄,可设置为年龄值(例如 30)pitch = 2 # 音调,可设置为 1 到 10 之间的值data = change_voice(data, gender, age, pitch)stream.write(data, CHUNK)# 关闭声音输入输出
stream.stop_stream()
stream.close()
p.terminate()
20230407 写一段串口发送与接收的程序
写一段51单片机串口发送和接收的程序,波特率115200,偶校验
#include <reg51.h>#define FOSC 11059200L
#define BAUD 115200L
#define TIMER_RELOAD_VALUE -(FOSC / 12 / BAUD)
#define RX_BUF_SIZE 32typedef unsigned char byte;sbit LED = P1^0; // LED 灯
sbit TXD = P3^1; // 串口发送端口
sbit RXD = P3^0; // 串口接收端口byte rx_buf[RX_BUF_SIZE]; // 接收缓存
byte rx_head = 0; // 缓存头指针
byte rx_tail = 0; // 缓存尾指针void init_uart() {SCON = 0x50; // 8 位数据,可变波特率TMOD &= 0x0F; // 设置定时器 1 为模式 0TMOD |= 0x20;TH1 = TIMER_RELOAD_VALUE >> 8; // 初始化波特率TL1 = TIMER_RELOAD_VALUE;TR1 = 1; // 启动定时器 1ES = 1; // 允许串口中断EA = 1; // 允许总中断
}void send_byte(byte b) {SBUF = b; // 发送一个字节while (!TI); // 等待发送完成TI = 0; // 清除发送完成标志
}void send_string(const char *s) {while (*s) {send_byte(*s++);}
}byte receive_byte() {byte b;if (rx_head != rx_tail) { // 如果缓存不为空b = rx_buf[rx_tail++]; // 弹出一个字节if (rx_tail >= RX_BUF_SIZE) { // 处理环形缓存rx_tail = 0;}return b;} else {return 0;}
}void receive_handler() interrupt 4 {if (RI) { // 如果接收中断标志被置位byte b = SBUF; // 读取接收缓存if (rx_head != rx_tail || b != 0xAA) { // 如果缓存不满或者不是同步字节rx_buf[rx_head++] = b; // 存入接收缓存if (rx_head >= RX_BUF_SIZE) { // 处理环形缓存rx_head = 0;}}RI = 0; // 清除接收中断标志}
}void main() {init_uart(); // 初始化串口LED = 1; // 熄灭 LED 灯send_string("Hello, world!\r\n"); // 发送欢迎信息while (1) {byte b = receive_byte(); // 从接收缓存中读取一个字节if (b == '1') { // 如果接收到 '1'LED = 0; // 点亮 LED 灯send_string("LED is on.\r\n"); // 发送提示信息} else if (b == '0') { // 如果接收到 '0'LED = 1; // 熄灭 LED 灯send_string("LED is off.\r\n"); // 发送提示信息}}
}
20230406 单片机代码解释
测试用例是指用于测试软件功能、性能和可靠性等方面的一组步骤、条件和数据。测试用例通常由测试工程师编写,以确保软件在不同的情况下能够正常运行,同时也可以帮助发现软件中的缺陷和问题。测试用例通常包括以下几个方面:
测试目的:明确测试的目的和测试的范围。
测试条件:指测试环境、测试数据和测试工具等方面的条件。
测试步骤:指具体的测试步骤和操作流程,包括预期结果和实际结果。
测试数据:指用于测试的数据,包括输入数据和输出数据。
预期结果:指根据测试条件和测试步骤所得出的预期结果。
实际结果:指执行测试步骤后得到的实际结果,用于与预期结果进行比较。
通过编写测试用例,可以有效地测试软件的各种功能和性能,从而提高软件的质量和可靠性。同时,测试用例也可以为软件的维护和升级提供重要的参考依据。
假设你是一名单片机工程师,请帮我解释下面的代码 void Task_Marks_Handler_Callback(void) { u8 i; for(i=0; i<Tasks_Max; i++) { if(Task_Comps[i].TIMCount) /* If the time is not 0 / { Task_Comps[i].TIMCount–; / Time counter decrement / if(Task_Comps[i].TIMCount == 0) / If time arrives */ { /*Resume the timer value and try again / Task_Comps[i].TIMCount = Task_Comps[i].TRITime; Task_Comps[i].Run = 1; / The task can be run */ } } } }
20230405 用python写个音乐播放器
import os
import tkinter as tk
from tkinter import filedialog
import pygame
from pygame.locals import QUIT, KEYDOWN, K_SPACE, K_LEFT, K_RIGHT, K_UP, K_DOWN, K_RETURN
from PIL import Image, ImageTkclass MusicPlayer:def __init__(self, master):self.master = mastermaster.title("Music Player")master.geometry("400x400")# 创建控件self.song_listbox = tk.Listbox(master, width=50)self.play_button = tk.Button(master, text="Play", command=self.play_music)self.pause_button = tk.Button(master, text="Pause", command=self.pause_music)self.stop_button = tk.Button(master, text="Stop", command=self.stop_music)self.prev_button = tk.Button(master, text="Prev", command=self.prev_music)self.next_button = tk.Button(master, text="Next", command=self.next_music)self.file_button = tk.Button(master, text="Open", command=self.select_music_file)self.volume_scale = tk.Scale(master, from_=0, to=100, orient=tk.HORIZONTAL, command=self.change_volume)self.current_time_label = tk.Label(master, text="00:00")self.total_time_label = tk.Label(master, text="00:00")self.current_song_label = tk.Label(master, text="")self.song_image_label = tk.Label(master)self.song_lyrics_text = tk.Text(master, height=5, wrap=tk.WORD)# 排列控件self.song_listbox.pack(pady=10)self.play_button.pack(side=tk.LEFT, padx=10)self.pause_button.pack(side=tk.LEFT, padx=10)self.stop_button.pack(side=tk.LEFT, padx=10)self.prev_button.pack(side=tk.LEFT, padx=10)self.next_button.pack(side=tk.LEFT, padx=10)self.file_button.pack(side=tk.LEFT, padx=10)self.volume_scale.pack(side=tk.LEFT, padx=10)self.current_time_label.pack(pady=10)self.total_time_label.pack(pady=10)self.current_song_label.pack(pady=10)self.song_image_label.pack(pady=10)self.song_lyrics_text.pack(pady=10)# 初始化变量self.music_file_list = []self.current_song_index = 0self.current_song_time = 0self.total_song_time = 0self.song_image = Noneself.song_lyrics = []# 初始化pygamepygame.init()pygame.mixer.init()def select_music_file(self):# 选择音乐文件filetypes = (("Music files", "*.mp3;*.ogg"), ("All files", "*.*"))file_path = filedialog.askopenfilename(title="Select Music File", filetypes=filetypes)if file_path:self.music_file_list.append(file_path)self.song_listbox.insert(tk.END, os.path.basename(file_path))def play_music(self):# 播放音乐if self.music_file_list:pygame.mixer.music.load(self.music_file_list[self.current_song_index])pygame.mixer.music.play()self.update_song_info()def pause_music(self):# 暂停音乐pygame.mixer.music.pause()def stop_music(self):# 停止音乐pygame.mixer.music.stop()def prev_music(self):# 上一曲self.current_song_index = (self.current_song_index - 1) % len(self.music_file_list)self.play_music()def next_music(self):# 下一曲self.current_song_index = (self.current_song_index + 1) % len(self.music_file_list)self.play_music()def change_volume(self, volume):# 改变音量pygame.mixer.music.set_volume(int(volume) / 100)def update_song_info(self):# 更新歌曲信息self.current_song_label.config(text=os.path.basename(self.music_file_list[self.current_song_index]))self.total_song_time = pygame.mixer.Sound(self.music_file_list[self.current_song_index]).get_length()self.total_time_label.config(text=self.convert_time(self.total_song_time))self.song_image = Image.open("song.jpg")self.song_image = self.song_image.resize((200, 200))self.song_image = ImageTk.PhotoImage(self.song_image)self.song_image_label.config(image=self.song_image)self.song_lyrics = ["[00:00.00] 歌词加载中..."]self.song_lyrics_text.delete("1.0", tk.END)self.song_lyrics_text.insert(tk.END, "\n".join(self.song_lyrics))def convert_time(self, seconds):# 将秒数转换成分钟:秒钟的格式minutes = seconds // 60seconds = seconds % 60return "{:02d}:{:02d}".format(int(minutes), int(seconds))def update_time(self):# 更新当前时间标签if pygame.mixer.music.get_busy():self.current_song_time = pygame.mixer.music.get_pos() / 1000self.current_time_label.config(text=self.convert_time(self.current_song_time))else:self.current_time_label.config(text="00:00")# 更新歌词current_lyrics_index = 0for i in range(len(self.song_lyrics)):if self.convert_time_to_seconds(self.song_lyrics[i].split("[")[1].split("]")[0]) <= self.current_song_time:current_lyrics_index = ielse:breakself.song_lyrics_text.delete("1.0", tk.END)self.song_lyrics_text.insert(tk.END, "\n".join(self.song_lyrics[current_lyrics_index:]))self.master.after(1000, self.update_time)def convert_time_to_seconds(self, time_str):# 将分钟:秒钟的格式转换成秒数minutes, seconds = time_str.split(":")return int(minutes) * 60 + int(seconds)def bind_key_event(self):# 绑定键盘事件self.master.bind("<Key>", self.key_event_handler)def key_event_handler(self, event):# 处理键盘事件if event.type == KEYDOWN:if event.key == K_SPACE:if pygame.mixer.music.get_busy():self.pause_music()else:self.play_music()elif event.key == K_LEFT:self.stop_music()self.prev_music()elif event.key == K_RIGHT:self.stop_music()self.next_music()elif event.key == K_UP:volume = int(self.volume_scale.get()) + 10if volume > 100:volume = 100self.volume_scale.set(volume)self.change_volume(volume)elif event.key == K_DOWN:volume = int(self.volume_scale.get()) - 10if volume < 0:volume = 0self.volume_scale.set(volume)self.change_volume(volume)elif event.key == K_RETURN:self.select_music_file()def run(self):# 运行音乐播放器self.bind_key_event()self.update_time()self.master.mainloop()if __name__ == "__main__":root = tk.Tk()music_player = MusicPlayer(root)music_player.run()
本文链接:https://my.lmcjl.com/post/3938.html
4 评论