Okay, here's what I am up to now. I am trying to figure out how to write sin waves to a .wav file, for playback. Here's the issue: I have no idea what I'm doing. I looked up the documentation for the wave module, and have been able to open a wav file and write to it. It'll save a .wav file for a number of seconds, playing a frequency. Here's the code for that:

import numpy as N
import wave

# Here, create all files to playback
class SoundFile:

    def __init__(self, duration=5, frequency=400, samplerate=44100):
        sr = samplerate
        samples = duration*samplerate
        period = samplerate / float(frequency)
        omega = N.pi * 2 / period
        xaxis = N.arange(int(period),dtype = N.float) * omega
        ydata = 16384 * N.sin(xaxis)
        signal = N.resize(ydata, (samples,))
        ssignal = ''
        for i in range(len(signal)):
           ssignal += wave.struct.pack('h',signal[i])
        filewriteto = frequency
        self.file = wave.open(str(filewriteto)+'.wav', 'wb')
        self.file.setparams((1, 2, sr, 44100*4, 'NONE', 'noncompressed'))

# Test that the soundfile class works
S = SoundFile(5, 220)

I have a small gist of what's going on here. Now, I know how sine wavs work, and can add/subtract them. The only issue I have is writing them. No, I don't want a premade module for it. I don't even know if there's anything that exists for it. I've googled multiple times for information, but none have been found by me.
So, I guess what I need help with is just writing sine waves to a .wav file.
Any help would be much appreciated :D

Recommended Answers

All 2 Replies

Thank you very much vegaseat. You have helped me greatly. I am going to be making a module for music, with a scale, interval, and chord finder, along with the ability to write what you type in. I am hoping to make it into a nice synthesizer on the computer. I'm not sure though. Anyways, without further ado, here is the semi-finished code for writing a chord to a .wav file:

# .wav file chord writer
# ~ Hondros
# Much thanks to vegaseat
    from numpy import *
        from math import *
        print("No module numpy or math found;\nThese are vital for the performance of this program")
import struct
import wave

class SoundFile():

    def __init__(self,
                 functions = None):
        """Create a synthetic wave file,
           with combined frequencies freq,
           file fname has a length of about data_size*2
           Note if you want a certain wav other than sin,
           type function as a string, replacing frequenice as: 'freq'"""

        if functions == None:
            def func(number, x, frate, function):
                function = ''
                for frequencie in number:
                    a = 'sin(2*pi*'+str(frequencie)+'*(x/frate))+'
                    function += a
                function = function[:-1]
                return eval(function)

        if type(functions) == type(""):
            def func(number, x, frate, function):
                numfunction = ''
                for frequencie in number:
                    for splits in function.split('freq'):
                        numfunction += splits +\
                        numfunction = numfunction[:-len(str(frequencie))]
                    numfunction += 'x'
                numfunction = numfunction[:-1]
                return eval(numfunction)

        sine_list = []
        for x in range(data_size):
            sine_list.append(func(freq, x, frate, functions))
        wavfile = wave.open(fname, "w")
        nchannels = 1
        sampwidth = 2
        framerate = int(frate)
        nframes = data_size
        comptype = "NONE"
        compname = "not compressed"
        print("Please be patient whilst the file is written")
        for s in sine_list:
            wavfile.writeframes(struct.pack('h', int(s*amp/2)))
        print("%s written" %(fname))
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.