The music notes is defined by frequency variations,
reference wiki - music note and
C (musical note).
Generally, the calculation is based on A4 music note:
f = [2 ^ (n/12)] × 440 Hz
For easily doing calculation, here we use C-1 as the base level:
There are all 11 levels, each level has 12 music notes
n = (i x 12)(level) + j (jth note)
f = [2 ^ (n/12)] x 8.176 Hz
Calculate all music note's frequencies by python:
# calculate number array musicNotes = [] for i in range(11): row = [] for j in range(12): row.append(8.176 * (2**((i*12+j)*(1.0/12)))) musicNotes.append(row) # display frequency table for i in range(11): musicNotes[i] [8.1760000000000002, 8.6621702594815986, 9.1772497069774346, 9.7229573722622469, 10.301114503940484, 10.913650647694201, 11.562610085962426, 12.250158660543748, 12.978591000891997, 13.75033818222874, 14.567975839030828, 15.434232760971051] [16.352, 17.324340518963197, 18.354499413954866, 19.445914744524494, 20.602229007880968, 21.827301295388398, 23.125220171924852, 24.500317321087497, 25.957182001783995, 27.50067636445748, 29.135951678061655, 30.868465521942102] [32.704000000000001, 34.648681037926387, 36.708998827909731, 38.891829489048988, 41.204458015761929, 43.654602590776797, 46.250440343849704, 49.000634642174987, 51.914364003567989, 55.001352728914959, 58.271903356123303, 61.736931043884205] [65.408000000000001, 69.297362075852774, 73.417997655819462, 77.783658978097975, 82.408916031523859, 87.309205181553594, 92.500880687699407, 98.001269284349974, 103.82872800713598, 110.00270545782992, 116.54380671224661, 123.47386208776841] [130.816, 138.59472415170555, 146.8359953116389, 155.56731795619595, 164.81783206304772, 174.61841036310716, 185.00176137539881, 196.00253856869995, 207.6574560142719, 220.00541091565984, 233.08761342449321, 246.94772417553673] [261.63200000000001, 277.1894483034111, 293.67199062327779, 311.1346359123919, 329.63566412609543, 349.23682072621432, 370.00352275079763, 392.00507713739989, 415.3149120285438, 440.01082183131967, 466.17522684898643, 493.89544835107347] [523.26400000000001, 554.3788966068222, 587.34398124655559, 622.2692718247838, 659.27132825219087, 698.47364145242864, 740.00704550159526, 784.01015427479979, 830.6298240570876, 880.02164366263935, 932.35045369797285, 987.79089670214694] [1046.528, 1108.7577932136444, 1174.6879624931112, 1244.5385436495676, 1318.5426565043817, 1396.9472829048573, 1480.0140910031905, 1568.0203085495996, 1661.2596481141752, 1760.0432873252787, 1864.7009073959457, 1975.5817934042939] [2093.056, 2217.5155864272874, 2349.3759249862223, 2489.0770872991352, 2637.0853130087612, 2793.8945658097145, 2960.028182006381, 3136.0406170991969, 3322.5192962283504, 3520.0865746505574, 3729.4018147918891, 3951.1635868085878] [4186.1120000000001, 4435.0311728545748, 4698.7518499724447, 4978.1541745982704, 5274.1706260175224, 5587.7891316194291, 5920.0563640127621, 6272.0812341983938, 6645.0385924567008, 7040.1731493011148, 7458.8036295837783, 7902.3271736171755] [8372.2240000000002, 8870.0623457091497, 9397.5036999448894, 9956.3083491965408, 10548.341252035045, 11175.578263238858, 11840.112728025524, 12544.162468396788, 13290.077184913402, 14080.34629860223, 14917.607259167557, 15804.654347234351]
Base on above calculated values, we define const float gMusicNotes[11][12] , all the frequencies are recorded in the array.
The circuit schematic:
The main process:
(Depends on MCU's characters, for PIC16F877A , the compiler is PICC, the memory size of music_cell gSong[] is limited.)
(This program implements delay process, the deviation of sound frequency is obvious; replace by timer process, the sound frequency would be better than delay process)
/******************************************************************************* Copyright (c) 2008 Wizign Ltd. All Rights Reserved. main.c: Main program for demo trigger of speaker Version: 1.0.0 Date: Oct 03, 2008 Author: YenHung Chen E-mail: yhchen@wizign.com Revision: ---------- ----------------------------------------------------------------- 2008/10/03 Created by YenHung Chen, demo trigger of speaker for both 8051 and PIC ******************************************************************************/ /******************************************************************************* <>For AT89S52 LCD display: There are 8 data pins, which are connected P2. The control pins are connected to P1_0, P1_1, and P1_2. Speaker: P3_0 <>For PIC16F877A LCD display: There are 8 data pins, which are connected PORTD The control pins are connected to RA0, RA1, and RA2. Speaker: RC0 ******************************************************************************/ #include "global.h" #include "delay.h" #include "lcd.h" #include "timer.h" #include "music.h" //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ // define IO and global variables #ifdef MCU_PIC /* // Configurations typedef unsigned int config; config at 0x2007 __CONFIG = _XT_OSC & _PWRTE_OFF & _BODEN_OFF & _WDT_OFF & _LVP_OFF; */ #endif #ifdef MCU_PIC bank1 static music_cell gSong[] = { #else // default is MCU_8051 music_cell gSong[] = { #endif {C4, LA, 1}, {C4, LA, 0.25}, {C4, SO, 0.25}, {C4, LA, 0.5}, {C4, SO, 1}, {C4, MI, 1}, {C4, SO, 0.5}, {C4, MI, 0.5}, {C4, MI, 0.5}, {C4, RE, 0.5}, {C4, MI, 2}, {C4, MI, 0}, {C4, RE, 1}, {C4, RE, 0.5}, {C4, DO, 0.5}, {C4, RE, 0.5}, {C4, RE, 0.5}, {C4, SO, 1}, {C4, SO, 0.5}, {C4, MI, 0.5}, {C4, MI, 0.5}, {C4, RE, 0.5}, {C4, MI, 2}, {C4, MI, 0}, {C4, LA, 1}, {C4, LA, 0.5}, {C4, SO, 0.5}, {C4, LA, 0.5}, {C4, SO, 0.5}, {C4, MI, 1}, {C4, FA, 0.5}, {C4, FA, 0.25}, {C4, FA, 0.25}, {C4, MI, 0.5}, {C4, RE, 0.5}, {C4, MI, 2}, {C4, MI, 0}, {C4, SO, 1}, {C4, SO, 0.5}, {C4, SO, 0.5}, {C4, SO, 0.5}, {C4, SO, 0.5}, {C4, SI, 1}, {C4, LA, 0.5}, {C4, LA, 0.25}, {C4, LA, 0.25}, {C4, LA, 0.5}, {C4, SO, 0.5}, {C4, LA, 2}, {C4, LA, 0}, {C5, DO, 1}, {C5, DO, 0.5}, {C5, DO, 0.5}, {C4, SI, 1}, {C4, SO, 1}, {C4, LA, 0.5}, {C4, LA, 0.25}, {C4, LA, 0.25}, {C4, LA, 0.5}, {C4, SO, 0.5}, {C4, LA, 1.5}, {C4, SO, 0.25}, {C4, MI, 0.25}, {C4, SO, 1}, {C4, SO, 0.75}, {C4, SO, 0.25}, {C4, SO, 0.5}, {C4, SO, 0.5}, {C4, SI, 1}, {C4, LA, 0.33}, {C4, LA, 0.33}, {C4, LA, 0.33}, {C4, LA, 0.5}, {C4, SO, 0.5}, {C4, LA, 2}, {C4, LA, 0} }; //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ // define functions /* Initilize parameters before run the main loop */ void InitParams(void) { LCDInit(); MusicInit(); } /* Display message on LCD panel */ void DisplayMusicStatus(char* pMsg) { LCDGoto(0); // goto 1st line LCDPuts(" WIZIGN LTD. "); // Display rotate status LCDGoto(0x40); // goto 2nd line LCDPuts(pMsg); } //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ // main program void main(void) { int count = 0; int direction = 0; // 0:clockwise, 1:counter clockwise // Initilize parameters InitParams(); /* do loop */ while(1){ DisplayMusicStatus("Play Music"); MusicPlay(10, gSong); } }
No comments:
Post a Comment