AudioToDeforumKeyframes.py 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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. def sig(x):
  60. return 1/(1 + np.exp(-12*(x-0.431)))
  61. strength_min = 0.4
  62. strength_max = 0.6
  63. # Assuming that into "generated" directoruy U've already create "project_name" subfolder
  64. #audio_input_file = '/home/lalo/data/studio_grafica/deforum/wilson_r1_mm_sample.wav'
  65. #audio_input_file = '/home/lalo/data/studio_suono/spx/231104_001_m01.wav'
  66. #audio_input_file = '/home/lalo/data/studio_grafica/deforum/eucrasy_r1_sample.wav'
  67. #audio_input_file = '/home/lalo/data/studio_grafica/deforum/eucrasy_r1.wav'
  68. #audio_input_file = '/home/lalo/data/studio_suono/231014_002_mastered_r2_clip.WAV'
  69. audio_input_file = 'C:/Users/LucaConte/Music/lc_music/wilson_r1_mm.wav'
  70. # Store the sampling rate as `sr`
  71. fps = 24
  72. flength = 22050//fps
  73. #audio frame size is 22050/30=735
  74. wave, sr = librosa.load(audio_input_file)
  75. rms = librosa.feature.rms(y=wave, frame_length=flength, hop_length=flength)
  76. #rms = librosa.feature.rms(y=wave, frame_length=735, hop_length=735)
  77. cent = librosa.feature.spectral_centroid(y=wave, sr=sr,n_fft=flength, hop_length=flength)
  78. duration = int(math.ceil(librosa.get_duration(y=wave, sr=sr)))
  79. frames = duration * fps
  80. print("RMS SIZE: " + str(len(rms[0])))
  81. print("Rms min/max: " + str(rms.min()) + "/" + str(rms.max()))
  82. print("CEN SIZE: " + str(len(cent[0])))
  83. print("Cent min/max: " + str(cent.min()) + "/" + str(cent.max()))
  84. print("TOTAL FRAMES: " + str(frames))
  85. print("Audio duration in secs: " + str(duration))
  86. #
  87. # sr.len /22050 = duration
  88. # rms.len * 30 = duration
  89. # rms[i] mi da la potenza media del frame iesimo
  90. m = interp1d([rms.min(), rms.max()], [strength_min, strength_max])
  91. c = interp1d([cent.min(), cent.max()], [-0.5, 0.5])
  92. # Creating the sctipt file (the real unique aoutput of this script)
  93. try:
  94. for frame in range(len(rms[0])):
  95. print(str(frame) + ":(" + str(1-m(rms[0][frame])) + "),", end="")
  96. print("")
  97. print("---------------")
  98. print("")
  99. for frame in range(cent.size):
  100. print(str(frame) + ":(" + str(cent[0][frame]) + "),", end="")
  101. except:
  102. print(exception)
  103. plotting_stuffs()