33This file can be used to interactively generate tone sequences.
44"""
55
6+ from pathlib import Path
67import argparse
78import logging
89import sys
910from .box import Sequencer
10- from .freqs import DTMF , MF
11+ from . import get_mf , list_mf
1112
1213
1314def parse_args () -> argparse .Namespace :
@@ -39,27 +40,58 @@ def parse_args() -> argparse.Namespace:
3940 type = float ,
4041 default = 44100.0 ,
4142 help = 'The sample rate of the waveforms.' )
42- parser .add_argument (
43- '-c' , '--channels' ,
44- type = int ,
45- default = 1 ,
46- help = 'The number of channels in the waveforms.' )
4743 parser .add_argument (
4844 '-m' , '--mf' ,
4945 type = str ,
50- default = 'DTMF ' ,
51- help = 'The MF to use e.g. DTMF .' )
46+ default = 'dtmf ' ,
47+ help = 'The MF to use e.g. dtmf, mf .' )
5248 parser .add_argument (
5349 '-d' , '--debug' ,
5450 action = 'store_true' ,
5551 help = 'Enable debug logging.' )
56- parser .add_argument (
52+ # we can have sequence or file,pipe,stdin OR interactive
53+ group = parser .add_mutually_exclusive_group (required = True )
54+ group .add_argument (
55+ '-f' , '--file' ,
56+ type = Path ,
57+ help = 'The file to read the sequence from.' )
58+ group .add_argument (
59+ '-P' , '--pipe' ,
60+ type = Path ,
61+ help = 'Read the sequence from a pipe.' )
62+ group .add_argument (
63+ '-S' , '--stdin' ,
64+ action = 'store_true' ,
65+ help = 'Read the sequence from stdin.' )
66+ group .add_argument (
67+ '-i' , '--interactive' ,
68+ action = 'store_true' ,
69+ help = 'Enter interactive mode.' )
70+ group .add_argument (
5771 'sequence' ,
5872 type = str ,
59- help = 'The sequence to generate.' )
73+ nargs = '?' ,
74+ help = 'The sequence of tones to generate.' )
75+
6076 return parser .parse_args ()
6177
6278
79+ def bluebox_interactive (seq : Sequencer ) -> None :
80+ """Enter interactive mode."""
81+
82+ print ('Entering interactive mode. Type "exit" to quit.' )
83+ while True :
84+ try :
85+ seq (input ('Sequence: ' ))
86+ except KeyboardInterrupt :
87+ print ('Exiting...' )
88+ break
89+ except Exception as e :
90+ logging .error (e )
91+ if seq ._stop_on_error :
92+ break
93+
94+
6395def bluebox () -> None :
6496 """Generate a tone sequence.
6597
@@ -76,12 +108,11 @@ def bluebox() -> None:
76108 stop_on_error = False
77109 logging .basicConfig (level = logging .INFO )
78110
79- if args .mf == 'DTMF' :
80- mf = DTMF ()
81- elif args .mf == 'MF' :
82- mf = MF ()
111+ if args .mf in list_mf ():
112+ mf = get_mf (args .mf )()
83113 else :
84114 logging .error ('Invalid MF: %s' , args .mf )
115+ logging .error ('Valid MFs: %s' , ', ' .join (list_mf ()))
85116 sys .exit (1 )
86117
87118 seq = Sequencer (
@@ -90,9 +121,11 @@ def bluebox() -> None:
90121 length = args .length ,
91122 pause = args .pause ,
92123 sample_rate = args .sample_rate ,
93- channels = args . channels ,
124+ channels = 1 ,
94125 stop_on_error = stop_on_error )
95-
126+ if args .interactive :
127+ bluebox_interactive (seq )
128+ return
96129 try :
97130 seq (args .sequence )
98131 except Exception as e :
0 commit comments