Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

194 строки
4.6 KiB

2 лет назад
  1. from AudioAnalyzer import *
  2. import random
  3. import colorsys
  4. #filename = "团子伴奏提取_Ryan+Gosling,Emma+Stone+-+City+Of+Stars+(From+"La+La+Land"+Soundtrack)_伴奏.mp3"
  5. filename = 'Ryan Gosling,Emma Stone - City Of Stars (From "La La Land" Soundtrack).mp3'
  6. def rnd_color():
  7. h, s, l = random.random(), 0.5 + random.random() / 2.0, 0.4 + random.random() / 5.0
  8. return [int(256 * i) for i in colorsys.hls_to_rgb(h, l, s)]
  9. analyzer = AudioAnalyzer()
  10. analyzer.load(filename)
  11. pygame.init()
  12. infoObject = pygame.display.Info()
  13. screen_w = int(infoObject.current_w/3)
  14. screen_h = int(infoObject.current_w/3)
  15. # Set up the drawing window
  16. screen = pygame.display.set_mode([screen_w, screen_h])
  17. t = pygame.time.get_ticks()
  18. getTicksLastFrame = t
  19. timeCount = 0
  20. avg_bass = 0
  21. bass_trigger = -30
  22. bass_trigger_started = 0
  23. min_decibel = -80
  24. max_decibel = 80
  25. circle_color = (40, 40, 40)
  26. polygon_default_color = [255, 255, 255]
  27. polygon_bass_color = polygon_default_color.copy()
  28. polygon_color_vel = [0, 0, 0]
  29. poly = []
  30. poly_color = polygon_default_color.copy()
  31. circleX = int(screen_w / 2)
  32. circleY = int(screen_h/2)
  33. min_radius = 100
  34. max_radius = 150
  35. radius = min_radius
  36. radius_vel = 0
  37. bass = {"start": 50, "stop": 100, "count": 12}
  38. heavy_area = {"start": 120, "stop": 250, "count": 40}
  39. low_mids = {"start": 251, "stop": 2000, "count": 50}
  40. high_mids = {"start": 2001, "stop": 6000, "count": 20}
  41. freq_groups = [bass, heavy_area, low_mids, high_mids]
  42. bars = []
  43. tmp_bars = []
  44. length = 0
  45. for group in freq_groups:
  46. g = []
  47. s = group["stop"] - group["start"]
  48. count = group["count"]
  49. reminder = s%count
  50. step = int(s/count)
  51. rng = group["start"]
  52. for i in range(count):
  53. arr = None
  54. if reminder > 0:
  55. reminder -= 1
  56. arr = np.arange(start=rng, stop=rng + step + 2)
  57. rng += step + 3
  58. else:
  59. arr = np.arange(start=rng, stop=rng + step + 1)
  60. rng += step + 2
  61. g.append(arr)
  62. length += 1
  63. tmp_bars.append(g)
  64. angle_dt = 360/length
  65. ang = 0
  66. for g in tmp_bars:
  67. gr = []
  68. for c in g:
  69. gr.append(
  70. RotatedAverageAudioBar(circleX+radius*math.cos(math.radians(ang - 90)), circleY+radius*math.sin(math.radians(ang - 90)), c, (255, 0, 255), angle=ang, width=8, max_height=370))
  71. ang += angle_dt
  72. bars.append(gr)
  73. pygame.mixer.music.load(filename)
  74. pygame.mixer.music.play(0)
  75. running = True
  76. while running:
  77. avg_bass = 0
  78. poly = []
  79. # ticks
  80. t = pygame.time.get_ticks()
  81. deltaTime = (t - getTicksLastFrame) / 1000.0
  82. getTicksLastFrame = t
  83. timeCount += deltaTime
  84. screen.fill(circle_color)
  85. for event in pygame.event.get():
  86. if event.type == pygame.QUIT:
  87. running = False
  88. for b1 in bars:
  89. for b in b1:
  90. b.update_all(deltaTime, pygame.mixer.music.get_pos() / 1000.0, analyzer)
  91. for b in bars[0]:
  92. avg_bass += b.avg
  93. avg_bass /= len(bars[0])
  94. if avg_bass > bass_trigger:
  95. if bass_trigger_started == 0:
  96. bass_trigger_started = pygame.time.get_ticks()
  97. if (pygame.time.get_ticks() - bass_trigger_started)/1000.0 > 2:
  98. polygon_bass_color = rnd_color()
  99. bass_trigger_started = 0
  100. if polygon_bass_color is None:
  101. polygon_bass_color = rnd_color()
  102. newr = min_radius + int(avg_bass * ((max_radius - min_radius) / (max_decibel - min_decibel)) + (max_radius - min_radius))
  103. radius_vel = (newr - radius) / 0.15
  104. polygon_color_vel = [(polygon_bass_color[x] - poly_color[x])/0.15 for x in range(len(poly_color))]
  105. elif radius > min_radius:
  106. bass_trigger_started = 0
  107. polygon_bass_color = None
  108. radius_vel = (min_radius - radius) / 0.15
  109. polygon_color_vel = [(polygon_default_color[x] - poly_color[x])/0.15 for x in range(len(poly_color))]
  110. else:
  111. bass_trigger_started = 0
  112. poly_color = polygon_default_color.copy()
  113. polygon_bass_color = None
  114. polygon_color_vel = [0, 0, 0]
  115. radius_vel = 0
  116. radius = min_radius
  117. radius += radius_vel * deltaTime
  118. for x in range(len(polygon_color_vel)):
  119. value = polygon_color_vel[x]*deltaTime + poly_color[x]
  120. poly_color[x] = value
  121. for b1 in bars:
  122. for b in b1:
  123. b.x, b.y = circleX+radius*math.cos(math.radians(b.angle - 90)), circleY+radius*math.sin(math.radians(b.angle - 90))
  124. b.update_rect()
  125. poly.append(b.rect.points[3])
  126. poly.append(b.rect.points[2])
  127. pygame.draw.polygon(screen, poly_color, poly)
  128. pygame.draw.circle(screen, circle_color, (circleX, circleY), int(radius))
  129. pygame.display.flip()
  130. pygame.quit()