Subject:
|
Play tunes with legOS: "Wir sind die Roboter"
|
Newsgroups:
|
lugnet.robotics.rcx.legos
|
Date:
|
Tue, 30 Nov 1999 01:42:27 GMT
|
Viewed:
|
1083 times
|
| |
| |
well, yesterday i promised to post some header file that makes it easy
to program the sound timer for playing tunes in a memory saving way -
here it is.
( see below, file notedefs.h )
the numbering scheme is N_<key>[m]_<number_of_octave>
N_A_0 corresponds to a quite low A-key, 55 Hz
N_A_8 is 8 octaves up, 14080 Hz
m, if present, denotes the ebony keys, e.g. N_Cm_3 is the ebony key just
right of the 3rd C-key (somewhere in the middle of the keyboard).
To play the desired key, put the MSB of the macro into the compare Match
register, put the 2 LSbits into CKS1/0 and set ICKS0 to one if bit7 is
set.
if that sounds confusing, below is a little application that plays "Wir
sind die Roboter", a somewhat wellknown song composed in the 70ies by
the german band KRAFTWERK. ( see below, file robots.c ).
The actual definition of the melody is in array robots[], consuming 36
Bytes of RAM 8-).
-------------- snip < file notedefs.h > snip --------
// some handy macros for tunes
#ifndef NOTEDEFS_H
#define NOTEDEFS_H
#define N_A_0 0x8d03
#define N_Am_0 0x8503
#define N_H_0 0x7e03
#define N_C_0 0x7603
#define N_Cm_0 0x7003
#define N_D_0 0x6903
#define N_Dm_0 0x6303
#define N_E_0 0x5e03
#define N_F_0 0x5803
#define N_Fm_0 0x5303
#define N_G_0 0x4f03
#define N_Gm_0 0x4a03
#define N_A_1 0x4603
#define N_Am_1 0x4203
#define N_H_1 0x3e03
#define N_C_1 0x3b03
#define N_Cm_1 0x3703
#define N_D_1 0x3403
#define N_Dm_1 0x3103
#define N_E_1 0x2e03
#define N_F_1 0x2c03
#define N_Fm_1 0x2903
#define N_G_1 0x2703
#define N_Gm_1 0xf683
#define N_A_2 0xe883
#define N_Am_2 0xdb83
#define N_H_2 0xcf83
#define N_C_2 0xc383
#define N_Cm_2 0xb883
#define N_D_2 0xae83
#define N_Dm_2 0xa483
#define N_E_2 0x9b83
#define N_F_2 0x9283
#define N_Fm_2 0x8a83
#define N_G_2 0x8283
#define N_Gm_2 0x7a83
#define N_A_3 0x7483
#define N_Am_3 0x6d83
#define N_H_3 0xfc02
#define N_C_3 0xee02
#define N_Cm_3 0xe002
#define N_D_3 0xd402
#define N_Dm_3 0xc802
#define N_E_3 0xbd02
#define N_F_3 0xb202
#define N_Fm_3 0xa802
#define N_G_3 0x9e02
#define N_Gm_3 0x9502
#define N_A_4 0x8d02
#define N_Am_4 0x8502
#define N_H_4 0xfc82
#define N_C_4 0xee82
#define N_Cm_4 0xe082
#define N_D_4 0xd482
#define N_Dm_4 0xc882
#define N_E_4 0xbd82
#define N_F_4 0xb282
#define N_Fm_4 0xa882
#define N_G_4 0x9e82
#define N_Gm_4 0x9582
#define N_A_5 0x8d82
#define N_Am_5 0x8582
#define N_H_5 0x7e82
#define N_C_5 0x7682
#define N_Cm_5 0x7082
#define N_D_5 0x6982
#define N_Dm_5 0x6382
#define N_E_5 0x5e82
#define N_F_5 0x5882
#define N_Fm_5 0x5382
#define N_G_5 0x4f82
#define N_Gm_5 0x4a82
#define N_A_6 0x4682
#define N_Am_6 0x4282
#define N_H_6 0xfc01
#define N_C_6 0xee01
#define N_Cm_6 0xe001
#define N_D_6 0xd401
#define N_Dm_6 0xc801
#define N_E_6 0xbd01
#define N_F_6 0xb201
#define N_Fm_6 0xa801
#define N_G_6 0x9e01
#define N_Gm_6 0x9501
#define N_A_7 0x8d01
#define N_Am_7 0x8501
#define N_H_7 0x7e01
#define N_C_7 0x7601
#define N_Cm_7 0x7001
#define N_D_7 0x6901
#define N_Dm_7 0x6301
#define N_E_7 0x5e01
#define N_F_7 0x5801
#define N_Fm_7 0x5301
#define N_G_7 0x4f01
#define N_Gm_7 0x4a01
#define N_A_8 0x4601
#endif
-------------- snip snip ----------------------------
-------------- snip < file robots.c > snip ----------
// play "wir sind die roboter"
#include <sys/h8.h>
#include <unistd.h>
#include "notedefs.h"
void playNote(unsigned note)
{
unsigned char CKSmask = note & 0xff;
unsigned char match = note >> 8;
// start timer
T0_CR = 0x00; // timer off
T0_CNT = 0x00; // counter reset
if (CKSmask & 0x80)
STCR |= 0x01; // ICKS0 = 1
else
STCR &= ~0x01; // ICKS0 = 0
T0_CORA = match; // set compare match A
T0_CSR = CSR_TOGGLE_ON_A; // Output toggles on compare Match A
T0_CR = CR_CLEAR_ON_A | (CKSmask &0x3); // Go
}
void quiet()
{
T0_CR = 0x00; // timer 0 off
}
typedef struct tone
{
unsigned note;
unsigned char len;
} tone;
void playTone( tone* t , unsigned msecs)
{
unsigned duration = t->len*msecs;
unsigned ptime = duration * 13 /16;
playNote(t->note);
msleep(ptime);
quiet();
msleep(duration-ptime);
}
tone robots[] =
{
{ N_D_3, 2 } , { N_C_3, 1 } , { N_D_3, 1 },
{ N_F_3, 1 } , { N_D_3, 1 } , { N_D_3, 2 },
{ N_F_3, 2 } , { N_G_3, 1 } , { N_C_4, 1 },
{ N_A_4, 2 } , { N_D_3, 2 } ,
{ 0, 0 }
};
void playMelody(tone *melody,unsigned msecs)
{
while ( melody->note )
{
playTone(melody,msecs);
++melody;
}
}
int main(int argc,char *argv[])
{
while (1)
playMelody(robots,200);
return 0;
}
-------------- snip snip ----------------------------
|
|
Message has 1 Reply:
2 Messages in This Thread:
- Entire Thread on One Page:
- Nested:
All | Brief | Compact | Dots
Linear:
All | Brief | Compact
|
|
|
|