def write_wav(data, filename, framerate, amplitude, length):
    wavfile = wave.open(filename,'w')
    nchannels = 1
    sampwidth = 2
    framerate = framerate
    nframes = length
    comptype = "NONE"
    compname = "not compressed"
    wavfile.setparams((nchannels,
                        sampwidth,
                        framerate,
                        nframes,
                        comptype,
                        compname))
    frames = []
    for s in data:
        mul = int(s * amplitude)
        print("I am here")
        print(mul)
        frames.append(struct.pack('h', mul))

    frames = ''.join(frames)
    wavfile.writeframes(frames)
    wavfile.close()
    print("%s written" %(filename)) 
def save_file(filename):

    wavfile = wave.open(filename, "w")

    # wav parameters

    # Number of channels
    nchannels = 1
    # Sample width in bytes
    sampwidth = 2
    # Number of frames or samples
    nframes = len(data)

    comptype = "NONE"
    compname = "not compressed"

    wavfile.setparams(
        (nchannels, sampwidth, samples, nframes, comptype, compname))

    for sample in data:
        """
        struct - Takes the data from my "data" list and packs it as binary data or else
        the file is not readable by an audio player.

        h - Means 16 bit number

        32767.0 - The floating point numbers are not represented right and will not work
        when writing to the wave file. Convert the floating point numbers to fixed point
        to get full scale audio.
        """
        wavfile.writeframes(struct.pack('h', int(sample * 32767.0)))

    wavfile.close()

    return
Beispiel #3
0
    def load(self, originalname):
        # Load + convert audio
        print("Loading sound file <{}>...".format(originalname))
        name = originalname.split(".")[0]
        ext = originalname.split(".")[-1].lower()

        audiofile = originalname
        if ext == MP3:
            newfilename = name + ".wav"
            old = AudioSegment.from_mp3(audiofile)
            old.export(newfilename, format=WAV)
            self.filename = newfilename
        elif ext == PCM:
            print "This is a PCM file!"
            pcmfile = open(audiofile, 'rb')
            pcmdata = pcmfile.read()
            pcmfile.close()

            wavfile = wave.open(name + '.wav', 'wb')
            wavfile.setparams((1, 2, 44100, 16, 'NONE', 'NONE'))
            wavfile.writeframes(pcmdata)
            wavfile.close()
            self.filename = name + ".wav"
        else:
            self.filename = audiofile
Beispiel #4
0
def write_wav(data, filename, framerate, amplitude):
    wavfile = wave.open(filename,'w')
    nchannels = 1
    sampwidth = 2
    framerate = framerate
    nframes = len(data)
    comptype = "NONE"
    compname = "not compressed"
    wavfile.setparams((nchannels,
                        sampwidth,
                        framerate,
                        nframes,
                        comptype,
                        compname))
    frames = []
    for s in data:
        mul = int(s * amplitude)
        frames.append(struct.pack('h', mul))

    frames = ''.join(frames)
    wavfile.writeframes(frames)
    wavfile.close()
    print("%s written" %(filename))