76 lines
2.1 KiB
Python
Executable File
76 lines
2.1 KiB
Python
Executable File
#!/bin/env python3
|
|
|
|
"""
|
|
# Prérequis
|
|
|
|
* python3
|
|
* pigtail
|
|
```
|
|
pip3 install pigtail
|
|
```
|
|
|
|
# Utilisation
|
|
|
|
* Ajuster les valeurs des constantes LOG et WAV_OUTPUT en fonction des besoins
|
|
* LOG: Le log à jouer
|
|
* WAV_OUTPUT: Le fichier wav de sortie
|
|
* MAX_DURATION: La durée maximale d'une note
|
|
* EXCLUDE: Les caractères dont les valeurs sont ajoutées à cette liste ne seront pas traités
|
|
* Exécuter python3 text2wav.py
|
|
* Lire le fichier wav résultant avec un lecteur audio supportant le format wav
|
|
|
|
"""
|
|
import wave
|
|
import math
|
|
import struct
|
|
import random
|
|
from pygtail import Pygtail
|
|
|
|
LOG = '/var/log/httpd/access_log'
|
|
WAV_OUTPUT = '/home/doug/test/test.wav'
|
|
MAX_DURATION = 0.08
|
|
EXCLUDE = []
|
|
|
|
def sine_wave(frequency=444, framerate=44100, amplitude=0.5, duration=1):
|
|
"""
|
|
frequency: La fréquence à échantillonner
|
|
framerate: Le taux d'échantillonnage
|
|
amplitude: Le volume sonore (de 0 à 1)
|
|
duration: La durée de l'échantillon en seconde
|
|
"""
|
|
amplitude = amplitude * 32767
|
|
if amplitude > 32767:
|
|
amplitude = 32767
|
|
elif amplitude < 0:
|
|
amplitude = 0
|
|
|
|
data = []
|
|
for i in range(int(duration * framerate)):
|
|
value = int(amplitude*math.cos(2*frequency*math.pi*float(i)/float(framerate)))
|
|
data.append(value)
|
|
return struct.pack('<{}h'.format(len(data)), *data)
|
|
|
|
def notes_table(oord):
|
|
freq = math.floor(math.log(oord)*5000-20000)
|
|
duration = MAX_DURATION
|
|
return (freq, duration)
|
|
|
|
with wave.open(WAV_OUTPUT, 'wb') as wave_file:
|
|
nchannels = 1
|
|
sampwidth = 2
|
|
framerate = 44100
|
|
wave_file.setnchannels(nchannels)
|
|
wave_file.setsampwidth(sampwidth)
|
|
wave_file.setframerate(framerate)
|
|
|
|
try:
|
|
for line in Pygtail(LOG):
|
|
for letter in line:
|
|
oord = ord(letter)
|
|
freq, duration = notes_table(oord)
|
|
if oord not in EXCLUDE:
|
|
print("Caractère: {} | Ord: {} | Fréquence: {} | Durée: {}".format(letter, oord, freq, duration))
|
|
wave_file.writeframes(sine_wave(frequency=freq, duration=duration))
|
|
except Exception as e:
|
|
print('\033[91m{}\033[0m'.format(e))
|