pygame 音乐粒子特效

代码

python 复制代码
import pygame
import numpy as np
import pymunk
from pymunk import Vec2d
import random
import librosa
import pydub

# 初始化pygame
pygame.init()

# 创建屏幕
screen = pygame.display.set_mode((1920*2-10, 1080*2-10))
clock = pygame.time.Clock()

# 加载音乐文件
audio_file = '周杰伦-周大侠.flac'
audio = pydub.AudioSegment.from_file(audio_file)
audio = audio.set_channels(1)  # 确保音乐是单声道的
audio.export('temp.wav', format='wav')  # 转换为wav格式,因为pygame不支持mp3

# 播放音乐
pygame.mixer.init()
pygame.mixer.music.load('temp.wav')
pygame.mixer.music.play(-1)  # -1表示无限循环播放

# 转换为波形数组
y, sr = librosa.load('temp.wav')

# 提取节奏
tempo, beat_frames = librosa.beat.beat_track(y=y, sr=sr)
beat_times = librosa.frames_to_time(beat_frames, sr=sr)

# 创建pymunk空间
space = pymunk.Space()
space.gravity = (0, 0)  # 设置重力

# 添加边界
border_thickness = 10
border_offset = border_thickness / 2

# 创建边界的四个顶点
bodies = [pymunk.Body(body_type=pymunk.Body.STATIC) for _ in range(4)]
segments = [
    pymunk.Segment(bodies[0], (border_offset, border_offset), (screen.get_width() - border_offset, border_offset), border_thickness),
    pymunk.Segment(bodies[1], (screen.get_width() - border_offset, border_offset), (screen.get_width() - border_offset, screen.get_height() - border_offset), border_thickness),
    pymunk.Segment(bodies[2], (screen.get_width() - border_offset, screen.get_height() - border_offset), (border_offset, screen.get_height() - border_offset), border_thickness),
    pymunk.Segment(bodies[3], (border_offset, screen.get_height() - border_offset), (border_offset, border_offset), border_thickness)
]

# 为边界形状添加到空间中
for body, segment in zip(bodies, segments):
    space.add(body, segment)  # 同时添加刚体和形状到空间中
    segment.elasticity = 1.0
    segment.friction = 0.0


# 粒子类
class Particle:
    def __init__(self, space, position):
        self.body = pymunk.Body(1, float('inf'))
        self.body.position = position
        self.shape = pymunk.Circle(self.body, 10)
        self.shape.elasticity = 1.0
        self.shape.friction = 0.0
        space.add(self.body, self.shape)
        self.color = (random.randint(0, 255), random.randint(0, 255), random.randint(0, 255))

    def draw(self, screen):
        pos = int(self.body.position.x), int(self.body.position.y)
        pygame.draw.circle(screen, self.color, pos, int(self.shape.radius))

# 创建粒子
particles = [Particle(space, (random.randint(100, 700), random.randint(100, 500))) for _ in range(1000)]

# 主循环
running = True
frame = 0
while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False

    screen.fill((0, 0, 0))

    # 更新pymunk空间
    space.step(1/60.0)

    # 根据音乐节奏更新粒子速度
    if frame < len(beat_times) and pygame.time.get_ticks() / 1000 > beat_times[frame]:
        for particle in particles:
            impulse = Vec2d(random.uniform(-50, 50), random.uniform(-50, 50))
            particle.body.apply_impulse_at_local_point(impulse)
        frame += 1

    # 绘制粒子
    for particle in particles:
        particle.draw(screen)

    pygame.display.flip()
    clock.tick(60)

pygame.quit()

解释

这段代码是一个使用Pygame和Pymunk库创建的音乐可视化效果。它加载一个音乐文件,提取音乐的节奏,并根据节奏在屏幕上创建和更新粒子的位置。以下是代码的详细解释:

  1. 导入所需的库:pygame用于图形和声音,numpy用于数学运算,pymunk用于物理模拟,random用于生成随机数,librosapydub用于处理音乐文件。
  2. 初始化Pygame并创建一个屏幕。
  3. 加载音乐文件,将其转换为单声道,并导出为WAV格式,因为Pygame不支持MP3格式。
  4. 使用pygame.mixer播放音乐。
  5. 使用librosa将音乐文件转换为波形数组,并提取音乐的节奏。
  6. 创建一个Pymunk空间,设置重力为0,并添加边界。
  7. 定义一个Particle类,用于创建和绘制粒子。
  8. 创建一个粒子列表,每个粒子都有一个随机位置。
  9. 主循环:处理事件,更新Pymunk空间,根据音乐节奏更新粒子速度,绘制粒子,并更新屏幕。
  10. 退出Pygame。
    这个代码的主要特点是使用音乐的节奏来控制粒子的运动,创造出一种动态的音乐可视化效果。
相关推荐
Eric.Lee202142 分钟前
ubuntu 安装 Miniconda
linux·运维·python·ubuntu·miniconda
无心水1 小时前
【Python实战进阶】1、Python高手养成指南:四阶段突破法从入门到架构师
开发语言·python·django·matplotlib·gil·python实战进阶·python工程化实战进阶
李剑一1 小时前
Python学习笔记1
python
Salt_07283 小时前
DAY 19 数组的常见操作和形状
人工智能·python·机器学习
无心水4 小时前
【Python实战进阶】2、Jupyter Notebook终极指南:为什么说不会Jupyter就等于不会Python?
python·jupyter·信息可视化·binder·google colab·python实战进阶·python工程化实战进阶
上班日常摸鱼5 小时前
Shell脚本基础教程:变量、条件判断、循环、函数实战(附案例)
python
无心水5 小时前
【Python实战进阶】5、Python字符串终极指南:从基础到高性能处理的完整秘籍
开发语言·网络·python·字符串·unicode·python实战进阶·python工业化实战进阶
2301_807583235 小时前
了解python,并编写第一个程序,常见的bug
linux·python
小白学大数据5 小时前
构建混合爬虫:何时使用Requests,何时切换至Selenium处理请求头?
爬虫·python·selenium·测试工具
2401_827560205 小时前
【Python脚本系列】PyAudio+librosa+dtw库录制、识别音频并实现点击(四)
python·语音识别