AudioToDeforumKeyframes.py 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. # Beat tracking example
  4. # Appunti: buono per un video u po' psichedelico ci va:
  5. # <lora:bb3l15:1> an abstract circuit board design with orange yellow green and purple squares over red background in the style of bb3l15
  6. # su modello colorful_v30 (https://civitai.com/images/3024879?modelVersionId=188803&prioritizedUserIds=913950&period=AllTime&sort=Most+Reactions&limit=20 )
  7. # <lora:Ink_scenery:1> black background, sketch jungle scenery, path, dense su sd 1.5
  8. # Wilson <lora:Ink_scenery:1> black background, sketch jungle setting
  9. import os
  10. import numpy as np
  11. import librosa
  12. import random
  13. import math
  14. from scipy.interpolate import interp1d
  15. import matplotlib.pyplot as plt
  16. def plotting_stuffs():
  17. plt.figure(figsize=(15, 17))
  18. t = librosa.frames_to_time(range(rms.size), hop_length=flength)
  19. ax = plt.subplot(3, 1, 1)
  20. librosa.display.waveshow(wave, alpha=0.5)
  21. plt.plot(t, rms[0], color="r")
  22. plt.plot(t, trms, color="b")
  23. plt.ylim((-1, 1))
  24. plt.title("Wave&Featurres")
  25. plt.show()
  26. x = np.linspace(0,1,100)
  27. y = sig(x)
  28. plt.plot(x, y)
  29. plt.show()
  30. plt.hist(rms[0], color = 'red', edgecolor = 'black', bins = 10)
  31. plt.show()
  32. plt.hist(cent[0], color = 'blue', edgecolor = 'black', bins = int(cent.max()/100))
  33. plt.show()
  34. plt.hist(zcr[0], color = 'green', edgecolor = 'black', bins = 10)
  35. plt.show()
  36. plt.figure(figsize=(150, 17))
  37. t = librosa.frames_to_time(range(zcr.size), hop_length=flength)
  38. ax = plt.subplot(3, 1, 1)
  39. plt.xticks(np.arange(0, 12103, step=1))
  40. plt.plot(t, zcr[0], color="r")
  41. plt.ylim((zcr.min(), zcr.max()))
  42. plt.title("ZCR")
  43. plt.show()
  44. def f(t):
  45. return 1.0025+0.002*np.sin(1.25*3.14*t/30)
  46. #return x ** 2
  47. def sig(x):
  48. return 1/(1 + np.exp(-12*(x-0.431)))
  49. def transformXYrandom(x):
  50. return random.random()*10*x-5;
  51. strength_min = 0.4
  52. strength_max = 0.73
  53. # Assuming that into "generated" directoruy U've already create "project_name" subfolder
  54. #audio_input_file = '/home/lalo/data/studio_suono/ardourprojects/space/export/star_r1_sessione_20240310_msub07.wav'
  55. audio_input_file = "/home/luca-conte/Musica/ardour_projects/end_titles/export/end_titles_draft_r1_sessione.wav"
  56. #audio_input_file = '/home/lalo/data/studio_grafica/deforum/wilson_r1_mm_sample2.wav'
  57. #audio_input_file = '/home/lalo/data/studio_suono/spx/231104_001_m01.wav'
  58. #audio_input_file = '/home/lalo/data/studio_grafica/deforum/eucrasy_r1_sample.wav'
  59. #audio_input_file = '/home/lalo/data/studio_grafica/deforum/eucrasy_r1.wav'
  60. #audio_input_file = '/home/lalo/data/studio_grafica/deforum/eucrasy_short_r1.wav'
  61. #audio_input_file = '/home/lalo/data/studio_suono/231014_002_mastered_r2_clip.WAV'
  62. #audio_input_file = 'C:/Users/LucaConte/Music/lc_music/wilson_r1_mm.wav'
  63. # Store the sampling rate as `sr`
  64. fps = 24
  65. flength = 22050//fps
  66. #audio frame size is 22050/30=735
  67. wave, sr = librosa.load(audio_input_file)
  68. rms = librosa.feature.rms(y=wave, frame_length=flength, hop_length=flength)
  69. zcr = librosa.feature.zero_crossing_rate(y=wave,frame_length=flength,hop_length=flength)
  70. #rms = librosa.feature.rms(y=wave, frame_length=735, hop_length=735)
  71. cent = librosa.feature.spectral_centroid(y=wave, sr=sr,n_fft=flength, hop_length=flength)
  72. duration = int(math.ceil(librosa.get_duration(y=wave, sr=sr)))
  73. frames = duration * fps
  74. print("RMS SIZE: " + str(len(rms[0])))
  75. print("Rms min/max: " + str(rms.min()) + "/" + str(rms.max()))
  76. print("CEN SIZE: " + str(len(cent[0])))
  77. print("Cent min/max: " + str(cent.min()) + "/" + str(cent.max()))
  78. print("TOTAL FRAMES: " + str(frames))
  79. print("Audio duration in secs: " + str(duration))
  80. #
  81. # sr.len /22050 = duration
  82. # rms.len * 30 = duration
  83. # rms[i] mi da la potenza media del frame iesimo
  84. trms= rms[0]
  85. m = interp1d([min(trms), max(trms)], [strength_min, strength_max])
  86. c = interp1d([cent.min(), cent.max()], [-0.5, 0.5])
  87. # Creating the sctipt file (the real unique aoutput of this script)
  88. try:
  89. print("")
  90. print("------RMS---------")
  91. print("")
  92. for frame in range(len(rms[0])):
  93. print(str(frame) + ":(" + str(trms[frame]) + "),", end="")
  94. print("")
  95. print("-------CENTROIDS--------")
  96. print("")
  97. for frame in range(cent.size):
  98. print(str(frame) + ":(" + str(cent[0][frame]) + "),", end="")
  99. print("")
  100. print("-----TRANSFORM----------")
  101. print("")
  102. for frame in range(cent.size):
  103. print(str(frame) + ":(" + str(transformXYrandom(1)) + "),", end="")
  104. print("")
  105. print("-----ZERO CROSSING RATE----------")
  106. print("")
  107. for frame in range(zcr.size):
  108. print(str(frame) + ":(" + str(zcr[0][frame]) + "),", end="")
  109. print("RMS SIZE: " + str(len(rms[0])))
  110. print("Rms min/max: " + str(rms.min()) + "/" + str(rms.max()))
  111. print("Rms mean: " + str(np.mean(rms[0])))
  112. print("CEN SIZE: " + str(len(cent[0])))
  113. print("Cent min/max: " + str(cent.min()) + "/" + str(cent.max()))
  114. print("TOTAL FRAMES: " + str(frames))
  115. print("Audio duration in secs: " + str(duration))
  116. except:
  117. print(exception)
  118. plotting_stuffs()