How to generate analog sound from digital data

I have built an Excel spreadsheet that shows the waveform for a harmonic series. The series goes from the fundamental to 16x the fundamental. Each harmonic has a factor to adjust the intensity of that frequency relative to the fundamental. The data increment is 1 degree (and showing a single cycle from 0-360) but of course can be adjusted to any arbitrary increment. The fundamental is the sine function so the amplitude is -1 to +1.

Is there any reasonably straightforward way for me to take that data and convert it to an analog signal so I can hear what it would sound like? A pay-to-play tool is not out of the question but I really don’t know where to start.

Edit: With an increment of 1 degree, if we use A 440 Hz as the fundamental, then that would give a sampling rate equivalent of 158.4KHz, much higher than CD quality. So it seems that there is enough data to produce a high-quality sound.

There are a lot things you could do. You will however be limited to the sample rates supported by your hardware.

Personally, I would do the entire thing in Matlab, or (since I can’t afford Matlab) Scilab. It is far more powerful then Excel for this sort fo thing - indeed it is really the right tool for the job. It also has an internal sound player function. But at its most basic, it will read a csv text file, and you can ask it to play it. Will take a couple of lines of code.

But download and install SciLab, and have a play. You won’t go back to Excel for these sort of tasks.

An alternative to SciLab is Gnu Octave, which is a free Matlab clone (almost), and has functions to write .wav files, which you should be able to play. I just tested it, and was able to make a tone, and write it to file, in two lines:

snd = sinetone(440, 44100, 1, 0.125);
wavwrite(snd, 44100, 16, ‘test.wav’);

In the above, 440 is the frequency, 44100 is the sample rate (CD quality), 1 is the duration (1 second), 0.125 is the amplitude, and 16 is the bits per sample for the output file test.wav.

For a three term example similar to what the OP wants:
snd0 = sinetone(440, 44100, 1, 0.25);
snd1 = sinetone(880, 44100, 1, 0.125);
snd2 = sinetone(1320, 44100, 1, 0.0625);
wavwrite(snd0+snd1+snd2, 44100, 16, ‘test2.wav’);

Both of those played in an audio player. Supposedly you can play the the sound from within Octave, but it didn’t work for me.

Thank you both. I am not familiar with Matlab or either of these alternatives. I have downloaded and installed SciLab and am trying to get to the “Hello world” stage. It seems very powerful but I have to figure out this language works. In looking at what **ZenBeam **provided, for example, are the variables untyped? What does sintone return and how does wavwrite type the first argument? Are they both real vectors?

I think there must be utilities out there that would turn an excel spreadsheet into a wav file. If you want to add different sine waves together or other stuff that you could picture how to do with excel, it hardly seems necessary to send you back to the Hello World stage just to get wav files.

sinetone returns a double precision (8 byte) real. In Matlab*, all numeric types are double unless you explicitly change them, and the same seems to be true for Octave, so you don’t have to worry about a variable’s type. I’ve never used SciLab, so I can’t answer for that.

  • I keep mentioning Matlab because I use that a lot at work. I use Octave at home because it’s free and works almost the same as Matlab.

I am gradually figuring this out. I can get both of these to work but don’t understand the syntax, so I don’t understand the difference. They do sound different:

–>s=[sin(2*%pit440);0.5sin(2%pit880)];

–>s=[sin(2*%pit440)+0.5sin(2%pit880)];

Basic if you know how. I can’t figure out how to do this.

A semicolon is a row separator, so you will have built a matrix with 2 rows in the first example. (I assume you created t with the linspace function, and thus s is a nice long vector of values.) If you used playsnd to play s, in the first example you will have got different output in the two stereo channels, one with A[sub]4[/sub] the other with A[sub]5[/sub]. The second example will have played the same thing through both channels, A[sub]4[/sub] with a second harmonic.

The function csvRead will read a comma separated value file into an array. You should be able to play that with no further work.

Thanks for that. When I play them they sound quite different, which surprises me if the only difference is stereo channel separation.

I’ll try csvRead when I get some time.

To OP: Not a direct reply, but–there are a whole bunch of people working with your ideas (in fact a friend submitted a computer-massaged exploration of harmonics at Juilliard 20 years ago)–some of whom get a little obsessed–and are scattered around the net at websites galore, some independent and some connected with music departments at schools.

Having said that, I don’t have any offhand except IRCAM, a French-government supported which I remember from Grad School a zillion years ago (inFrench/English).

Good luck and have fun with your work! (PM me if you want to talk to the harmonics-generation guys.)