2021-09-20 14:38:02 -04:00
|
|
|
###############################################################################
|
|
|
|
# Copyright (C) 2020 Simon Adlem, G7RZU <g7rzu@gb7fr.org.uk>
|
|
|
|
#
|
|
|
|
# This program is free software; you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation; either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with this program; if not, write to the Free Software Foundation,
|
|
|
|
# Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
###############################################################################
|
2020-09-17 15:34:50 -04:00
|
|
|
from bitarray import bitarray
|
|
|
|
from itertools import islice
|
2021-05-09 09:36:02 -04:00
|
|
|
import os
|
|
|
|
import glob
|
2020-09-17 15:34:50 -04:00
|
|
|
|
|
|
|
class readAMBE:
|
|
|
|
|
|
|
|
def __init__(self, lang,path):
|
Allow loading if multiple languages and selection both via
default in config file and OPTIONS.
Remove ANNOUNCEMENT_LANGUAGE from [GLOBAL]
Replace with ANNOUNCEMENT_LANGUAGES - Comma Separated list
Current list:
ANNOUNCEMENT_LANGUAGES: en_GB,en_GB_2,en_US,es_ES,es_ES_2,fr_FR,de_DE,dk_DK,it_IT,no_NO,pl_PL,se_SE
Add:
ANNOUNCEMENT_LANGUAGE to MASTER definition. If using GENERATOR, this becomes
the template default.
To change via OPTIONS add LANG=<language code>
take codes from list above
2021-05-13 19:07:11 -04:00
|
|
|
self.langcsv = lang
|
|
|
|
self.langs = lang.split(',')
|
2020-09-17 15:34:50 -04:00
|
|
|
self.path = path
|
Allow loading if multiple languages and selection both via
default in config file and OPTIONS.
Remove ANNOUNCEMENT_LANGUAGE from [GLOBAL]
Replace with ANNOUNCEMENT_LANGUAGES - Comma Separated list
Current list:
ANNOUNCEMENT_LANGUAGES: en_GB,en_GB_2,en_US,es_ES,es_ES_2,fr_FR,de_DE,dk_DK,it_IT,no_NO,pl_PL,se_SE
Add:
ANNOUNCEMENT_LANGUAGE to MASTER definition. If using GENERATOR, this becomes
the template default.
To change via OPTIONS add LANG=<language code>
take codes from list above
2021-05-13 19:07:11 -04:00
|
|
|
|
2020-09-17 15:34:50 -04:00
|
|
|
def _make_bursts(self,data):
|
|
|
|
it = iter(data)
|
|
|
|
for i in range(0, len(data), 108):
|
2020-09-19 09:05:52 -04:00
|
|
|
yield bitarray([k for k in islice(it, 108)] )
|
2020-09-17 15:34:50 -04:00
|
|
|
|
2020-10-02 12:13:34 -04:00
|
|
|
#Read indexed files
|
2020-09-17 15:34:50 -04:00
|
|
|
def readfiles(self):
|
Allow loading if multiple languages and selection both via
default in config file and OPTIONS.
Remove ANNOUNCEMENT_LANGUAGE from [GLOBAL]
Replace with ANNOUNCEMENT_LANGUAGES - Comma Separated list
Current list:
ANNOUNCEMENT_LANGUAGES: en_GB,en_GB_2,en_US,es_ES,es_ES_2,fr_FR,de_DE,dk_DK,it_IT,no_NO,pl_PL,se_SE
Add:
ANNOUNCEMENT_LANGUAGE to MASTER definition. If using GENERATOR, this becomes
the template default.
To change via OPTIONS add LANG=<language code>
take codes from list above
2021-05-13 19:07:11 -04:00
|
|
|
|
2020-09-17 15:34:50 -04:00
|
|
|
_AMBE_LENGTH = 9
|
|
|
|
|
Allow loading if multiple languages and selection both via
default in config file and OPTIONS.
Remove ANNOUNCEMENT_LANGUAGE from [GLOBAL]
Replace with ANNOUNCEMENT_LANGUAGES - Comma Separated list
Current list:
ANNOUNCEMENT_LANGUAGES: en_GB,en_GB_2,en_US,es_ES,es_ES_2,fr_FR,de_DE,dk_DK,it_IT,no_NO,pl_PL,se_SE
Add:
ANNOUNCEMENT_LANGUAGE to MASTER definition. If using GENERATOR, this becomes
the template default.
To change via OPTIONS add LANG=<language code>
take codes from list above
2021-05-13 19:07:11 -04:00
|
|
|
_wordBADictofDicts = {}
|
2021-05-09 09:36:02 -04:00
|
|
|
|
Allow loading if multiple languages and selection both via
default in config file and OPTIONS.
Remove ANNOUNCEMENT_LANGUAGE from [GLOBAL]
Replace with ANNOUNCEMENT_LANGUAGES - Comma Separated list
Current list:
ANNOUNCEMENT_LANGUAGES: en_GB,en_GB_2,en_US,es_ES,es_ES_2,fr_FR,de_DE,dk_DK,it_IT,no_NO,pl_PL,se_SE
Add:
ANNOUNCEMENT_LANGUAGE to MASTER definition. If using GENERATOR, this becomes
the template default.
To change via OPTIONS add LANG=<language code>
take codes from list above
2021-05-13 19:07:11 -04:00
|
|
|
for _lang in self.langs:
|
|
|
|
|
|
|
|
_prefix = self.path+_lang
|
2021-05-09 09:36:02 -04:00
|
|
|
_wordBADict = {}
|
Allow loading if multiple languages and selection both via
default in config file and OPTIONS.
Remove ANNOUNCEMENT_LANGUAGE from [GLOBAL]
Replace with ANNOUNCEMENT_LANGUAGES - Comma Separated list
Current list:
ANNOUNCEMENT_LANGUAGES: en_GB,en_GB_2,en_US,es_ES,es_ES_2,fr_FR,de_DE,dk_DK,it_IT,no_NO,pl_PL,se_SE
Add:
ANNOUNCEMENT_LANGUAGE to MASTER definition. If using GENERATOR, this becomes
the template default.
To change via OPTIONS add LANG=<language code>
take codes from list above
2021-05-13 19:07:11 -04:00
|
|
|
|
|
|
|
indexDict = {}
|
|
|
|
|
|
|
|
if os.path.isdir(_prefix):
|
|
|
|
ambeBytearray = {}
|
|
|
|
_wordBitarray = bitarray(endian='big')
|
|
|
|
_wordBADict = {}
|
|
|
|
_glob = _prefix + "/*.ambe"
|
|
|
|
for ambe in glob.glob(_glob):
|
|
|
|
basename = os.path.basename(ambe)
|
|
|
|
_voice,ext = basename.split('.')
|
|
|
|
inambe = open(ambe,'rb')
|
|
|
|
_wordBitarray.frombytes(inambe.read())
|
|
|
|
inambe.close()
|
|
|
|
_wordBADict[_voice] = []
|
|
|
|
pairs = 1
|
|
|
|
_lastburst = ''
|
|
|
|
for _burst in self._make_bursts(_wordBitarray):
|
2021-05-09 09:36:02 -04:00
|
|
|
#Not sure if we need to pad or not? Seems to make little difference.
|
Allow loading if multiple languages and selection both via
default in config file and OPTIONS.
Remove ANNOUNCEMENT_LANGUAGE from [GLOBAL]
Replace with ANNOUNCEMENT_LANGUAGES - Comma Separated list
Current list:
ANNOUNCEMENT_LANGUAGES: en_GB,en_GB_2,en_US,es_ES,es_ES_2,fr_FR,de_DE,dk_DK,it_IT,no_NO,pl_PL,se_SE
Add:
ANNOUNCEMENT_LANGUAGE to MASTER definition. If using GENERATOR, this becomes
the template default.
To change via OPTIONS add LANG=<language code>
take codes from list above
2021-05-13 19:07:11 -04:00
|
|
|
if len(_burst) < 108:
|
|
|
|
pad = (108 - len(_burst))
|
|
|
|
for i in range(0,pad,1):
|
|
|
|
_burst.append(False)
|
|
|
|
if pairs == 2:
|
|
|
|
_wordBADict[_voice].append([_lastburst,_burst])
|
|
|
|
_lastburst = ''
|
|
|
|
pairs = 1
|
|
|
|
next
|
|
|
|
else:
|
|
|
|
pairs = pairs + 1
|
|
|
|
_lastburst = _burst
|
|
|
|
|
|
|
|
_wordBitarray.clear()
|
|
|
|
_wordBADict['silence'] = ([
|
|
|
|
[bitarray('101011000000101010100000010000000000001000000000000000000000010001000000010000000000100000000000100000000000'),
|
|
|
|
bitarray('001010110000001010101000000100000000000010000000000000000000000100010000000100000000001000000000001000000000')]
|
|
|
|
])
|
|
|
|
_wordBADictofDicts[_lang] = _wordBADict
|
|
|
|
else:
|
|
|
|
try:
|
|
|
|
with open(_prefix+'.indx') as index:
|
|
|
|
for line in index:
|
|
|
|
(voice,start,length) = line.split()
|
|
|
|
indexDict[voice] = [int(start) * _AMBE_LENGTH ,int(length) * _AMBE_LENGTH]
|
|
|
|
index.close()
|
|
|
|
except IOError:
|
|
|
|
return False
|
|
|
|
|
|
|
|
ambeBytearray = {}
|
|
|
|
_wordBitarray = bitarray(endian='big')
|
|
|
|
_wordBADict = {}
|
|
|
|
try:
|
|
|
|
with open(_prefix+'.ambe','rb') as ambe:
|
|
|
|
for _voice in indexDict:
|
|
|
|
ambe.seek(indexDict[_voice][0])
|
|
|
|
_wordBitarray.frombytes(ambe.read(indexDict[_voice][1]))
|
|
|
|
#108
|
|
|
|
_wordBADict[_voice] = []
|
|
|
|
pairs = 1
|
|
|
|
_lastburst = ''
|
|
|
|
for _burst in self._make_bursts(_wordBitarray):
|
|
|
|
#Not sure if we need to pad or not? Seems to make little difference.
|
|
|
|
if len(_burst) < 108:
|
|
|
|
pad = (108 - len(_burst))
|
|
|
|
for i in range(0,pad,1):
|
|
|
|
_burst.append(False)
|
|
|
|
if pairs == 2:
|
|
|
|
_wordBADict[_voice].append([_lastburst,_burst])
|
|
|
|
_lastburst = ''
|
|
|
|
pairs = 1
|
|
|
|
next
|
|
|
|
else:
|
|
|
|
pairs = pairs + 1
|
|
|
|
_lastburst = _burst
|
|
|
|
|
|
|
|
_wordBitarray.clear()
|
|
|
|
ambe.close()
|
|
|
|
except IOError:
|
|
|
|
return False
|
|
|
|
_wordBADict['silence'] = ([
|
|
|
|
[bitarray('101011000000101010100000010000000000001000000000000000000000010001000000010000000000100000000000100000000000'),
|
|
|
|
bitarray('001010110000001010101000000100000000000010000000000000000000000100010000000100000000001000000000001000000000')]
|
|
|
|
])
|
|
|
|
_wordBADictofDicts[_lang] = _wordBADict
|
|
|
|
|
|
|
|
return _wordBADictofDicts
|
|
|
|
|
2020-10-02 15:47:04 -04:00
|
|
|
#Read a single ambe file from the audio directory
|
2020-10-02 12:13:34 -04:00
|
|
|
def readSingleFile(self,filename):
|
|
|
|
ambeBytearray = {}
|
2020-10-18 08:41:38 -04:00
|
|
|
_wordBitarray = bitarray(endian='big')
|
2020-10-02 12:13:34 -04:00
|
|
|
_wordBA= []
|
|
|
|
try:
|
|
|
|
with open(self.path+filename,'rb') as ambe:
|
|
|
|
_wordBitarray.frombytes(ambe.read())
|
|
|
|
#108
|
|
|
|
_wordBA = []
|
|
|
|
pairs = 1
|
|
|
|
_lastburst = ''
|
|
|
|
for _burst in self._make_bursts(_wordBitarray):
|
|
|
|
#Not sure if we need to pad or not? Seems to make little difference.
|
|
|
|
if len(_burst) < 108:
|
|
|
|
pad = (108 - len(_burst))
|
|
|
|
for i in range(0,pad,1):
|
|
|
|
_burst.append(False)
|
|
|
|
if pairs == 2:
|
|
|
|
_wordBA.append([_lastburst,_burst])
|
|
|
|
_lastburst = ''
|
|
|
|
pairs = 1
|
|
|
|
next
|
|
|
|
else:
|
|
|
|
pairs = pairs + 1
|
|
|
|
_lastburst = _burst
|
|
|
|
|
|
|
|
_wordBitarray.clear()
|
|
|
|
ambe.close()
|
|
|
|
except IOError:
|
2021-05-24 17:32:54 -04:00
|
|
|
raise
|
2020-10-02 12:13:34 -04:00
|
|
|
|
|
|
|
return(_wordBA)
|
|
|
|
|
|
|
|
|
2020-09-17 15:34:50 -04:00
|
|
|
if __name__ == '__main__':
|
|
|
|
|
2021-05-09 09:36:02 -04:00
|
|
|
#test = readAMBE('en_GB','./Audio/')
|
2020-09-17 15:34:50 -04:00
|
|
|
|
2021-05-09 09:36:02 -04:00
|
|
|
#print(test.readfiles())
|
|
|
|
test = readAMBE('en_GB_2','./Audio/')
|
2020-10-02 16:17:59 -04:00
|
|
|
print(test.readfiles())
|
2020-10-02 12:13:34 -04:00
|
|
|
print(test.readSingleFile('44xx.ambe'))
|