개발자/Raspberry Pi3

라즈베리파이 음악 연주 코드

지구빵집 2018. 11. 22. 00:52
반응형

 

 

라즈베리파이 수동부저 모듈사용하여 음악 연주하기

 

라즈베리파이에서 수동부저를 사용하여 음악을 연주하는 코드를 시험한다. 일단 아래와 같이 아두이노에서 많이 사용하는 부저 스피커 모듈을 준비한다. 보통 3.3V 이상~ 5V 에서 동작하므로 라즈베리파이와 직접 연결해도 잘 동작한다.

참고 : https://github.com/leon-anavi/rpi-examples

 

 

아래 테스트에 들어가기 전에 라즈베리파이에 "wiringPi 라이브러리"를 설치해야 하고, 라즈베리파이 GPIO 에 대해 약간은 알고 있어야 한다. 혹시 처음 여기에 들어온 사람은 아래 포스팅을 참고하면 좋겠고, 준비가 안되어 있어도 따라하면 된다. 실수를 통해 더욱 많은 것을 배울 수 있다는 사실은 만고불변의 진리다. 일단 오래 기억되니까 말이다.

라즈베리파이의 GPIO를 제어할 수 있는 wiringPi 라이브러리의 설치 방법 https://diymaker.tistory.com/56

Raspberry Pi GPIO 와 wiringPi 라이브러리 https://diymaker.tistory.com/57

아래 코드는 은하계 전쟁을 다룬 불멸의 SF 영화 스타워즈의 주제곡이다. 소스코드 파일네임은 starwars.c 로 하고 입력한다. 다음 곡은 슈퍼마리오 게임에서 나오는 곡이다. 

'$'는 입력하지 말고 컴파일은 

$gcc -o starwars starwars.c -l wiringPi

에러없이 컴파일이 되면 아래 명령어로 실행한다. 거칠지만 아름다운 띠~띠띠띠 소리가 난다. 

$sudo ./starwars

 

 

 

 

슈퍼마리오 앞 부분 연주

 

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <stdio.h>
#include <errno.h>
#include <string.h>
 
#include <wiringPi.h>
#include <softTone.h>
 
#define PIN 28
 
int scale [23= { 6596590659052365907840,0,03920,0,05230,03920,0,330 } ;
 
int main ()
{
  int i ;
  wiringPiSetup () ;
  softToneCreate (PIN) ;
  for (i = 0 ; i < 23 ; ++i)
    {
      printf ("%3d\n", i) ;
      softToneWrite (PIN, scale [i]) ;
      delay (200) ;
    }
}
 
cs

 

 

스타워즈 한 번 연주코드

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
#include <wiringPi.h>
#include <stdio.h>
 
//Pin 11 on Raspberry Pi corresponds to BCM GPIO 17 and wiringPi pin 0
#define BeepPin 28
 
//FREQUENCIES
#define cL 129
#define cLS 139
#define dL 146
#define dLS 156
#define eL 163
#define fL 173
#define fLS 185
#define gL 194
#define gLS 207
#define aL 219
#define aLS 228
#define bL 232
 
#define c 261
#define cS 277
#define d 294
#define dS 311
#define e 329
#define f 349
#define fS 370
#define g 391
#define gS 415
#define a 440
#define aS 455
#define b 466
 
#define cH 523
#define cHS 554
#define dH 587
#define dHS 622
#define eH 659
#define fH 698
#define fHS 740
#define gH 784
#define gHS 830
#define aH 880
#define aHS 910
#define bH 933
 
//This function generates the square wave that makes the piezo speaker sound at a determinated frequency.
void beep(unsigned int note, unsigned int duration)
{
  //This is the semiperiod of each note.
  long beepDelay = (long)(1000000/note);
  //This is how much time we need to spend on the note.
  long time = (long)((duration*1000)/(beepDelay*2));
  int i;
  for(i=0;i<time;i++)
  {
    //1st semiperiod
    digitalWrite(BeepPin, HIGH);
    delayMicroseconds(beepDelay);
    //2nd semiperiod
    digitalWrite(BeepPin, LOW);
    delayMicroseconds(beepDelay);
  }
 
  //Add a little delay to separate the single notes
  digitalWrite(BeepPin, LOW);
  delay(20);
}
 
//The source code of the Imperial March from Star Wars
void play()
{
  beep( a, 500);
  beep( a, 500);
  beep( f, 350);
  beep( cH, 150);
 
  beep( a, 500);
  beep( f, 350);
  beep( cH, 150);
  beep( a, 1000);
  beep( eH, 500);
 
  beep( eH, 500);
  beep( eH, 500);
  beep( fH, 350);
  beep( cH, 150);
  beep( gS, 500);
 
  beep( f, 350);
  beep( cH, 150);
  beep( a, 1000);
  beep( aH, 500);
  beep( a, 350);
 
  beep( a, 150);
  beep( aH, 500);
  beep( gHS, 250);
  beep( gH, 250);
  beep( fHS, 125);
 
  beep( fH, 125);
  beep( fHS, 250);
 
  delay(250);
 
  beep( aS, 250);
  beep( dHS, 500);
  beep( dH, 250);
  beep( cHS, 250);
  beep( cH, 125);
 
  beep( b, 125);
  beep( cH, 250);
 
  delay(250);
 
  beep( f, 125);
  beep( gS, 500);
  beep( f, 375);
  beep( a, 125);
  beep( cH, 500);
 
  beep( a, 375);
  beep( cH, 125);
  beep( eH, 1000);
  beep( aH, 500);
  beep( a, 350);
 
  beep( a, 150);
  beep( aH, 500);
  beep( gHS, 250);
  beep( gH, 250);
  beep( fHS, 125);
 
  beep( fH, 125);
  beep( fHS, 250);
 
  delay(250);
 
  beep( aS, 250);
  beep( dHS, 500);
  beep( dH, 250);
  beep( cHS, 250);
  beep( cH, 125);
 
  beep( b, 125);
  beep( cH, 250);
 
  delay(250);
 
  beep( f, 250);
  beep( gS, 500);
  beep( f, 375);
  beep( cH, 125);
  beep( a, 500);
 
  beep( f, 375);
  beep( c, 125);
  beep( a, 1000);
}
 
int main(void)
{
  //Check wiringPi setup
  if(-1 == wiringPiSetup())
  {
    printf("setup wiringPi failed!");
    return 1;
  }
 
  //Prepare GPIO0
  pinMode(BeepPin, OUTPUT);
  //Play the Imperial March
  play();
 
  return 0;
}
 
cs

 

 

반응형