2023-11-08 11:19:38 +01:00
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Beat tracking example
# Appunti: buono per un video u po' psichedelico ci va:
# <lora:bb3l15:1> an abstract circuit board design with orange yellow green and purple squares over red background in the style of bb3l15
# su modello colorful_v30 (https://civitai.com/images/3024879?modelVersionId=188803&prioritizedUserIds=913950&period=AllTime&sort=Most+Reactions&limit=20 )
# <lora:Ink_scenery:1> black background, sketch jungle scenery, path, dense su sd 1.5
# Wilson <lora:Ink_scenery:1> black background, sketch jungle setting
import os
import numpy as np
import librosa
2023-11-30 07:15:50 +01:00
import random
2023-11-08 11:19:38 +01:00
import math
from scipy . interpolate import interp1d
import matplotlib . pyplot as plt
2023-11-08 13:03:47 +01:00
def plotting_stuffs ( ) :
plt . figure ( figsize = ( 15 , 17 ) )
t = librosa . frames_to_time ( range ( rms . size ) , hop_length = flength )
ax = plt . subplot ( 3 , 1 , 1 )
librosa . display . waveshow ( wave , alpha = 0.5 )
plt . plot ( t , rms [ 0 ] , color = " r " )
2023-11-14 16:24:49 +01:00
plt . plot ( t , trms , color = " b " )
2023-11-08 13:03:47 +01:00
plt . ylim ( ( - 1 , 1 ) )
plt . title ( " Wave&Featurres " )
plt . show ( )
2023-11-14 16:24:49 +01:00
x = np . linspace ( 0 , 1 , 100 )
y = sig ( x )
2023-11-08 13:03:47 +01:00
plt . plot ( x , y )
plt . show ( )
2023-11-08 23:52:36 +01:00
plt . hist ( rms [ 0 ] , color = ' red ' , edgecolor = ' black ' , bins = 10 )
plt . show ( )
plt . hist ( cent [ 0 ] , color = ' blue ' , edgecolor = ' black ' , bins = int ( cent . max ( ) / 100 ) )
plt . show ( )
2023-11-08 11:19:38 +01:00
def f ( t ) :
return 1.0025 + 0.002 * np . sin ( 1.25 * 3.14 * t / 30 )
#return x ** 2
2023-11-14 16:24:49 +01:00
def sig ( x ) :
return 1 / ( 1 + np . exp ( - 12 * ( x - 0.431 ) ) )
2023-11-08 11:19:38 +01:00
2023-11-30 07:15:50 +01:00
def transformXYrandom ( x ) :
return random . random ( ) * 10 * x - 5 ;
2024-06-03 10:42:31 +02:00
strength_min = 0.4
strength_max = 0.73
2023-11-08 11:19:38 +01:00
# Assuming that into "generated" directoruy U've already create "project_name" subfolder
2024-06-03 10:42:31 +02:00
audio_input_file = ' /home/lalo/data/studio_suono/ardourprojects/space/export/star_r1_sessione_20240310_msub07.wav '
#audio_input_file = '/home/lalo/data/studio_grafica/deforum/wilson_r1_mm_sample2.wav'
2023-11-08 11:19:38 +01:00
#audio_input_file = '/home/lalo/data/studio_suono/spx/231104_001_m01.wav'
#audio_input_file = '/home/lalo/data/studio_grafica/deforum/eucrasy_r1_sample.wav'
#audio_input_file = '/home/lalo/data/studio_grafica/deforum/eucrasy_r1.wav'
2023-11-14 16:24:49 +01:00
#audio_input_file = '/home/lalo/data/studio_grafica/deforum/eucrasy_short_r1.wav'
2023-11-08 11:19:38 +01:00
#audio_input_file = '/home/lalo/data/studio_suono/231014_002_mastered_r2_clip.WAV'
2023-11-08 23:52:36 +01:00
#audio_input_file = 'C:/Users/LucaConte/Music/lc_music/wilson_r1_mm.wav'
2023-11-08 11:19:38 +01:00
# Store the sampling rate as `sr`
fps = 24
flength = 22050 / / fps
#audio frame size is 22050/30=735
2023-11-08 13:03:47 +01:00
wave , sr = librosa . load ( audio_input_file )
rms = librosa . feature . rms ( y = wave , frame_length = flength , hop_length = flength )
2023-11-08 11:19:38 +01:00
2023-11-08 13:03:47 +01:00
#rms = librosa.feature.rms(y=wave, frame_length=735, hop_length=735)
cent = librosa . feature . spectral_centroid ( y = wave , sr = sr , n_fft = flength , hop_length = flength )
duration = int ( math . ceil ( librosa . get_duration ( y = wave , sr = sr ) ) )
2023-11-08 11:19:38 +01:00
frames = duration * fps
print ( " RMS SIZE: " + str ( len ( rms [ 0 ] ) ) )
2023-11-08 23:52:36 +01:00
print ( " Rms min/max: " + str ( rms . min ( ) ) + " / " + str ( rms . max ( ) ) )
2023-11-08 11:19:38 +01:00
print ( " CEN SIZE: " + str ( len ( cent [ 0 ] ) ) )
2023-11-08 23:52:36 +01:00
print ( " Cent min/max: " + str ( cent . min ( ) ) + " / " + str ( cent . max ( ) ) )
2023-11-08 11:19:38 +01:00
print ( " TOTAL FRAMES: " + str ( frames ) )
2023-11-08 23:52:36 +01:00
print ( " Audio duration in secs: " + str ( duration ) )
#
2023-11-08 11:19:38 +01:00
# sr.len /22050 = duration
# rms.len * 30 = duration
# rms[i] mi da la potenza media del frame iesimo
2023-11-14 16:24:49 +01:00
trms = rms [ 0 ]
2023-11-08 11:19:38 +01:00
2023-11-14 16:24:49 +01:00
m = interp1d ( [ min ( trms ) , max ( trms ) ] , [ strength_min , strength_max ] )
2023-11-08 11:19:38 +01:00
c = interp1d ( [ cent . min ( ) , cent . max ( ) ] , [ - 0.5 , 0.5 ] )
# Creating the sctipt file (the real unique aoutput of this script)
try :
2023-11-30 07:15:50 +01:00
print ( " " )
print ( " ------RMS--------- " )
print ( " " )
2023-11-08 11:19:38 +01:00
for frame in range ( len ( rms [ 0 ] ) ) :
2024-06-03 10:42:31 +02:00
print ( str ( frame ) + " :( " + str ( trms [ frame ] ) + " ), " , end = " " )
2023-11-08 11:19:38 +01:00
print ( " " )
2023-11-30 07:15:50 +01:00
print ( " -------CENTROIDS-------- " )
2023-11-08 11:19:38 +01:00
print ( " " )
2023-11-08 13:03:47 +01:00
for frame in range ( cent . size ) :
2023-11-08 11:19:38 +01:00
print ( str ( frame ) + " :( " + str ( cent [ 0 ] [ frame ] ) + " ), " , end = " " )
2023-11-30 07:15:50 +01:00
print ( " " )
print ( " -----TRANSFORM---------- " )
print ( " " )
for frame in range ( cent . size ) :
print ( str ( frame ) + " :( " + str ( transformXYrandom ( 1 ) ) + " ), " , end = " " )
2024-06-03 10:42:31 +02:00
print ( " RMS SIZE: " + str ( len ( rms [ 0 ] ) ) )
print ( " Rms min/max: " + str ( rms . min ( ) ) + " / " + str ( rms . max ( ) ) )
print ( " CEN SIZE: " + str ( len ( cent [ 0 ] ) ) )
print ( " Cent min/max: " + str ( cent . min ( ) ) + " / " + str ( cent . max ( ) ) )
print ( " TOTAL FRAMES: " + str ( frames ) )
print ( " Audio duration in secs: " + str ( duration ) )
2023-11-08 11:19:38 +01:00
except :
print ( exception )
2023-11-08 13:03:47 +01:00
plotting_stuffs ( )
2023-11-08 11:19:38 +01:00