AudioToDeforumKeyframes.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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 math
  13. from scipy.interpolate import interp1d
  14. from scipy.interpolate import UnivariateSpline
  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.ylim((-1, 1))
  23. plt.title("Wave&Featurres")
  24. plt.show()
  25. x = np.linspace(1, 4000, 4000)
  26. y = f(x)
  27. #plt.plot(x, y)
  28. #plt.show()
  29. plt.hist(rms[0], color = 'red', edgecolor = 'black', bins = 10)
  30. plt.show()
  31. # Generate some example data
  32. data =cent[0]
  33. # Create a histogram
  34. hist, bin_edges = np.histogram(data, bins=100, density=True)
  35. # Calculate bin centers
  36. bin_centers = 0.5 * (bin_edges[:-1] + bin_edges[1:])
  37. # Perform spline interpolation on the histogram data
  38. spline = UnivariateSpline(bin_centers, hist, s=0)
  39. # Create a finer x-axis for plotting the interpolated function
  40. x_interp = np.linspace(min(bin_centers), max(bin_centers), 1000)
  41. y_interp = spline(x_interp)
  42. # Plot the histogram and the interpolated function
  43. plt.hist(data, bins=100, density=True, alpha=0.6, color='g', label='Histogram')
  44. plt.plot(x_interp, y_interp, label='Interpolated PDF', color='red')
  45. plt.legend()
  46. plt.xlabel('x')
  47. plt.ylabel('Probability Density')
  48. plt.title('Histogram and Interpolated PDF')
  49. # Obtain min and max values of the probability density
  50. min_density = np.min(hist)
  51. max_density = np.max(hist)
  52. print(f"Min Probability Density: {min_density}")
  53. print(f"Max Probability Density: {max_density}")
  54. plt.show()
  55. plt.show()
  56. def f(t):
  57. return 1.0025+0.002*np.sin(1.25*3.14*t/30)
  58. #return x ** 2
  59. strength_min = 0.4
  60. strength_max = 0.6
  61. # Assuming that into "generated" directoruy U've already create "project_name" subfolder
  62. #audio_input_file = '/home/lalo/data/studio_grafica/deforum/wilson_r1_mm_sample.wav'
  63. #audio_input_file = '/home/lalo/data/studio_suono/spx/231104_001_m01.wav'
  64. #audio_input_file = '/home/lalo/data/studio_grafica/deforum/eucrasy_r1_sample.wav'
  65. #audio_input_file = '/home/lalo/data/studio_grafica/deforum/eucrasy_r1.wav'
  66. #audio_input_file = '/home/lalo/data/studio_suono/231014_002_mastered_r2_clip.WAV'
  67. audio_input_file = 'C:/Users/LucaConte/Music/lc_music/wilson_r1_mm.wav'
  68. # Store the sampling rate as `sr`
  69. fps = 24
  70. flength = 22050//fps
  71. #audio frame size is 22050/30=735
  72. wave, sr = librosa.load(audio_input_file)
  73. rms = librosa.feature.rms(y=wave, frame_length=flength, hop_length=flength)
  74. #rms = librosa.feature.rms(y=wave, frame_length=735, hop_length=735)
  75. cent = librosa.feature.spectral_centroid(y=wave, sr=sr,n_fft=flength, hop_length=flength)
  76. duration = int(math.ceil(librosa.get_duration(y=wave, sr=sr)))
  77. frames = duration * fps
  78. print("RMS SIZE: " + str(len(rms[0])))
  79. print("Rms min/max: " + str(rms.min()) + "/" + str(rms.max()))
  80. print("CEN SIZE: " + str(len(cent[0])))
  81. print("Cent min/max: " + str(cent.min()) + "/" + str(cent.max()))
  82. print("TOTAL FRAMES: " + str(frames))
  83. print("Audio duration in secs: " + str(duration))
  84. #
  85. # sr.len /22050 = duration
  86. # rms.len * 30 = duration
  87. # rms[i] mi da la potenza media del frame iesimo
  88. m = interp1d([rms.min(), rms.max()], [strength_min, strength_max])
  89. c = interp1d([cent.min(), cent.max()], [-0.5, 0.5])
  90. # Creating the sctipt file (the real unique aoutput of this script)
  91. try:
  92. for frame in range(len(rms[0])):
  93. print(str(frame) + ":(" + str(1-m(rms[0][frame])) + "),", end="")
  94. print("")
  95. print("---------------")
  96. print("")
  97. for frame in range(cent.size):
  98. print(str(frame) + ":(" + str(cent[0][frame]) + "),", end="")
  99. except:
  100. print(exception)
  101. plotting_stuffs()