validators.py
Go to the documentation of this file.
00001 #!/usr/bin/env python
00002 # -*- coding: utf-8 -*-
00003 
00004 
00005 import wave
00006 import StringIO
00007 from functools import wraps
00008 from exceptions import ParameterException
00009 from exceptions import UnsupportedLanguageException
00010 from exceptions import InvalidAudioDataException
00011 
00012 def accepts(**types):
00013     """ check function argument types
00014 
00015     @param types: parameter types (kwdict)
00016     @type  types: dict
00017     @raise ParameterException: parameter type exception
00018     """
00019     def check_accepts(f):
00020         @wraps(f)
00021         def wrapper(*args, **kwds):
00022             for cnt, val in enumerate(args):
00023                 if types.has_key(f.func_code.co_varnames[cnt]) and \
00024                     not isinstance(val, types[f.func_code.co_varnames[cnt]]):
00025                     raise ParameterException("argument '%s'=%r does not match %s" % \
00026                                 (f.func_code.co_varnames[cnt], val, types[f.func_code.co_varnames[cnt]]))
00027                     del types[f.func_code.co_varnames[cnt]]
00028 
00029             for key, val in kwds.iteritems():
00030                 if types.has_key(key) and not isinstance(val, types[key]):
00031                     raise ParameterException("argument '%s'=%r does not match %s" % (key, val, types[key]))
00032 
00033             return f(*args, **kwds)
00034         return wrapper
00035     return check_accepts
00036 
00037 
00038 def check_wave_data(data, framerate, nchannel, sampwidth, maxlength):
00039     """ check wave file error.
00040 
00041     @param data: parameter data
00042     @type  data: str
00043     @param framerate: wave file framerate
00044     @type  framerate: int
00045     @param nchannel: wave file channel
00046     @type  nchannel: int
00047     @param sampwidth: sampling width (1=>8bit, 2=>16bit, 4=>32bit)
00048     @type  sampwidth: int
00049     @param maxlength: max wave length [ms]
00050     @type  maxlength: int
00051     @raises InvalidAudioDataException
00052     """
00053     try:
00054         wav_buffer = StringIO.StringIO(data)
00055         wav_file = wave.open(wav_buffer, 'rb')
00056         framerate_ = wav_file.getframerate()
00057         sampwidth_ = wav_file.getsampwidth()
00058         channels_ = wav_file.getnchannels()
00059         nframes_ = wav_file.getnframes()
00060 
00061         read_data = wav_file.readframes(nframes_)
00062         read_data_size = len(read_data)
00063         header_data_size = sampwidth_ * channels_ * nframes_
00064         wave_length_ = int(float(wav_file.getnframes()) / wav_file.getframerate() * 1000.0)
00065 
00066         if framerate_ != framerate:
00067             msg = 'the wav file frame rate MUST be %d. curr: %d' % (framerate, framerate_)
00068             raise InvalidAudioDataException(msg)
00069 
00070         if sampwidth_ != sampwidth:
00071             msg = 'the wav file samplewidth MUST be %dbit. curr: %d' % (sampwidth*8, sampwidth_*8)
00072             raise InvalidAudioDataException(msg)
00073 
00074         if channels_ != nchannel:
00075             msg = 'the wav file channels MUST be %d. curr: %d' % (nchannel, channels_)
00076             raise InvalidAudioDataException(msg)
00077 
00078         if header_data_size != read_data_size:
00079             msg = 'the wav file is broken. data_size(by header): %d data_size(by data): %d' % (header_data_size, read_data_size)
00080             raise InvalidAudioDataException(msg)
00081 
00082         if wave_length_ > maxlength:
00083             msg = 'the playing time of the wav file is too long. max %d[ms] curr: %d[ms]' % (maxlength, wave_length_)
00084             raise InvalidAudioDataException(msg)
00085 
00086         if wave_length_ == 0:
00087             msg = 'the playing time of the wav file is zero.'
00088             raise InvalidAudioDataException(msg)
00089 
00090     except wave.Error as err:
00091         raise InvalidAudioDataException('the input audio data MUST be wav format. Exception: %s' % str(err))
00092 
00093 
00094 def check_language(language, supported_languages):
00095     """ check supported language
00096 
00097     @param param_name: check parameter name
00098     @type  param_name: str
00099     @param languages: supported languages in the client
00100     @type  languages: list
00101     @raises UnsupportedLanguageException
00102     """
00103     # check supported language
00104     if not language in supported_languages:
00105         raise UnsupportedLanguageException('%s is unsupported language. supported languages are %s' % (language, supported_languages));
00106 


rospeex_core
Author(s): Komei Sugiura
autogenerated on Wed Aug 26 2015 16:10:30