11# Copyright: Multiple Authors
22#
3- # This file is part of SigMF . https://github.com/sigmf/sigmf-python
3+ # This file is part of sigmf-python . https://github.com/sigmf/sigmf-python
44#
55# SPDX-License-Identifier: LGPL-3.0-or-later
66
77"""converter for wav containers"""
88
9- import os
10- import tempfile
11- import datetime
12- import pathlib
139import argparse
10+ import datetime
1411import getpass
12+ import logging
13+ import os
14+ import pathlib
15+ import tempfile
1516
1617from scipy .io import wavfile
1718
19+ from .. import SigMFFile , __specification__
20+ from .. import __version__ as toolversion
1821from .. import archive
19- from ..sigmffile import SigMFFile
2022from ..utils import get_data_type_str
2123
24+ log = logging .getLogger ()
25+
2226
2327def convert_wav (input_wav_filename , archive_filename = None , start_datetime = None , author = None ):
2428 """
2529 read a .wav and write a .sigmf archive
2630 """
31+ input_path = pathlib .Path (input_wav_filename )
32+ input_stem = input_path .stem
2733 samp_rate , wav_data = wavfile .read (input_wav_filename )
2834
2935 global_info = {
@@ -33,27 +39,27 @@ def convert_wav(input_wav_filename, archive_filename=None, start_datetime=None,
3339 SigMFFile .NUM_CHANNELS_KEY : 1 if len (wav_data .shape ) < 2 else wav_data .shape [1 ],
3440 SigMFFile .RECORDER_KEY : os .path .basename (__file__ ),
3541 SigMFFile .SAMPLE_RATE_KEY : samp_rate ,
42+ SigMFFile .VERSION_KEY : __specification__ ,
3643 }
3744
3845 if start_datetime is None :
39- fname = pathlib .Path (input_wav_filename )
40- mtime = datetime .datetime .fromtimestamp (fname .stat ().st_mtime )
46+ mtime = datetime .datetime .fromtimestamp (input_path .stat ().st_mtime )
4147 start_datetime = mtime .isoformat () + "Z"
4248
4349 capture_info = {SigMFFile .START_INDEX_KEY : 0 }
4450 if start_datetime is not None :
4551 capture_info [SigMFFile .DATETIME_KEY ] = start_datetime
4652
4753 tmpdir = tempfile .mkdtemp ()
48- sigmf_data_filename = input_wav_filename + archive .SIGMF_DATASET_EXT
54+ sigmf_data_filename = input_stem + archive .SIGMF_DATASET_EXT
4955 sigmf_data_path = os .path .join (tmpdir , sigmf_data_filename )
5056 wav_data .tofile (sigmf_data_path )
5157
5258 meta = SigMFFile (data_file = sigmf_data_path , global_info = global_info )
5359 meta .add_capture (0 , metadata = capture_info )
5460
5561 if archive_filename is None :
56- archive_filename = os . path . basename ( input_wav_filename ) + archive .SIGMF_ARCHIVE_EXT
62+ archive_filename = input_stem + archive .SIGMF_ARCHIVE_EXT
5763 meta .tofile (archive_filename , toarchive = True )
5864 return os .path .abspath (archive_filename )
5965
@@ -65,13 +71,22 @@ def main():
6571 parser = argparse .ArgumentParser (description = "Convert .wav to .sigmf container." )
6672 parser .add_argument ("input" , type = str , help = "Wavfile path" )
6773 parser .add_argument ("--author" , type = str , default = None , help = f"set { SigMFFile .AUTHOR_KEY } metadata" )
74+ parser .add_argument ('-v' , '--verbose' , action = 'count' , default = 0 )
75+ parser .add_argument ('--version' , action = 'version' , version = f'%(prog)s v{ toolversion } ' )
6876 args = parser .parse_args ()
6977
78+ level_lut = {
79+ 0 : logging .WARNING ,
80+ 1 : logging .INFO ,
81+ 2 : logging .DEBUG ,
82+ }
83+ logging .basicConfig (level = level_lut [min (args .verbose , 2 )])
84+
7085 out_fname = convert_wav (
7186 input_wav_filename = args .input ,
7287 author = args .author ,
7388 )
74- print ( "Wrote" , out_fname )
89+ log . info ( f"Write { out_fname } " )
7590
7691
7792if __name__ == "__main__" :
0 commit comments