Nouveau post car cette version n'est pas compatible avec l'ancienne !
http://www.purebasic.fr/french/viewtopic.php?t=9027
J'ai été obligé de poster le code de l'éditeur sans balises(voir post suivant) car il y avait un problème Désolé!
Changements par rapport à l'ancienne version :
-de nouvelles modulations
-possibilité de charger des paramètres avec un copier-coller
-petit nettoyage du code (j'espère que j'ai pas frotté trop fort , j'ai utilisé EnableExplicit, ça m'a permis de supprimer des variables inutilisées)
En attendant, voici le nouveau lecteur :
(si vous mettez plusieurs sons, n'oubliez pas de changer les deux premiers paramètres de creation_wav(0,*son,....)!)
Code : Tout sélectionner
;pure_ADSR_procedure
#largeur_ecran=280
#hauteur_ecran=100
Declare creation_wav(ID,*son,type_signal.b,duree.f,freq_A.f,freq_B.f,freq_C.f,freq_D.f,freq_E.f,x_B.f,x_C.f,x_D.f,x_E.f,ampl_A.f,ampl_B.f,ampl_C.f,ampl_d.f,ampl_E.f,mod_ampl_A.b,mod_ampl_B.b,mod_ampl_C.b,mod_ampl_D.b,mod_freq_A.b,mod_freq_B.b,mod_freq_C.b,mod_freq_D.b,n_AB.b,n_BC.b,n_CD.b,n_DE.b,m_AB.b,m_BC.b,m_CD.b,m_DE.b)
;-****************************************************************************************
;-PROGRAMME PRINCIPAL
InitSound()
OpenWindow(0, 0, 0, #largeur_ecran, #hauteur_ecran, "Exemple d'utilisation de pure_ADSR", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
creation_wav(0,*son,4,5967,20,388,440,536,20,25181,112234,244138,263145,33,32767,23483,18750,33,3,4,5,8,2,3,4,2,5,6,87,12,20,22,40,5)
PlaySound(0)
;
;-BOUCLE PRINCIPALE
Repeat
Repeat
Event = WindowEvent()
Select Event
Case #PB_Event_CloseWindow
End
EndSelect
Until Event = 0
Delay(1)
ForEver
;-****************************************************************************************
Macro affine(a,b,xA,yA,xB,yB)
a.f=(yB-yA)/(xB-xA)
b.f=(yA*xB-yB*xA)/(xB-xA)
EndMacro
Macro exp(x)
Pow(#e,x)
EndMacro
Macro exponentielle(a,b,xA,yA,xB,yB)
;test pour les amplitudes yA nulles!
If yA=0
yA=0.001*ampl_max
EndIf
If yB=0
yB=0.001*ampl_max
EndIf
b.f=Log(yB/yA)/(xB-xA)
a.f=yA*exp(b*xA)
EndMacro
Macro hyperbole(a,b,xA,yA,xB,yB)
a.f=(yA-yB)/((yA*yB*(xB-xA)))
b.f=(xB*yB-xA*yA)/((yA*yB*(xB-xA)))
EndMacro
Macro modulation_amplitude(type_mod_ampl,ampl,a_aff,b_aff,a_exp,x_ech_point_1,x_ech_point_2,b_exp,n)
Select type_mod_ampl
Case 0 ;aucune modulation
amplitude=ampl
Case 1 ;affine
amplitude=(a_aff*x_echantillon+b_aff)
Case 2 ;exponentielle
amplitude=(a_exp*exp((x_echantillon-x_ech_point_1)*b_exp))
Case 3 ;exponentielle2
amplitude=ampl+(a_exp*exp((x_ech_point_2-x_ech_point_1)*b_exp))*(1-exp(-(x_echantillon-x_ech_point_1)*b_exp))
Case 4 ;modulation sin(kt*n/durée_portion)
;x_echantillon/frequence_echantillonnage=temps en seconde car 44100 échantillons correspondent à 1s
amplitude=(a_aff*x_echantillon+b_aff)*(0.5+0.5*Sin(2 * #PI *n/(x_ech_point_2-x_ech_point_1)*x_echantillon))
Case 5 ;modulation sin(kt/nT)
amplitude=(a_aff*x_echantillon+b_aff)*(0.5+0.5*Sin(2 * #PI * frequence/n*x_echantillon/freq_ech))
Case 6 ; modulation scie(kt*n/durée_portion)
If x_echantillon=x_ech_point_1
x_origine_dent_mod=x_ech_point_1
nbre_ech_par_T_modulation=Int(Round((x_ech_point_2-x_ech_point_1)/n,#PB_Round_Nearest))
EndIf
If (x_echantillon-x_origine_dent_mod)%nbre_ech_par_T_modulation=0
x_origine_dent_mod=x_echantillon
nbre_ech_par_T_modulation=Int(Round((x_ech_point_2-x_ech_point_1)/n,#PB_Round_Nearest))
EndIf
amplitude=((a_aff*x_echantillon+b_aff)/nbre_ech_par_T_modulation)*(x_echantillon-x_origine_dent_mod)
Case 7 ;modulation scie(kt/nT)
If x_echantillon=x_ech_point_1
x_origine_dent_mod=x_ech_point_1
nbre_ech_par_T_modulation=Int(Round(freq_ech/(frequence/n),#PB_Round_Nearest))
EndIf
If (x_echantillon-x_origine_dent_mod)%nbre_ech_par_T_modulation=0
x_origine_dent_mod=x_echantillon
nbre_ech_par_T_modulation=Int(Round(freq_ech/(frequence/n),#PB_Round_Nearest))
EndIf
amplitude=((a_aff*x_echantillon+b_aff)/nbre_ech_par_T_modulation)*(x_echantillon-x_origine_dent_mod)
Case 8 ; modulation triangle(kt*n/durée_portion)
If x_echantillon=x_ech_point_1
x_origine_triangle_mod=x_ech_point_1
nbre_ech_par_T_modulation=Int(Round((x_ech_point_2-x_ech_point_1)/n,#PB_Round_Nearest))
alternance_mod.b=-1
EndIf
If (x_echantillon-x_origine_triangle_mod)%Int(nbre_ech_par_T_modulation*0.5)=0
nbre_ech_par_T_modulation=Int(Round((x_ech_point_2-x_ech_point_1)/n,#PB_Round_Nearest))
x_origine_triangle_mod=x_echantillon
alternance_mod=-alternance_mod
EndIf
amplitude=(a_aff*x_echantillon+b_aff)*(1-alternance_mod)*0.5+(2*(a_aff*x_echantillon+b_aff)/nbre_ech_par_T_modulation)*(x_echantillon-x_origine_triangle_mod)*alternance_mod
Case 9 ;modulation triangle(kt/nT)
If x_echantillon=x_ech_point_1
x_origine_triangle_mod=x_ech_point_1
nbre_ech_par_T_modulation=Int(Round(freq_ech/(frequence/n),#PB_Round_Nearest))
alternance_mod=-1
EndIf
If (x_echantillon-x_origine_triangle_mod)%Int(nbre_ech_par_T_modulation*0.5)=0
nbre_ech_par_T_modulation=Int(Round(freq_ech/(frequence/n),#PB_Round_Nearest))
x_origine_triangle_mod=x_echantillon
alternance_mod=-alternance_mod
EndIf
amplitude=(a_aff*x_echantillon+b_aff)*(1-alternance_mod)*0.5+(2*(a_aff*x_echantillon+b_aff)/nbre_ech_par_T_modulation)*(x_echantillon-x_origine_triangle_mod)*alternance_mod
EndSelect
EndMacro
Macro modulation_frequence(type_mod_freq,freq,a_aff,b_aff,a_exp,x_ech_point_1,x_ech_point_2,b_exp,m,a_hyp,b_hyp))
Select type_mod_freq
Case 0 ;aucune modulation
frequence=freq
Case 1 ;affine
frequence=(a_aff*x_echantillon+b_aff)
Case 2 ;hyperbolique
frequence=1/(a_hyp*x_echantillon+b_hyp)
Case 3 ;exponentielle
frequence=(a_exp*exp((x_echantillon-x_ech_point_1)*b_exp))
Case 4 ;exponentielle2
frequence=freq+(a_exp*exp((x_ech_point_2-x_ech_point_1)*b_exp))*(1-exp(-(x_echantillon-x_ech_point_1)*b_exp))
Case 5 ;modulation sin(kt*n/durée_portion)
;x_echantillon/frequence_echantillonnage=temps en seconde car 44100 échantillons correspondent à 1s
frequence=(a_aff*x_echantillon+b_aff)*(0.6+0.4*Sin(2 * #PI *m/(x_ech_point_2-x_ech_point_1)*x_echantillon))
Case 6 ; modulation scie(kt*n/durée_portion)
If x_echantillon=x_ech_point_1
x_origine_dent_mod_freq=x_ech_point_1
nbre_ech_par_T_modulation_freq=Int(Round((x_ech_point_2-x_ech_point_1)/m,#PB_Round_Nearest))
EndIf
If (x_echantillon-x_origine_dent_mod_freq)%nbre_ech_par_T_modulation_freq=0
x_origine_dent_mod_freq=x_echantillon
nbre_ech_par_T_modulation_freq=Int(Round((x_ech_point_2-x_ech_point_1)/m,#PB_Round_Nearest))
EndIf
frequence=((a_aff*x_echantillon+b_aff)/nbre_ech_par_T_modulation_freq)*(x_echantillon-x_origine_dent_mod_freq)+#frequence_min
Case 7 ; modulation triangle(kt*n/durée_portion)
If x_echantillon=x_ech_point_1
x_origine_triangle_mod_freq=x_ech_point_1
nbre_ech_par_T_modulation_freq=Int(Round((x_ech_point_2-x_ech_point_1)/m,#PB_Round_Nearest))
alternance_mod_freq.b=-1
EndIf
If (x_echantillon-x_origine_triangle_mod_freq)%Int(nbre_ech_par_T_modulation_freq*0.5)=0
nbre_ech_par_T_modulation_freq=Int(Round((x_ech_point_2-x_ech_point_1)/m,#PB_Round_Nearest))
x_origine_triangle_mod_freq=x_echantillon
alternance_mod_freq=-alternance_mod_freq
EndIf
frequence=(a_aff*x_echantillon+b_aff)*(1-alternance_mod_freq)*0.5+(2*(a_aff*x_echantillon+b_aff)/nbre_ech_par_T_modulation_freq)*(x_echantillon-x_origine_triangle_mod_freq)*alternance_mod_freq+#frequence_min
Case 8 ;aléatoire
frequence=#frequence_min+Random(freq)
EndSelect
EndMacro
Procedure creation_wav(ID,*son,type_signal.b,duree.f,freq_A.f,freq_B.f,freq_C.f,freq_D.f,freq_E.f,x_B.f,x_C.f,x_D.f,x_E.f,ampl_A.f,ampl_B.f,ampl_C.f,ampl_d.f,ampl_E.f,mod_ampl_A.b,mod_ampl_B.b,mod_ampl_C.b,mod_ampl_D.b,mod_freq_A.b,mod_freq_B.b,mod_freq_C.b,mod_freq_D.b,n_AB.b,n_BC.b,n_CD.b,n_DE.b,m_AB.b,m_BC.b,m_CD.b,m_DE.b)
#frequence_min=20
#frequence_max=10000
#e=2.71828182845904
amplitude.f
ampl_max.f=32767 ;Pow(2, #bitrate-1)-1
frequence.f
freq_ech.f=44100
x_A.f=0
nbre_ech_par_periode=freq_ech/freq_A
alternance.b=-1
;-modulation affine des amplitudes (y=a*x+b)
affine(a_ampl_AB_aff,b_ampl_AB_aff,x_A,ampl_A,x_B,ampl_B)
affine(a_ampl_BC_aff,b_ampl_BC_aff,x_B,ampl_B,x_C,ampl_C)
affine(a_ampl_CD_aff,b_ampl_CD_aff,x_C,ampl_C,x_D,ampl_d)
affine(a_ampl_DE_aff,b_ampl_DE_aff,x_D,ampl_d,x_E,ampl_E)
;-modulation exponentielle des amplitudes (y=a*exp(b*x)) !!!!penser au changement d'origine pour le calcul de a et b
exponentielle(a_ampl_AB_exp,b_ampl_AB_exp,0,ampl_A,x_B,ampl_B)
exponentielle(a_ampl_BC_exp,b_ampl_BC_exp,0,ampl_B,x_C-x_B,ampl_C)
exponentielle(a_ampl_CD_exp,b_ampl_CD_exp,0,ampl_C,x_D-x_C,ampl_d)
exponentielle(a_ampl_DE_exp,b_ampl_DE_exp,0,ampl_d,x_E-x_D,ampl_E)
;-modulation affine des fréquences (f=a*x+b)
affine(a_freq_AB_aff,b_freq_AB_aff,x_A,freq_A,x_B,freq_B)
affine(a_freq_BC_aff,b_freq_BC_aff,x_B,freq_B,x_C,freq_C)
affine(a_freq_CD_aff,b_freq_CD_aff,x_C,freq_C,x_D,freq_D)
affine(a_freq_DE_aff,b_freq_DE_aff,x_D,freq_D,x_E,freq_E)
;-modulation exponentielle des fréquences (y=a*exp(b*x)) !!!!penser au changement d'origine pour le calcul de a et b
exponentielle(a_freq_AB_exp,b_freq_AB_exp,x_A,freq_A,x_B,freq_B)
exponentielle(a_freq_BC_exp,b_freq_BC_exp,0,freq_B,x_C-x_B,freq_C)
exponentielle(a_freq_CD_exp,b_freq_CD_exp,0,freq_C,x_D-x_C,freq_D)
exponentielle(a_freq_DE_exp,b_freq_DE_exp,0,freq_D,x_E-x_D,freq_E)
;-modulation hyperbolique des fréquences (f=1/(a*x+b))
hyperbole(a_freq_AB_hyp,b_freq_AB_hyp,x_A,freq_A,x_B,freq_B)
hyperbole(a_freq_BC_hyp,b_freq_BC_hyp,x_B,freq_B,x_C,freq_C)
hyperbole(a_freq_CD_hyp,b_freq_CD_hyp,x_C,freq_C,x_D,freq_D)
hyperbole(a_freq_DE_hyp,b_freq_DE_hyp,x_D,freq_D,x_E,freq_E)
#samplerate = 44100 ; samplerate
#bitrate = 16 ; Bits per sample, #bitrate Mod 8 must be 0 !
#channels = 2 ; number of channels
totalsamples.l =(duree/1000)*#samplerate ; length in samples
nbre_ech_total.l=totalsamples
#avBytesPerSec = #channels*#bitrate/8*#samplerate ; calculate the average bytes per second
datachunkBytes.l = totalsamples * #channels * #bitrate/8 +1
Structure PBUnion
StructureUnion
b.b
w.w
l.l
f.f
s.s
EndStructureUnion
EndStructure
*son = AllocateMemory(44 + datachunkBytes)
*mem.PBUnion = *son
*mem\l = 'FFIR' : *mem+4 ; riff-chunk-ID "RIFF"
*mem\l = 36 + datachunkBytes : *mem+4 ; normally filesize minus (these) 8 Bytes
*mem\l = 'EVAW' : *mem+4 ; wave-chunk-ID "WAVE"
*mem\l = ' tmf' : *mem+4 ; format-chunk-ID "FMT "
*mem\l = 16 : *mem+4 ; chunk data size (+ Extra Format Bytes)
*mem\w = 1 : *mem+2 ; compression code
*mem\w = #channels : *mem+2 ; number of channels
*mem\l = #samplerate : *mem+4 ; samplerate
*mem\l = #avBytesPerSec : *mem+4 ; average bytes per second, (channels)*(block align)*(samplerate)
*mem\w = #bitrate/8*#channels : *mem+2 ; Block Align ('bytes per sample')
*mem\w = #bitrate : *mem+2 ; Bits per sample
*mem\l = 'atad' : *mem+4 ; data-chunk-ID "DATA"
*mem\l = datachunkBytes : *mem+4 ; data chunk size in bytes
;/ Now lets write #totalsamples
;/ work only for 16 Bit or more. 8-Bit has zero-line at value 128, so it is unsigned
For x_echantillon=0 To nbre_ech_total
For actchannel = 1 To #channels
If x_echantillon<x_B
modulation_amplitude(mod_ampl_A,ampl_A,a_ampl_AB_aff,b_ampl_AB_aff,a_ampl_AB_exp,x_A,x_B,b_ampl_AB_exp,n_AB)
modulation_frequence(mod_freq_A,freq_A, a_freq_AB_aff,b_freq_AB_aff,a_freq_AB_exp,x_A,x_B,b_freq_AB_exp,m_AB,a_freq_AB_hyp,b_freq_AB_hyp)
ElseIf x_echantillon>=x_B And x_echantillon<x_C
modulation_amplitude(mod_ampl_B,ampl_B,a_ampl_BC_aff,b_ampl_BC_aff,a_ampl_BC_exp,x_B,x_C,b_ampl_BC_exp,n_BC)
modulation_frequence(mod_freq_B,freq_B, a_freq_BC_aff,b_freq_BC_aff,a_freq_BC_exp,x_B,x_C,b_freq_BC_exp,m_BC,a_freq_BC_hyp,b_freq_BC_hyp)
ElseIf x_echantillon>=x_C And x_echantillon<x_D
modulation_amplitude(mod_ampl_C,ampl_C,a_ampl_CD_aff,b_ampl_CD_aff,a_ampl_CD_exp,x_C,x_D,b_ampl_CD_exp,n_CD)
modulation_frequence(mod_freq_C,freq_C, a_freq_CD_aff,b_freq_CD_aff,a_freq_CD_exp,x_C,x_D,b_freq_CD_exp,m_CD,a_freq_CD_hyp,b_freq_CD_hyp)
Else
modulation_amplitude(mod_ampl_D,ampl_d,a_ampl_DE_aff,b_ampl_DE_aff,a_ampl_DE_exp,x_D,x_E,b_ampl_DE_exp,n_DE)
modulation_frequence(mod_freq_D,freq_D, a_freq_DE_aff,b_freq_DE_aff,a_freq_DE_exp,x_D,x_E,b_freq_DE_exp,m_DE,a_freq_DE_hyp,b_freq_DE_hyp)
EndIf
Select type_signal
;signal sinusoïdal
Case 0
*mem\w=amplitude*Sin(2 * #PI * frequence*x_echantillon/freq_ech) : *mem+2
;signal carré
Case 1
If x_echantillon%Int(nbre_ech_par_periode*0.5)=0
nbre_ech_par_periode=Int(Round(freq_ech/frequence,#PB_Round_Nearest))
alternance=-alternance
EndIf
*mem\w=amplitude*alternance : *mem+2
;signal triangle
Case 2
If x_echantillon%Int(nbre_ech_par_periode*0.5)=0
nbre_ech_par_periode=Int(Round(freq_ech/frequence,#PB_Round_Nearest))
x_origine_triangle=x_echantillon
alternance=-alternance
EndIf
*mem\w=(4*(amplitude/nbre_ech_par_periode)*(x_echantillon-x_origine_triangle)-amplitude)*alternance : *mem+2
;signal dents de scie
Case 3
If x_echantillon%Int(nbre_ech_par_periode)=0
nbre_ech_par_periode=Int(Round(freq_ech/frequence,#PB_Round_Nearest))
x_origine_dent=x_echantillon
EndIf
*mem\w=2*(amplitude/nbre_ech_par_periode)*(x_echantillon-x_origine_dent)-amplitude : *mem+2
;signal bruit sinusoïdal
Case 4
*mem\w=Random(amplitude)*Sin(2 * #PI * frequence*x_echantillon/freq_ech) : *mem+2
;signal bruit carré
Case 5
If x_echantillon%Int(nbre_ech_par_periode*0.5)=0
nbre_ech_par_periode=Int(Round(freq_ech/frequence,#PB_Round_Nearest))
alternance=-alternance
EndIf
*mem\w=Random(amplitude)*alternance : *mem+2
EndSelect
Next actchannel
Next x_echantillon
CatchSound(ID, *son)
EndProcedure
Hasta la vista