AudioToDeforumKeyframes.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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. def f(t):
  35. return 1.0025+0.002*np.sin(1.25*3.14*t/30)
  36. #return x ** 2
  37. def sig(x):
  38. return 1/(1 + np.exp(-12*(x-0.431)))
  39. def transformXYrandom(x):
  40. return random.random()*10*x-5;
  41. strength_min = 0.4
  42. strength_max = 0.73
  43. # Assuming that into "generated" directoruy U've already create "project_name" subfolder
  44. audio_input_file = '/home/lalo/data/studio_suono/ardourprojects/space/export/star_r1_sessione_20240310_msub07.wav'
  45. #audio_input_file = '/home/lalo/data/studio_grafica/deforum/wilson_r1_mm_sample2.wav'
  46. #audio_input_file = '/home/lalo/data/studio_suono/spx/231104_001_m01.wav'
  47. #audio_input_file = '/home/lalo/data/studio_grafica/deforum/eucrasy_r1_sample.wav'
  48. #audio_input_file = '/home/lalo/data/studio_grafica/deforum/eucrasy_r1.wav'
  49. #audio_input_file = '/home/lalo/data/studio_grafica/deforum/eucrasy_short_r1.wav'
  50. #audio_input_file = '/home/lalo/data/studio_suono/231014_002_mastered_r2_clip.WAV'
  51. #audio_input_file = 'C:/Users/LucaConte/Music/lc_music/wilson_r1_mm.wav'
  52. # Store the sampling rate as `sr`
  53. fps = 24
  54. flength = 22050//fps
  55. #audio frame size is 22050/30=735
  56. wave, sr = librosa.load(audio_input_file)
  57. rms = librosa.feature.rms(y=wave, frame_length=flength, hop_length=flength)
  58. #rms = librosa.feature.rms(y=wave, frame_length=735, hop_length=735)
  59. cent = librosa.feature.spectral_centroid(y=wave, sr=sr,n_fft=flength, hop_length=flength)
  60. duration = int(math.ceil(librosa.get_duration(y=wave, sr=sr)))
  61. frames = duration * fps
  62. print("RMS SIZE: " + str(len(rms[0])))
  63. print("Rms min/max: " + str(rms.min()) + "/" + str(rms.max()))
  64. print("CEN SIZE: " + str(len(cent[0])))
  65. print("Cent min/max: " + str(cent.min()) + "/" + str(cent.max()))
  66. print("TOTAL FRAMES: " + str(frames))
  67. print("Audio duration in secs: " + str(duration))
  68. #
  69. # sr.len /22050 = duration
  70. # rms.len * 30 = duration
  71. # rms[i] mi da la potenza media del frame iesimo
  72. trms= rms[0]
  73. m = interp1d([min(trms), max(trms)], [strength_min, strength_max])
  74. c = interp1d([cent.min(), cent.max()], [-0.5, 0.5])
  75. # Creating the sctipt file (the real unique aoutput of this script)
  76. try:
  77. print("")
  78. print("------RMS---------")
  79. print("")
  80. for frame in range(len(rms[0])):
  81. print(str(frame) + ":(" + str(trms[frame]) + "),", end="")
  82. print("")
  83. print("-------CENTROIDS--------")
  84. print("")
  85. for frame in range(cent.size):
  86. print(str(frame) + ":(" + str(cent[0][frame]) + "),", end="")
  87. print("")
  88. print("-----TRANSFORM----------")
  89. print("")
  90. for frame in range(cent.size):
  91. print(str(frame) + ":(" + str(transformXYrandom(1)) + "),", end="")
  92. print("RMS SIZE: " + str(len(rms[0])))
  93. print("Rms min/max: " + str(rms.min()) + "/" + str(rms.max()))
  94. print("CEN SIZE: " + str(len(cent[0])))
  95. print("Cent min/max: " + str(cent.min()) + "/" + str(cent.max()))
  96. print("TOTAL FRAMES: " + str(frames))
  97. print("Audio duration in secs: " + str(duration))
  98. except:
  99. print(exception)
  100. plotting_stuffs()