views:

507

answers:

6

Is there a way to play a Christmas tune on a PC or Mac without having a pre-recorded sound file? (No .mp3 or .wav or whatever-sound file)

I remember on my TI 99/4A and Apple II sounds (resembling music) could be played. Not sure if modern computers have these abilities (aside from beep).

+1  A: 

MIDI is an option, although on a PC it usually sounds almost as bad as beep.

MusiGenesis
That's an artifact of having a bad soundbank. With a high-quality soundbank, MIDI can sound almost as good as real music. In fact, the music community uses MIDI every day; they don't have the same negative views of MIDI that programmers often have.
David
+3  A: 

Yes, you can play midi.

Midi doesn't encode sounds per se, it encodes information used to play music; the pitch, tone, intensity, etc.

There is a C# midi toolkit on codeplex at :http://www.codeproject.com/KB/audio-video/MIDIToolkit.aspx

The quality of the sound depends entirely on the midi device used to play it, so it will vary in quality from computer to computer.

You can find a nice list of Christmas midi files at: http://www.lockergnome.com/midi/

Windows Media Player can play midi files as can Quick Time (I believe).

Stever B
+3  A: 
PLAY "e4 e4 e2 e4 e4 e2 e4 g4 c4 d4 e2"
yjerem
What language is this?
Ricket
@Ricket: it's QBASIC to me. maybe it's another BASIC to somebody else.
yjerem
+2  A: 

What about generating PCM data on the fly? PCM - Pulse Code Modulated - sound is just a bunch of samples of voltage across an analog sound system.

Think about a speaker. As sound is played, it vibrates. What if you took a ruler and measured the location of the speaker at a rate faster then the frequency of the sound? You would get a picture of a waveform. That's exactly what PCM data looks like, with each measurement stored as an 8 or 16 bit int. The frequency, say 44khz is the number of samples per second. CDs use 44khz sampling frequency and 16 bit samples.

DirectSound (on windows) and OpenAL (cross platform) are two libraries you can use to play databuffers full of PCM data. I've used DirectSound in the past, not to play data but rather to read in data from the microphone to get the volume level.

If you wanted to create a PCM sample for a certain note, you just calculate the frequency (here's a table), and then put a sinewave in your buffer. You can mix different frequencies together just by adding them (make sure the sum is less then the maximum volume, to avoid clipping)

Chad Okere
+6  A: 

"Jingle Bells" in java (bloated as usual), using JFugue, with tubular bells and xylophones (polyphonic!):

import org.jfugue.*;

public class JingleBells
{
    public static void main(String[] args)
    {
     Player player = new Player();
     player.play("T170 "+
        "     V0 I[XYLOPHONE] C4q C4q C3h C4q C4q C3h C3q B3q A3q G3q C4h "+
        "     V1 I[TUBULAR_BELLS] E5q E5q E5h E5q E5q E5h E5q G5q C5q D5q Eqh "+
        "     V2 I[XYLOPHONE] G3h     G2q G3q G3h     G3h");
    }
}
friol
+4  A: 

Speaking of "as bad as beep", if you have beep installed on your linux box, you can run the following shell script (in the same vein as Jeremy Ruten's answer):

#!/bin/sh
beep -f 659 -l 400
sleep 0.05
beep -f 659 -l 400
sleep 0.05
beep -f 659 -l 800
sleep 0.05
beep -f 659 -l 400
sleep 0.05
beep -f 659 -l 400
sleep 0.05
beep -f 659 -l 800
sleep 0.05
beep -f 659 -l 400
sleep 0.05
beep -f 783 -l 400
sleep 0.05
beep -f 523 -l 400
sleep 0.05
beep -f 587 -l 400
sleep 0.05
beep -f 659 -l 800
Adam Bellaire