fixing config options and adding uuid4 naming

This commit is contained in:
ange committed 2025-03-06 02:58:35 +01:00
1 parent ec88bd442c
commit 842402bbd8
6 files changed
+55 -36

No files matched your search

+4 -4
View File
@@ -42,18 +42,18 @@ class EventLoop:
try:
event = self.event_queue.get(timeout=0.5)
if (event.ray_event == Events.RayEventType.PickUp):
print("EventLoop() : phone was pickup")
# print("EventLoop() : phone was pickup")
self.trigger_main_sequence()
# print("playing OK")
elif (event.ray_event == Events.RayEventType.HangUp):
print("EventLoop() : phone was Hang Up")
# print("EventLoop() : phone was Hang Up")
self.stop_main_sequence()
self.stop_record()
elif(event.ray_event == Events.RayEventType.MessagePlayFinished):
print("EventLoop() : Audio message read. Ready to record")
# print("EventLoop() : Audio message read. Ready to record")
self.record()
elif(event.ray_event == Events.RayEventType.RecorderTimeout):
print("EventLoop() : Recorder timeout")
# print("EventLoop() : Recorder timeout")
pass
# trigger main sequence
+3 -3
View File
@@ -13,7 +13,7 @@ class PhyEventLoop:
def __init__(self, queue, config):
self.device_path = config['phy-event']['gpio-dev-input']
self.pickup_state = int(config["phy-event"]["pickup-state"])
print(f"PhyEventLoop() : current state {self.pickup_state}")
# print(f"PhyEventLoop() : current state {self.pickup_state}")
self.device = evdev.InputDevice(self.device_path)
self.event_queue = queue
self.running = False
@@ -30,12 +30,12 @@ class PhyEventLoop:
if event:
# print(event)
if (event.type == evdev.ecodes.EV_KEY and event.code == evdev.ecodes.KEY_F12):
print(f"PhyEventLoop() : Event received -> {event}")
# print(f"PhyEventLoop() : Event received -> {event}")
key_ev = Events.RayEvent(producer=self, producer_type=Events.EventProducerType.Keyboard, ray_event=Events.RayEventType.HangUp)
if (event.value == self.pickup_state):
key_ev.ray_event = Events.RayEventType.PickUp
# print(f"ending value {key_ev.ray_event.name}")
print(f"PhyEventLoop() : sending -> {key_ev.ray_event.name}")
# print(f"PhyEventLoop() : sending -> {key_ev.ray_event.name}")
self.event_queue.put(key_ev)
except queue.Empty:
pass
+8 -7
View File
@@ -3,7 +3,7 @@ import threading
import pyaudio
import wave
import time
import os
import threading
from RayPonder import Events
@@ -13,9 +13,10 @@ from RayPonder import Events
class Player():
def __init__(self, config, queue):
self.evt_qu = queue
self.config = config
self.config = config['player']
# TODO mettre filename dans fichier de config
self.filename = './messages/Cabine6.wav'
self.filename = os.path.join( config['general']['root'] , self.config['message-path'] , self.config['start-message'])
# print(f"Player() : Filename -> {self.filename}")
self.p = pyaudio.PyAudio()
self.playing = False
self.stopped = True
@@ -64,7 +65,7 @@ class Player():
# print(f"remaining {wf.tell()}")
if(wf.tell() == wf.getnframes()):
#need to send a message to say we can record....
print("PLAYER() : sending EndOfMessage")
# print("PLAYER() : sending EndOfMessage")
evt = Events.RayEvent(producer=self, ray_event=Events.RayEventType.MessagePlayFinished)
self.evt_qu.put(evt)
self.stream.stop_stream()
@@ -76,20 +77,20 @@ class Player():
def play(self):
self.playing = True
self.stopped = False
print(f"PLAYER() : playing {self.playing}, stopped {self.stopped}")
# print(f"PLAYER() : playing {self.playing}, stopped {self.stopped}")
# print(f"not self.running {not self.running}")
def stop(self):
self.playing = False
self.stopped = True
print(f"PLAYER() : playing {self.playing}, stopped {self.stopped}")
# print(f"PLAYER() : playing {self.playing}, stopped {self.stopped}")
def clean_up(self):
self.running = False
self.stop = self.playing = False
print(f"PLAYER() :playing : {self.playing}, stopped : {self.stopped}")
# print(f"PLAYER() :playing : {self.playing}, stopped : {self.stopped}")
self.p.terminate()
self.stream.close()
print("PLAYER() : Player closed gracefully")
+25 -17
View File
@@ -6,6 +6,7 @@ from datetime import datetime
import threading
import time
from RayPonder import Events
import uuid
class Recorder:
def __init__(self, config = None, queue = None):
@@ -14,18 +15,24 @@ class Recorder:
self.recording = False
self.running = False
self.p = pyaudio.PyAudio()
self.dest_dir = os.path.join(os.getcwd(), self.config['dest-dir'])
self.file_name_format = self.config['strftime-template'] + self.config['file-prepend'] + self.config['file-ext']
print(f"RECORDER() : dest dir {self.dest_dir}")
print(f"RECORDER() : file name format {self.file_name_format}")
print(f"RECORDER() : filename example {self.format_name()}")
self.generate_uuid = self.config.getboolean('add-uuid')
self.dest_dir = os.path.join(config['general']['root'], self.config['dest-dir'])
self.file_name_format = self.config['strftime-template'] + self.config['file-prepend']
# print(f"RECORDER() : dest dir {self.dest_dir}")
# print(f"RECORDER() : file name format {self.file_name_format}")
# print(f"RECORDER() : filename example {self.format_name()}")
self.chunk_size = 1024
self.framerate = int(self.config['framerate'])
self.max_duration = int(self.config['recode-timeout']) * 60 # in seconds
self.channels = int(self.config['channels'])
def format_name(self):
return datetime.now().strftime(self.file_name_format)
filename = datetime.now().strftime(self.file_name_format)
if self.generate_uuid:
f_id = uuid.uuid4().hex
filename = filename + "_" + f_id[0:6]
# print(f"uuid {f_id} \n{filename}")
return filename + self.config['file-ext']
def start(self):
self.running = True
@@ -61,10 +68,10 @@ class Recorder:
data = stream.read(self.chunk_size)
buff.append(data)
duration -= 1
print(f"RECORDER() : remaining {self.get_remaining(duration)}")
# print(f"RECORDER() : remaining {self.get_remaining(duration)}")
if(not self.recording):
timeout = False
print("RECORDER() : stop recording")
# print("RECORDER() : stop recording")
break
if(duration == 0):
self.recording = False
@@ -76,7 +83,6 @@ class Recorder:
self.queue.put(evt_timeout)
stream.stop_stream()
stream.close()
wf = wave.open(filename, 'wb')
wf.setnchannels(2)
wf.setsampwidth(self.p.get_sample_size(pyaudio.paInt32))
@@ -86,26 +92,28 @@ class Recorder:
def get_remaining(self, duration):
print(f"RECODER() : duration in chunks {duration}")
return int( duration / self.chunk_size )
pass
def stop_recording(self):
self.recording = False
def record(self):
print("RECORDER() : Start Recording")
# print("RECORDER() : Start Recording")
self.recording = True
def shutdown(self):
def clean_up(self):
if(self.recording):
self.stop_recording()
self.running = False
self.clean_up()
def clean_up(self):
#free pyaudio
self.p.terminate()
print("RECORDER() : Player closed gracefully")
def shutdown(self):
self.clean_up()
self.thread.join()
if __name__ == "__main__":
recorder = Recorder()
+6 -5
View File
@@ -1,7 +1,8 @@
[general]
root= ""
root= /mnt/coope_drive/
[recorder]
dest-dir = records/
add-uuid = yes
dest-dir = record/
# put 2 percentage sign in template to avoir configparser error
strftime-template = %%Y%%m%%d_%%Hh%%Mm%%Ss_
file-ext = .wav
@@ -11,10 +12,10 @@ framerate = 48000
#not used
format = 32
# integer in minutes
recode-timeout = 2
recode-timeout = 10
[player]
message-path = message/
start-message = Cabine6.wav
message-path = messages/
start-message = message01.wav
[phy-event]
gpio-dev-input = /dev/input/event0
pickup-state = 0
+9
View File
@@ -3,11 +3,20 @@ import configparser
#from configobj import ConfigObj
from RayPonder.EventLoop import EventLoop
import threading
import os
import sys
def main():
config = configparser.ConfigParser()
config.read('ray-ponder.conf')
root = config['general']['root']
if(os.path.exists(root) ):
print("ROOT EXISTS OK")
else:
print("ERROR !!!\nRoot folder does not exists. Check config file.")
sys.exit(20)
event_loop = EventLoop(config)
event_loop.start()
event_loop.run()