Skip to content

Commit 22b0d44

Browse files
committed
first working version
1 parent 5528b7b commit 22b0d44

File tree

11 files changed

+97
-21
lines changed

11 files changed

+97
-21
lines changed

README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
# Piano
22

33
## Background
4-
- I like playing the Piano, and often indulge in it.
5-
- The frequemcy of the different notes was obtained from <a href="https://pages.mtu.edu/~suits/notefreqs.html">here</a>.
4+
- I like playing the Piano, and often indulge in it
5+
- The frequemcy of the different notes was obtained from <a href="https://pages.mtu.edu/~suits/notefreqs.html">here</a>
66

77
## Setup
88
My setup was on Ubuntu 20.04. To download SFML, run:

include/black_key.h

+1-3
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,14 @@
11
#ifndef BLACK_KEY_H_INCLUDED
22
#define BLACK_KEY_H_INCLUDED
33

4-
#include <SFML/Graphics.hpp>
5-
64
#include "config.h"
75
#include "key.h"
86

97
class BlackKey : public Key
108
{
119
public:
1210
BlackKey();
13-
BlackKey(sf::RenderWindow* renderWindow, int xPos, double pitchInit);
11+
BlackKey(sf::RenderWindow* renderWindow, int xPos, double pitchInit, sf::SoundBuffer* soundBuffer);
1412
~BlackKey();
1513
};
1614

include/config.h

+4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
#ifndef CONFIG_H_INCLUDED
22
#define CONFIG_H_INCLUDED
33

4+
#include <string>
5+
46
const int WINDOW_WIDTH = 900;
57
const int WINDOW_HEIGHT = 400;
68

9+
const std::string soundFile = "/media/adit/HDD/GITHUB/Piano/res/sound/sound.ogg";
10+
711
#endif // CONFIG_H_INCLUDED

include/key.h

+5-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#define KEY_H_INCLUDED
33

44
#include <SFML/Graphics.hpp>
5+
#include <SFML/Audio.hpp>
56
#include <iostream>
67

78
class Key
@@ -10,14 +11,17 @@ class Key
1011
sf::RenderWindow* window;
1112
sf::RectangleShape* rect;
1213

13-
double pitch;
14+
sf::Sound* sound;
15+
16+
int isPlaying;
1417

1518
public:
1619
Key();
1720
~Key();
1821

1922
void draw();
2023
void play();
24+
void stopPlaying();
2125
};
2226

2327
#endif // KEY_H_INCLUDED

include/piano.h

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
#ifndef PIANO_H_INCLUDED
22
#define PIANO_H_INCLUDED
33

4-
#include <SFML/Graphics.hpp>
5-
64
#include "white_key.h"
75
#include "black_key.h"
86
#include "sound_config.h"
@@ -14,6 +12,9 @@ class Piano
1412
sf::Event event;
1513
sf::RectangleShape* background;
1614

15+
sf::SoundBuffer* soundBuffer;
16+
sf::Sound* sound;
17+
1718
WhiteKey* whiteKeys[7];
1819
BlackKey* blackKeys[5];
1920

include/white_key.h

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
#ifndef WHITE_KEY_H_INCLUDED
22
#define WHITE_KEY_H_INCLUDED
33

4-
#include <SFML/Graphics.hpp>
5-
64
#include "config.h"
75
#include "key.h"
86

7+
const sf::Color whiteKeyPressedColor(200, 200, 200);
8+
99
class WhiteKey : public Key
1010
{
1111
public:
1212
WhiteKey();
13-
WhiteKey(sf::RenderWindow* renderWindow, int num, double pitchInit);
13+
WhiteKey(sf::RenderWindow* renderWindow, int num, double pitchInit, sf::SoundBuffer* soundBuffer);
1414
~WhiteKey();
1515
};
1616

res/sound/sound.ogg

66.2 KB
Binary file not shown.

src/black_key.cpp

+4-2
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,11 @@ BlackKey::BlackKey(){
44

55
}
66

7-
BlackKey::BlackKey(sf::RenderWindow* renderWindow, int xPos, double pitchInit){
7+
BlackKey::BlackKey(sf::RenderWindow* renderWindow, int xPos, double pitch, sf::SoundBuffer* soundBuffer){
88
window = renderWindow;
9-
pitch = pitchInit;
9+
10+
sound->setBuffer(*soundBuffer);
11+
sound->setPitch(pitch);
1012

1113
rect->setPosition(sf::Vector2f(xPos+10, WINDOW_HEIGHT/6));
1214
rect->setSize(sf::Vector2f(WINDOW_WIDTH/15-20, WINDOW_HEIGHT/2.5));

src/key.cpp

+27-1
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,42 @@
22

33
Key::Key(){
44
rect = new sf::RectangleShape();
5+
sound = new sf::Sound();
6+
7+
rect->setOutlineThickness(5);
8+
rect->setOutlineColor(sf::Color::Black);
9+
10+
isPlaying = 0;
511
}
612

713
Key::~Key(){
814
delete rect;
15+
delete sound;
916
}
1017

1118
void Key::draw(){
1219
window->draw(*rect);
1320
}
1421

1522
void Key::play(){
16-
std::cout << pitch << std::endl;
23+
if (isPlaying == 0){
24+
sound->play();
25+
isPlaying = 2;
26+
27+
if (rect->getFillColor() == sf::Color::White)
28+
rect->setFillColor(sf::Color(200, 200, 200));
29+
else if (rect->getFillColor() == sf::Color::Black)
30+
rect->setFillColor(sf::Color(50, 50, 50));
31+
}
32+
}
33+
34+
void Key::stopPlaying(){
35+
if (rect->getFillColor() == sf::Color(200, 200, 200))
36+
rect->setFillColor(sf::Color::White);
37+
else if (rect->getFillColor() == sf::Color(50, 50, 50))
38+
rect->setFillColor(sf::Color::Black);
39+
40+
isPlaying -= (isPlaying > 0) ? 1 : 0;
41+
if (isPlaying == 0)
42+
sound->stop();
1743
}

src/piano.cpp

+44-3
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,24 @@
22

33
Piano::Piano(){
44
window = new sf::RenderWindow(sf::VideoMode(WINDOW_WIDTH, WINDOW_HEIGHT), "Piano");
5-
window->setFramerateLimit(10);
5+
window->setFramerateLimit(20);
66

77
background = new sf::RectangleShape();
88
background->setPosition(sf::Vector2f(0, 0));
99
background->setSize(sf::Vector2f(WINDOW_WIDTH, WINDOW_HEIGHT));
1010
background->setFillColor(sf::Color::Cyan);
11+
12+
soundBuffer = new sf::SoundBuffer();
13+
if (!soundBuffer->loadFromFile(soundFile)){
14+
std::cout << "Couldn't load sound file. Exiting now.." << std::endl;
15+
exit(1);
16+
}
1117

1218
for (int i=0; i<7; i++)
13-
whiteKeys[i] = new WhiteKey(window, i, (frequency[i]/frequency[0]));
19+
whiteKeys[i] = new WhiteKey(window, i, (frequency[i]/frequency[0]), soundBuffer);
1420
double xPosBlack[5] = {9.0/30.0, 11.0/30.0, 15.0/30.0, 17.0/30.0, 19.0/30.0};
1521
for (int i=0; i<5; i++)
16-
blackKeys[i] = new BlackKey(window, WINDOW_WIDTH*xPosBlack[i], (frequency[i+7]/frequency[0]));
22+
blackKeys[i] = new BlackKey(window, WINDOW_WIDTH*xPosBlack[i], (frequency[i+7]/frequency[0]), soundBuffer);
1723
}
1824

1925
Piano::~Piano(){
@@ -52,26 +58,61 @@ void Piano::display(){
5258
void Piano::handleKeyboard(){
5359
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Num1))
5460
whiteKeys[0]->play();
61+
else
62+
whiteKeys[0]->stopPlaying();
63+
5564
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Num2))
5665
blackKeys[0]->play();
66+
else
67+
blackKeys[0]->stopPlaying();
68+
5769
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Num3))
5870
whiteKeys[1]->play();
71+
else
72+
whiteKeys[1]->stopPlaying();
73+
5974
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Num4))
6075
blackKeys[1]->play();
76+
else
77+
blackKeys[1]->stopPlaying();
78+
6179
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Num5))
6280
whiteKeys[2]->play();
81+
else
82+
whiteKeys[2]->stopPlaying();
83+
6384
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Num6))
6485
whiteKeys[3]->play();
86+
else
87+
whiteKeys[3]->stopPlaying();
88+
6589
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Num7))
6690
blackKeys[2]->play();
91+
else
92+
blackKeys[2]->stopPlaying();
93+
6794
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Num8))
6895
whiteKeys[4]->play();
96+
else
97+
whiteKeys[4]->stopPlaying();
98+
6999
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Num9))
70100
blackKeys[3]->play();
101+
else
102+
blackKeys[3]->stopPlaying();
103+
71104
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Num0))
72105
whiteKeys[5]->play();
106+
else
107+
whiteKeys[5]->stopPlaying();
108+
73109
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Dash))
74110
blackKeys[4]->play();
111+
else
112+
blackKeys[4]->stopPlaying();
113+
75114
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Equal))
76115
whiteKeys[6]->play();
116+
else
117+
whiteKeys[6]->stopPlaying();
77118
}

src/white_key.cpp

+4-4
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,15 @@ WhiteKey::WhiteKey(){
44

55
}
66

7-
WhiteKey::WhiteKey(sf::RenderWindow* renderWindow, int num, double pitchInit){
7+
WhiteKey::WhiteKey(sf::RenderWindow* renderWindow, int num, double pitch, sf::SoundBuffer* soundBuffer){
88
window = renderWindow;
9-
pitch = pitchInit;
9+
10+
sound->setBuffer(*soundBuffer);
11+
sound->setPitch(pitch);
1012

1113
rect->setPosition(sf::Vector2f((num+4)*WINDOW_WIDTH/15, WINDOW_HEIGHT/3));
1214
rect->setSize(sf::Vector2f(WINDOW_WIDTH/15, WINDOW_HEIGHT/2));
1315
rect->setFillColor(sf::Color::White);
14-
rect->setOutlineThickness(5);
15-
rect->setOutlineColor(sf::Color::Black);
1616
}
1717

1818
WhiteKey::~WhiteKey(){

0 commit comments

Comments
 (0)