sid_library
This is an old revision of the document!
Table of Contents
This library allows the operation of the SIDaster Shield (V2.3+) by specifying setup / communication functions. See the comments inside the library file files for in depth description.
Installation
- Go to your
Arduino\Library
subfolder. - delete any previous installation of the SID library by removing the
SID
folder - Extract files from the library archive to your
Arduino\Library
subfolder.
Usage
You shall first include the library inside its Arduino design by addin a call to the library:
#include « SID.h »
Then you need to create an instance of the SID type, and initilalise it:
- Create a SID synth by declaring it :
SID mysid;
- In the Arduino
setup()
function, shall setup successively the clock and the SPI interface by calling in this ordermysid.clk_setup();
mysid.SPI_setup();
- Send bytes to the SID by using
sid.send(Address, Data);
Address is the SID address ranging 0-24 as mentionned in the SID datasheet, and the Data is the byte you want to write. See the mos6581 datasheet for more details on how to program the Chip.
Code example
Here is an example of how to use the library:
#include <SID.h> #include <MIDI.h> // Pulse width must be less than 0x0FFF (4095) // note - must also be higher than nothing if using a square wave // #define PULSE_WIDTH 0x0 // These all must be below 0x0F (15) static const unsigned char BASE_NOTE = 12; static const unsigned int NOTES[] = { 291, 308, 326, 346, 366, 388, 411, 435, 461, 489, 518, 549, 581, 616, 652, 691, 732, 776, 822, 871, 923, 978, 1036, 1097, 1163, 1232, 1305, 1383, 1465, 1552, 1644, 1742, 1845, 1955, 2071, 2195, 2325, 2463, 2610, 2765, 2930, 3104, 3288, 3484, 3691, 3910, 4143, 4650, // 4650 = C4 4927, 5220, 5530, 5859, 6207, 6577, 6968, 7382, 7821, 8286, 8779, 9301, 9854, 10440, 11060, 11718, 12415, 13153, 13935, 14764, 15642, 16572, 17557, 18601, 19709, 20897, 22121, 23436, 24830, 26306, 27871, 29528, 31234, 33144, 35115, 37203, 39415, 41759, 44242, 46873, 49660, 52613, 55741, 59056, 62567}; //============================== // GLOBAL SYNTH VARIABLES INIT //============================== // Coarse Frequency, base 2, value from 0 (-2) to 5 (+2) static byte coarse1 = 0; static byte coarse2 = 0; static byte coarse3 = 0; // Default midi channel static byte mchannel = 1; // Synth State Managment static boolean trigged = false; // indicating if synth is playing static boolean retrigger = true; // indicating if retriggering ADSR is on // Local Osc Frequency // Registers @ B00000 and B00001 // Default 440hz = 0x1CD5 static word freq1 = 0x1CD5; static word freq2 = 0x1CD5; static word freq3 = 0x1CD5; // PWM // Default Square Wave = 0x800 // Formula : PWout = (PWn/40.95) % static word pw1 = 0x800; static word pw2 = 0x800; static word pw3 = 0x800; // Control Register // Default: Saw // Content NOI|SQR|SAW|TRI|TST|MOD|SYN|GAT static byte ctrl1 = 0X21; static byte ctrl2 = 0X21; static byte ctrl3 = 0X21; // Attack and Decay // Default : Shortest static byte ad1 = 0x00; static byte ad2 = 0x00; static byte ad3 = 0x00; // Sustain and Release // Default: Max sustain and no release static byte sr1 = 0xF0; static byte sr2 = 0xF0; static byte sr3 = 0xF0; // Cutoff Frequency static word freq = 0x7FF; // Resonnance and CTRL // Res= 0 no resonnance, Res=F max resonnance // Routing 0 is no filter, 1 is filter // RES(4)|EXT|F3|F2|F1 static byte res = 0x1; // Mode // Content EXT|HP|BP|LP|VOL(4) static byte mode = 0x1F; // Mono Note Memory static byte last_note = 0; static byte current_note = 0; static byte notes = 0; // ######################################################## SID sid; // Give voices some initial params // void synth_init() { // Freq init sid.send(SID_FREQ1LO,char(freq1)); sid.send(SID_FREQ1HI,char(freq1>>8)); sid.send(SID_FREQ2LO,char(freq2)); sid.send(SID_FREQ2HI,char(freq2>>8)); sid.send(SID_FREQ3LO,char(freq3)); sid.send(SID_FREQ3HI,char(freq3>>8)); // PWM Init sid.send(SID_PW1LO,char(pw1)); sid.send(SID_PW1HI,char(pw1>>8)); sid.send(SID_PW2LO,char(pw2)); sid.send(SID_PW2HI,char(pw2>>8)); sid.send(SID_PW3LO,char(pw3)); sid.send(SID_PW3HI,char(pw3>>8)); // CTRL sid.send(SID_CTRL1,ctrl1); sid.send(SID_CTRL2,ctrl2); sid.send(SID_CTRL3,ctrl3); // ADSR sid.send(SID_AD1,ad1); sid.send(SID_SR1,sr1); sid.send(SID_AD2,ad2); sid.send(SID_SR2,sr2); sid.send(SID_AD3,ad3); sid.send(SID_SR3,sr3); // Filter + Main sid.send(SID_FCLO,char(freq)); sid.send(SID_FCHI,char(freq>>8)); sid.send(SID_RES,res); sid.send(SID_MODE,mode); } // Play sound at frequency specified on voice specified // void synth_on(int note) { freq1=NOTES[note]; sid.send(SID_FREQ1LO,char(freq1)); sid.send(SID_FREQ1HI,char(freq1>>8)); freq2=NOTES[note+3]; sid.send(SID_FREQ2LO,char(freq2)); sid.send(SID_FREQ2HI,char(freq2>>8)); freq3=NOTES[note+7]; sid.send(SID_FREQ3LO,char(freq3)); sid.send(SID_FREQ3HI,char(freq3>>8)); ctrl1|=1; sid.send(SID_CTRL1,ctrl1); ctrl2|=1; sid.send(SID_CTRL2,ctrl2); ctrl3|=1; sid.send(SID_CTRL3,ctrl3); } void synth_off(int note) { ctrl1&=~1; sid.send(SID_CTRL1,ctrl1); ctrl2&=~1; sid.send(SID_CTRL2,ctrl2); ctrl3&=~1; sid.send(SID_CTRL3,ctrl3); } // Debug 2 bytes of data via the address + data shifters // // Initially executed code // void setup() { // Set up the SID chip sid.clk_setup(); sid.SPI_setup(); // Give the voices some initial values // Set up the MIDI input MIDI.begin(); MIDI.setHandleNoteOn(DoHandleNoteOn); MIDI.setHandleNoteOff(DoHandleNoteOff); synth_init(); } // Main program loop // void loop() { MIDI.read(); } // Note On Handler // PS : The if / else is implemented because some Midi equipements do not use Note Off, but use instead note On with velocity = 0 void DoHandleNoteOn (byte channel, byte note, byte velocity) { synth_on(note); } void DoHandleNoteOff (byte channel, byte note, byte velocity) { synth_off(note); }
sid_library.1317122457.txt.gz · Last modified: 2018/08/26 09:36 (external edit)