Giter VIP home page Giter VIP logo

fabianoriccardi / melody-player Goto Github PK

View Code? Open in Web Editor NEW
37.0 1.0 7.0 117 KB

An Arduino library to play melodies on buzzers. Melodies can be easily written in RTTTL or in a custom human-readable format. Only for ESP8266 and ESP32.

License: GNU Lesser General Public License v2.1

C++ 96.02% C 3.98%
arduino arduino-library esp8266 esp32 buzzer melody piezoelectric sound rtttl ringtone

melody-player's People

Contributors

aaronkirschen avatar fabianoriccardi avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

melody-player's Issues

Issue with servo when playing melody

Hi,

Thank you for the library!
I found an issue, when a sevo is connected and melody is played, the servo goes crazy at the time of melody play.
Is there something that can be done?

Thank you

RTTTL playback tempo

Thanks for the great library. I use it to play RTTTL ringtones on my ESP8266 alarm clock. The first melody is played directly from a hardcoded String, when the device is plugged in. This melody plays fine.
I'd also like to play melodies ad hoc that I send via MQTT (pubsubclient library) as RTTTL string to the device. This also works, except that the melody plays far too slow.

Do you have any idea what could cause such a slow playback?

Error compiling for board SparkFun ESP32 Thing Plus.

Hi,
I get an Error during compile. See the log:

Arduino: 1.8.19 (Windows 10), Board: "SparkFun ESP32 Thing Plus, 80MHz, Default (6.25MB APP/OTA/3.43MB SPIFFS), 921600, None"

In file included from C:\Users\Admin\Documents\Arduino\libraries\Melody_Player\src/melody_player.h:4,

                 from C:\Users\Admin\Documents\Arduino\sketch_mar04a\sketch_mar04a.ino:8:

C:\Users\Admin\Documents\Arduino\libraries\Melody_Player\src/melody.h:26:70: error: 'vector' is not a member of 'std'

   Melody(String title, unsigned short timeUnit, std::shared_ptr<std::vector<NoteDuration>> notes, bool automaticSilence):

                                                                      ^~~~~~

C:\Users\Admin\Documents\Arduino\libraries\Melody_Player\src/melody.h:26:70: note: 'std::vector' is defined in header '<vector>'; did you forget to '#include <vector>'?

C:\Users\Admin\Documents\Arduino\libraries\Melody_Player\src/melody.h:6:1:

+#include <vector>

 

C:\Users\Admin\Documents\Arduino\libraries\Melody_Player\src/melody.h:26:70:

   Melody(String title, unsigned short timeUnit, std::shared_ptr<std::vector<NoteDuration>> notes, bool automaticSilence):

                                                                      ^~~~~~

C:\Users\Admin\Documents\Arduino\libraries\Melody_Player\src/melody.h:26:70: error: 'vector' is not a member of 'std'

C:\Users\Admin\Documents\Arduino\libraries\Melody_Player\src/melody.h:26:70: note: 'std::vector' is defined in header '<vector>'; did you forget to '#include <vector>'?

C:\Users\Admin\Documents\Arduino\libraries\Melody_Player\src/melody.h:26:77: error: template argument 1 is invalid

   Melody(String title, unsigned short timeUnit, std::shared_ptr<std::vector<NoteDuration>> notes, bool automaticSilence):

                                                                             ^~~~~~~~~~~~

C:\Users\Admin\Documents\Arduino\libraries\Melody_Player\src/melody.h:26:70: error: 'vector' is not a member of 'std'

   Melody(String title, unsigned short timeUnit, std::shared_ptr<std::vector<NoteDuration>> notes, bool automaticSilence):

                                                                      ^~~~~~

C:\Users\Admin\Documents\Arduino\libraries\Melody_Player\src/melody.h:26:70: note: 'std::vector' is defined in header '<vector>'; did you forget to '#include <vector>'?

C:\Users\Admin\Documents\Arduino\libraries\Melody_Player\src/melody.h:26:70: error: 'vector' is not a member of 'std'

C:\Users\Admin\Documents\Arduino\libraries\Melody_Player\src/melody.h:26:70: note: 'std::vector' is defined in header '<vector>'; did you forget to '#include <vector>'?

C:\Users\Admin\Documents\Arduino\libraries\Melody_Player\src/melody.h:26:89: error: template argument 1 is invalid

   Melody(String title, unsigned short timeUnit, std::shared_ptr<std::vector<NoteDuration>> notes, bool automaticSilence):

                                                                                         ^~

C:\Users\Admin\Documents\Arduino\libraries\Melody_Player\src/melody.h:26:70: error: 'vector' is not a member of 'std'

   Melody(String title, unsigned short timeUnit, std::shared_ptr<std::vector<NoteDuration>> notes, bool automaticSilence):

                                                                      ^~~~~~

C:\Users\Admin\Documents\Arduino\libraries\Melody_Player\src/melody.h:26:70: note: 'std::vector' is defined in header '<vector>'; did you forget to '#include <vector>'?

C:\Users\Admin\Documents\Arduino\libraries\Melody_Player\src/melody.h:26:70: error: 'vector' is not a member of 'std'

C:\Users\Admin\Documents\Arduino\libraries\Melody_Player\src/melody.h:26:70: note: 'std::vector' is defined in header '<vector>'; did you forget to '#include <vector>'?

C:\Users\Admin\Documents\Arduino\libraries\Melody_Player\src/melody.h:26:89: error: template argument 1 is invalid

   Melody(String title, unsigned short timeUnit, std::shared_ptr<std::vector<NoteDuration>> notes, bool automaticSilence):

                                                                                         ^~

C:\Users\Admin\Documents\Arduino\libraries\Melody_Player\src/melody.h:26:54: error: 'std::shared_ptr' is not a type

   Melody(String title, unsigned short timeUnit, std::shared_ptr<std::vector<NoteDuration>> notes, bool automaticSilence):

                                                      ^~~~~~~~~~

C:\Users\Admin\Documents\Arduino\libraries\Melody_Player\src/melody.h:26:64: error: expected ',' or '...' before '<' token

   Melody(String title, unsigned short timeUnit, std::shared_ptr<std::vector<NoteDuration>> notes, bool automaticSilence):

                                                                ^

C:\Users\Admin\Documents\Arduino\libraries\Melody_Player\src/melody.h:84:24: error: 'vector' is not a member of 'std'

   std::shared_ptr<std::vector<NoteDuration>> notes;

                        ^~~~~~

C:\Users\Admin\Documents\Arduino\libraries\Melody_Player\src/melody.h:84:24: note: 'std::vector' is defined in header '<vector>'; did you forget to '#include <vector>'?

C:\Users\Admin\Documents\Arduino\libraries\Melody_Player\src/melody.h:84:24: error: 'vector' is not a member of 'std'

C:\Users\Admin\Documents\Arduino\libraries\Melody_Player\src/melody.h:84:24: note: 'std::vector' is defined in header '<vector>'; did you forget to '#include <vector>'?

C:\Users\Admin\Documents\Arduino\libraries\Melody_Player\src/melody.h:84:31: error: template argument 1 is invalid

   std::shared_ptr<std::vector<NoteDuration>> notes;

                               ^~~~~~~~~~~~

C:\Users\Admin\Documents\Arduino\libraries\Melody_Player\src/melody.h:84:43: error: expected unqualified-id before '>' token

   std::shared_ptr<std::vector<NoteDuration>> notes;

                                           ^~

C:\Users\Admin\Documents\Arduino\libraries\Melody_Player\src/melody.h: In constructor 'Melody::Melody()':

C:\Users\Admin\Documents\Arduino\libraries\Melody_Player\src/melody.h:24:4: error: class 'Melody' does not have any field named 'notes'

    notes(nullptr) {};

    ^~~~~

C:\Users\Admin\Documents\Arduino\libraries\Melody_Player\src/melody.h: In constructor 'Melody::Melody(String, short unsigned int, int)':

C:\Users\Admin\Documents\Arduino\libraries\Melody_Player\src/melody.h:27:39: error: class 'Melody' does not have any field named 'notes'

     title(title), timeUnit(timeUnit), notes(notes), automaticSilence(automaticSilence) {};

                                       ^~~~~

C:\Users\Admin\Documents\Arduino\libraries\Melody_Player\src/melody.h:27:45: error: 'notes' was not declared in this scope

     title(title), timeUnit(timeUnit), notes(notes), automaticSilence(automaticSilence) {};

                                             ^~~~~

C:\Users\Admin\Documents\Arduino\libraries\Melody_Player\src/melody.h:27:45: note: suggested alternative: 'note_t'

     title(title), timeUnit(timeUnit), notes(notes), automaticSilence(automaticSilence) {};

                                             ^~~~~

                                             note_t

C:\Users\Admin\Documents\Arduino\libraries\Melody_Player\src/melody.h: In member function 'short unsigned int Melody::getLength() const':

C:\Users\Admin\Documents\Arduino\libraries\Melody_Player\src/melody.h:48:8: error: 'notes' was not declared in this scope

     if(notes == nullptr) return 0;

        ^~~~~

C:\Users\Admin\Documents\Arduino\libraries\Melody_Player\src/melody.h:48:8: note: suggested alternative: 'note_t'

     if(notes == nullptr) return 0;

        ^~~~~

        note_t

C:\Users\Admin\Documents\Arduino\libraries\Melody_Player\src/melody.h:49:14: error: 'notes' was not declared in this scope

     return (*notes).size();

              ^~~~~

C:\Users\Admin\Documents\Arduino\libraries\Melody_Player\src/melody.h:49:14: note: suggested alternative: 'note_t'

     return (*notes).size();

              ^~~~~

              note_t

C:\Users\Admin\Documents\Arduino\libraries\Melody_Player\src/melody.h: In member function 'NoteDuration Melody::getNote(short unsigned int) const':

C:\Users\Admin\Documents\Arduino\libraries\Melody_Player\src/melody.h:57:14: error: 'notes' was not declared in this scope

     if(i < (*notes).size()) {

              ^~~~~

C:\Users\Admin\Documents\Arduino\libraries\Melody_Player\src/melody.h:57:14: note: suggested alternative: 'note_t'

     if(i < (*notes).size()) {

              ^~~~~

              note_t

C:\Users\Admin\Documents\Arduino\libraries\Melody_Player\src/melody.h:60:14: error: 'notes' was not declared in this scope

     return (*notes).back();

              ^~~~~

C:\Users\Admin\Documents\Arduino\libraries\Melody_Player\src/melody.h:60:14: note: suggested alternative: 'note_t'

     return (*notes).back();

              ^~~~~

              note_t

C:\Users\Admin\Documents\Arduino\libraries\Melody_Player\src/melody.h: In member function 'bool Melody::isValid() const':

C:\Users\Admin\Documents\Arduino\libraries\Melody_Player\src/melody.h:71:12: error: 'notes' was not declared in this scope

     return notes != nullptr && (*notes).size() != 0;

            ^~~~~

C:\Users\Admin\Documents\Arduino\libraries\Melody_Player\src/melody.h:71:12: note: suggested alternative: 'note_t'

     return notes != nullptr && (*notes).size() != 0;

            ^~~~~

            note_t

In file included from C:\Users\Admin\Documents\Arduino\sketch_mar04a\sketch_mar04a.ino:9:

C:\Users\Admin\Documents\Arduino\libraries\Melody_Player\src/melody_factory.h: At global scope:

C:\Users\Admin\Documents\Arduino\libraries\Melody_Player\src/melody_factory.h:50:24: error: 'vector' is not a member of 'std'

   std::shared_ptr<std::vector<NoteDuration>> notes;

                        ^~~~~~

C:\Users\Admin\Documents\Arduino\libraries\Melody_Player\src/melody_factory.h:50:24: note: 'std::vector' is defined in header '<vector>'; did you forget to '#include <vector>'?

C:\Users\Admin\Documents\Arduino\libraries\Melody_Player\src/melody_factory.h:9:1:

+#include <vector>

 #endif

C:\Users\Admin\Documents\Arduino\libraries\Melody_Player\src/melody_factory.h:50:24:

   std::shared_ptr<std::vector<NoteDuration>> notes;

                        ^~~~~~

C:\Users\Admin\Documents\Arduino\libraries\Melody_Player\src/melody_factory.h:50:24: error: 'vector' is not a member of 'std'

C:\Users\Admin\Documents\Arduino\libraries\Melody_Player\src/melody_factory.h:50:24: note: 'std::vector' is defined in header '<vector>'; did you forget to '#include <vector>'?

C:\Users\Admin\Documents\Arduino\libraries\Melody_Player\src/melody_factory.h:50:31: error: template argument 1 is invalid

   std::shared_ptr<std::vector<NoteDuration>> notes;

                               ^~~~~~~~~~~~

C:\Users\Admin\Documents\Arduino\libraries\Melody_Player\src/melody_factory.h:50:43: error: expected unqualified-id before '>' token

   std::shared_ptr<std::vector<NoteDuration>> notes;

                                           ^~

exit status 1

Error compiling for board SparkFun ESP32 Thing Plus.



This report would have more information with
"Show verbose output during compilation"
option enabled in File -> Preferences.

Static tone on ESP32 when the MelodyPlayer object is instantiated

I have a static tone on ESP32, just by initiating the library:
MelodyPlayer player(BUZZER_PIN);
I found that I can get rid of this static tone by commenting digitalWrite(pin, HIGH); in the constructor and the turnOff() functions.
A fix looks like this:

`#ifdef ESP32
MelodyPlayer::MelodyPlayer(unsigned char pin, unsigned char pwmChannel):
pin(pin), pwmChannel(pwmChannel), state(State::STOP), melodyState(nullptr) {
pinMode(pin, OUTPUT);
//digitalWrite(pin, HIGH);
};
#else
MelodyPlayer::MelodyPlayer(unsigned char pin):
pin(pin), state(State::STOP), melodyState(nullptr) {
pinMode(pin, OUTPUT);
//digitalWrite(pin, HIGH);
};
#endif

void MelodyPlayer::haltPlay(){
// Stop player, but do not reset the melodyState
ticker.detach();
turnOff();
}

void MelodyPlayer::turnOn(){
#ifdef ESP32
const int resolution = 8;
// 2000 is a frequency, it will be changed at the first play
ledcSetup(pwmChannel, 2000, resolution);
ledcAttachPin(pin, pwmChannel);
ledcWrite(pwmChannel, 125);
#endif
}

void MelodyPlayer::turnOff() {
#ifdef ESP32
ledcWrite(pwmChannel, 0);
ledcDetachPin(pin);
#else
// Remember that this will set LOW output
noTone(pin);
#endif

pinMode(pin, OUTPUT);
//digitalWrite(pin, HIGH);
}
`
Is there a reason why the buzzer pin is set to HIGH?
I can send a pull request if this is a bug.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.