def downsampleAudio(inputFile, M):
    """
    Inputs:
        inputFile: file name of the wav file (including path)
        	M: downsampling factor (positive integer)
    """
    print("Reading WAV...")
    (sampleRate, samples) = wavread(inputFile)
    step = M

    print("Sample Rate:",sampleRate,"kHz/16")
    print("Sample Size:",len(samples))

    base = os.path.basename(inputFile)
    outputFile = "%s_downsampled.wav" % base.replace(".wav", "")

    print("Downsampling...")
    downsampled = hopSamples(samples, step)

    print("Writing downsampled WAV...")

    flen=len(samples)
    nlen=len(downsampled)
    newRate = sampleRate/(flen/nlen)
    wavwrite(downsampled, sampleRate, outputFile)

    print("Done:", outputFile)
Ejemplo n.º 2
0
def downsampleAudio(inputFile, M):
    """
    Inputs:
        inputFile: file name of the wav file (including path)
        	M: downsampling factor (positive integer)
    """
    (fs, x) = wavread(inputFile)
    newSamples = hopSamples(x, M)
    wavwrite(newSamples, int(fs / M),
             os.path.basename(inputFile)[0:-4] + '_downsampled.wav')
Ejemplo n.º 3
0
def downsampleAudio(inputFile, M):
    """
    Inputs:
        inputFile: file name of the wav file (including path)
        	M: downsampling factor (positive integer)
    """
    fs, x = wavread(inputFile)

    wavwrite(hopSamples(x, M), int(fs / M),
             os.path.splitext(inputFile)[0] + "_downsampled.wav")
Ejemplo n.º 4
0
def downsampleAudio(inputFile, M):
    """
    Inputs:
        inputFile: file name of the wav file (including path)
        	M: downsampling factor (positive integer)
    """
    ## Your code here
    (fs, x) = wavread(inputFile)
    y = hopSamples(x, M)
    wavwrite(y, fs / M, os.path.basename(inputFile)[0:-4] + '_downsampled.wav')
Ejemplo n.º 5
0
def downsampleAudio(inputFile, M):
    """
    Inputs:
        inputFile: file name of the wav file (including path)
        	M: downsampling factor (positive integer)
    """
    ## Your code here
    fs, x = wavread(inputFile)
    y = hopSamples(x, M)
    wavwrite(y, fs, 'test.wav')
Ejemplo n.º 6
0
def downsampleAudio(inputFile, M):
    """
    Inputs:
        inputFile: file name of the wav file (including path)
        	M: downsampling factor (positive integer)
    """
    ## Your code here
    fs, x = wavread(inputFile)
    new_x = hopSamples(x, M)
    wavwrite(new_x, fs // M, re.sub('.wav', '_downsampled.wav', inputFile))
Ejemplo n.º 7
0
def downsampleAudio(inputFile, M):
    """
    Inputs:
        inputFile: file name of the wav file (including path)
        M: downsampling factor (positive integer)
    """
    # Your code here
    fs, x = wavread(inputFile)
    y = hopSamples(x, M)
    wavwrite(y, floor(fs / M), "result.wav")
Ejemplo n.º 8
0
def downsampleAudio(inputFile, M):
    """
    Inputs:
        inputFile: file name of the wav file (including path)
        	M: downsampling factor (positive integer)
    """
    ## Your code here
    samplingRate, samples = wavread(inputFile)
    downsampledSamples = hopSamples(samples, M)
    wavwrite(downsampledSamples, M,
             inputFile.replace('.wav', '_downsampled.wav'))
Ejemplo n.º 9
0
def downsampleAudio(inputFile, M):
    """
    Inputs:
        inputFile: file name of the wav file (including path)
        	M: downsampling factor (positive integer)
    """
    ## Your code here
    fs,samples=wavread(inputFile)
    downsampled=hopSamples(samples,M)
    wavwrite(downsampled,fs,"{}_downsampled.wav".format(inputFile[:-4]))
    
Ejemplo n.º 10
0
def downsampleAudio(inputFile, M):
    """
    Inputs:
        inputFile: file name of the wav file (including path)
        	M: downsampling factor (positive integer)
    """
    ## Your code here
    fileNameParts = os.path.splitext(inputFile)
    fs, x = wavread(inputFile)
    wavwrite(hopSamples(x, M), fs,
             fileNameParts[0] + '_downsampled' + fileNameParts[1])
Ejemplo n.º 11
0
def downsampleAudio(inputFile, M):
    """
    Inputs:
        inputFile: file name of the wav file (including path)
        	M: downsampling factor (positive integer)
    """
    outputFile = inputFile.rstrip('.wav') + '_downsampled.wav'
    print outputFile
    (sr, data) = wavread(inputFile)
    downsampleData = hopSamples(data, M)
    wavwrite(downsampleData, sr / M, outputFile)
Ejemplo n.º 12
0
def downsampleAudio(inputFile, M):
    """
    Inputs:
        inputFile: file name of the wav file (including path)
        	M: downsampling factor (positive integer)
    """
    fs, x = wavread(inputFile)
    y = hopSamples(x, M)
    basename, extension = inputFile.rsplit(".", 1)
    outputFile = basename + "_downsampled." + extension
    wavwrite(y, fs / M, outputFile)
Ejemplo n.º 13
0
def downsampleAudio(inputFile, M):
    """
    Inputs:
        inputFile: file name of the wav file (including path)
        	M: downsampling factor (positive integer)
    """
    ## Your code here
    fs, x = wavread(inputFile)
    if fs <> 44100:
        print "Sample rate must be 44100."
    ds = hopSamples(x, M)
    wavwrite(ds, fs / M, inputFile[:-4] + "_downsampled.wav")
Ejemplo n.º 14
0
def downsampleAudio(inputFile, M):
    """
    Inputs:
        inputFile: file name of the wav file (including path)
        	M: downsampling factor (positive integer)
    """
    x = wavread(inputFile)[1]
    fs = wavread(inputFile)[0]
    a = hopSamples(x, M)
    file_name = inputFile.replace('.wav', '_downsampled.wav')
    print(file_name)
    wavwrite(a, fs/M, file_name)
Ejemplo n.º 15
0
def downsampleAudio(inputFile, M):
    """
    Inputs:
        inputFile: file name of the wav file (including path)
        	M: downsampling factor (positive integer)
    """
    ## Your code here
    outputFile = inputFile[0:inputFile.rfind('.')] + "_downsampled.wav"

    (fs, x) = wavread(inputFile)
    fs = int(fs / M)
    y = hopSamples(x, M)

    wavwrite(y, fs, outputFile)
Ejemplo n.º 16
0
def downsampleAudio(inputFile, M):
    """
    Inputs:
        inputFile: file name of the wav file (including path)
        	M: downsampling factor (positive integer)
    """
    fs, x = wavread(inputFile)

    y = hopSamples(x, M)

    dirname = os.path.dirname(inputFile)
    file, ext = os.path.basename(inputFile).split('.')

    outputFile = os.path.join(dirname, file + '_downsampled.' + ext)
    wavwrite(y, int(fs / M), outputFile)
Ejemplo n.º 17
0
def downsampleAudio(inputFile, M):
    """
    Inputs:
        inputFile: file name of the wav file (including path)
        	M: downsampling factor (positive integer)
    """

    (fs, x) = wavread(inputFile)

    y = hopSamples(x, M)

    fs_new = fs / M

    wavwrite(y, fs_new, 'Downsampled_Sound.wav')

    return 0
Ejemplo n.º 18
0
def downsampleAudio(inputFile, M):
    """
    Inputs:
        inputFile: file name of the wav file (including path)
        	M: downsampling factor (positive integer)
    """
    ## Your code here
    from math import floor
    fs, x = wavread(inputFile)
    y = hopSamples(x, M)
    new_fs = floor(fs / M)
    outputFile = inputFile[0:len(inputFile) - 4] + "_downsampled.wav"
    wavwrite(y, new_fs, outputFile)
    print("Input data:", inputFile)
    print("\tfs=%s\tqSamp=%s" % (fs, len(x)))
    print("Output:", outputFile)
    print("\tfs=%s\tqSamp=%s" % (new_fs, len(y)))
Ejemplo n.º 19
0
def output(partIdx):
    """Uses the student code to compute the output for test cases."""
    outputString = ''
    filename = open('testInputA1.pkl', 'rb')
    try:  ## load the dict
        dictInp = pickle.load(filename, encoding='latin1')  ## python3
    except TypeError:
        dictInp = pickle.load(filename)  ## python2

    testCases = dictInp['testCases']
    outputType = dictInp['outputType']

    if partIdx == 0:  # This is wk1-part-1: readAudio
        for line in testCases['A1-part-1']:
            answer = readAudio(**line)
            if outputType['A1-part-1'][0] == type(answer):
                outputString += convertNpObjToStr(answer) + '\n'
            else:
                wrongOutputTypeError(outputType['A1-part-1'][0], partIdx)
                return ''
                #sys.exit(1)

    elif partIdx == 1:  # This is wk1-part-2: minMaxAudio
        for line in testCases['A1-part-2']:
            answer = minMaxAudio(**line)
            if outputType['A1-part-2'][0] == type(answer):
                outputString += str(answer).strip('()') + '\n'
            else:
                wrongOutputTypeError(outputType['A1-part-2'][0], partIdx)
                return ''
                #sys.exit(1)

    elif partIdx == 2:  # This is wk1-part-3: hopSamples
        for line in testCases['A1-part-3']:
            answer = hopSamples(**line)
            if outputType['A1-part-3'][0] == type(answer):
                answer = answer.copy(
                )  # Important, else does not allocate continuous memory locations
                outputString += convertNpObjToStr(answer) + '\n'
            else:
                wrongOutputTypeError(outputType['A1-part-3'][0], partIdx)
                return ''
                #sys.exit(1)

    return outputString.strip()
Ejemplo n.º 20
0
def downsampleAudio(inputFile, M):
    """
    Inputs:
        inputFile: file name of the wav file (including path)
        	M: downsampling factor (positive integer)
    """
    ## Your code here

    (fs, x) = wavread(inputFile)
    print(fs)

    downsampled = hopSamples(x, M)

    out_name = '%s_downsampled.wav' % inputFile[:-4]
    print(out_name)
    print(int(fs / M))

    wavwrite(downsampled, int(fs / M), out_name)
Ejemplo n.º 21
0
def downsampleAudio(inputFile, M):
    """
    Inputs:
        inputFile: file name of the wav file (including path)
        	M: downsampling factor (positive integer)
    """
    ## Your code here
    wav_file = Path(inputFile)
    if not wav_file.is_file():
        raise ValueError(inputFile + " file does not exist")
    fs, input_audio = wavread(inputFile)

    # downsample
    sampled_audio = hopSamples(input_audio, M)

    # save downsampled audio
    new_audio_file = wav_file.stem + "-M" + str(M) + ".wav"
    print('Saving new wav file: ' + new_audio_file)
    wavwrite(sampled_audio, int(fs / M), new_audio_file)
Ejemplo n.º 22
0
def output(partIdx):
  """Uses the student code to compute the output for test cases."""
  outputString = ''

  dictInp = pickle.load(open("testInputA1.pkl"))  ## load the dict
  testCases = dictInp['testCases']
  outputType = dictInp['outputType']

  if partIdx == 0: # This is wk1-part-1: readAudio
    for line in testCases['A1-part-1']:
      answer = readAudio(**line)
      if outputType['A1-part-1'][0] == type(answer):
        outputString += convertNpObjToStr(answer) + '\n'
      else:
        wrongOutputTypeError(outputType['A1-part-1'][0])
        sys.exit(1)
      
  elif partIdx == 1: # This is wk1-part-2: minMaxAudio
    for line in testCases['A1-part-2']:
      answer = minMaxAudio(**line)
      if outputType['A1-part-2'][0] == type(answer):
        outputString += str(answer).strip('()') + '\n'
      else:
        wrongOutputTypeError(outputType['A1-part-2'][0])
        sys.exit(1)
      
  elif partIdx == 2: # This is wk1-part-3: hopSamples
    for line in testCases['A1-part-3']:
      answer = hopSamples(**line) 
      if outputType['A1-part-3'][0] == type(answer):
        answer = answer.copy()  # Important, else does not allocate continuous memory locations
        outputString += convertNpObjToStr(answer) + '\n'
      else:
        wrongOutputTypeError(outputType['A1-part-3'][0])
        sys.exit(1)        

  return outputString.strip()
Ejemplo n.º 23
0
def simpleDecimator(inputFile, M, outputFile):
    (fs,data) = wavread(inputFile)
    wavwrite(hopSamples(data,M), fs/M, outputFile)
Ejemplo n.º 24
0
 def test_hopSamples(self):
     expected = np.array([0, 2, 4, 6, 8])
     actual = hopSamples(np.arange(10), 2)
     results = expected == actual
     print(results)
     self.assertTrue(results.all())