Home | History | Annotate | Download | only in processing
      1 #!/usr/bin/python
      2 
      3 # Copyright (C) 2012 The Android Open Source Project
      4 #
      5 # Licensed under the Apache License, Version 2.0 (the "License");
      6 # you may not use this file except in compliance with the License.
      7 # You may obtain a copy of the License at
      8 #
      9 #       http://www.apache.org/licenses/LICENSE-2.0
     10 #
     11 # Unless required by applicable law or agreed to in writing, software
     12 # distributed under the License is distributed on an "AS IS" BASIS,
     13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     14 # See the License for the specific language governing permissions and
     15 # limitations under the License.
     16 
     17 from consts import *
     18 import numpy as np
     19 import scipy as sp
     20 import scipy.fftpack as fft
     21 import matplotlib.pyplot as plt
     22 
     23 # generate random signal with max freq
     24 # Input: peak amplitude,
     25 #        duration in msec,
     26 #        sampling rate HZ
     27 #        high frequency,
     28 # Output: generated sound (stereo)
     29 
     30 def do_gen_random(peakAmpl, durationInMSec, samplingRate, fHigh, stereo=True):
     31     samples = durationInMSec * samplingRate / 1000
     32     result = np.zeros(samples * 2 if stereo else samples, dtype=np.int16)
     33     randomSignal = np.random.normal(scale = peakAmpl * 2 / 3, size=samples)
     34     fftData = fft.rfft(randomSignal)
     35     freqSamples = samples/2
     36     iHigh = freqSamples * fHigh * 2 / samplingRate + 1
     37     #print len(randomSignal), len(fftData), fLow, fHigh, iHigh
     38     if iHigh > freqSamples - 1:
     39         iHigh = freqSamples - 1
     40     fftData[0] = 0 # DC
     41     for i in range(iHigh, freqSamples - 1):
     42         fftData[ 2 * i + 1 ] = 0
     43         fftData[ 2 * i + 2 ] = 0
     44     if (samples - 2 *freqSamples) != 0:
     45         fftData[samples - 1] = 0
     46 
     47     filteredData = fft.irfft(fftData)
     48     #freq = np.linspace(0.0, samplingRate, num=len(fftData), endpoint=False)
     49     #plt.plot(freq, abs(fft.fft(filteredData)))
     50     #plt.plot(filteredData)
     51     #plt.show()
     52     if stereo:
     53         for i in range(len(filteredData)):
     54             result[2 * i] = filteredData[i]
     55             result[2 * i + 1] = filteredData[i]
     56     else:
     57         for i in range(len(filteredData)):
     58             result[i] = filteredData[i]
     59     return result
     60 
     61 
     62 def gen_random(inputData, inputTypes):
     63     output = []
     64     outputData = []
     65     outputTypes = []
     66     # basic sanity check
     67     inputError = False
     68     if (inputTypes[0] != TYPE_I64):
     69         inputError = True
     70     if (inputTypes[1] != TYPE_I64):
     71         inputError = True
     72     if (inputTypes[2] != TYPE_I64):
     73         inputError = True
     74     if (inputTypes[3] != TYPE_I64):
     75         inputError = True
     76     if inputError:
     77         output.append(RESULT_ERROR)
     78         output.append(outputData)
     79         output.append(outputTypes)
     80         return output
     81 
     82     result = do_gen_random(inputData[0], inputData[1], inputData[2], inputData[3])
     83 
     84     output.append(RESULT_OK)
     85     outputData.append(result)
     86     outputTypes.append(TYPE_STEREO)
     87     output.append(outputData)
     88     output.append(outputTypes)
     89     return output
     90 
     91 # test code
     92 if __name__=="__main__":
     93     peakAmplitude = 10000
     94     samplingRate = 44100
     95     durationInMSec = 10000
     96     #fLow = 500
     97     fHigh = 15000
     98     result = do_gen_random(peakAmplitude, durationInMSec, samplingRate, fHigh)
     99     plt.plot(result)
    100     plt.show()
    101