Music notes coding for a non musician

Hi

Im working on a project that i will need a few lines that will create a few bars of a tune to be output from my nano, the bit im having dificulty with is converting the music (sheet form) into letters ie. C#, B, Aflat etc to insert in my line . If anyone has a link to a site that might help that would be a start.

Many thanks
Only just starting out on this journey learning C++
Old dog learning new tricks .......late

https://docs.arduino.cc/built-in-examples/digital/toneMelody/

https://docs.arduino.cc/learn/programming/audio/

The question is unclear: are you looking for an "Optical Music Recognition software" ?

I believe he wants to learn how to read the musical staff.
This may help getting you started.

This attempts to explain sharps and flats

1 Like

2112 is quite correct, i am looking to put just a few bars of a tune into the startup section of a sketch i am trying to produce, i have found a file i need namely " pitches.h" so im half way there its just extracting the appropriate notes from the sheet music for the tune and converting into the letters/notelength to add to the coding. Two factors here 1: i am not a musician and 2: ive only just started to attempt to learn C++ coding so be gentle with me ....lol

Thanks

Thanks , ill have to sit down and do it " old school " with a pad and pen and transcribe the notes and lengths by hand . I had wondered if there was a site or app that might have saved me time. Thanks for the links they will be handy , i was just about to "google" the same key words , YouTube is always your best friend...eh ? Many thanks .

I will add that this is a new venture for me at 66 starting to learn to programme .......why ? Because a: its out there and b: because i can lol
Ive always been the same , wont let anything beat me .....lol

1 Like

If you find a MIDI version of your song (which one?), it might be easier to extract the information than trying to decipher a music score if you can't read music.

keep all of them, the unused ones won't take up any memory (they are just #define)

keep this file pitches.h (source)

/*************************************************

 * Public Constants

 *************************************************/

#define NOTE_B0  31
#define NOTE_C1  33
#define NOTE_CS1 35
#define NOTE_D1  37
#define NOTE_DS1 39
#define NOTE_E1  41
#define NOTE_F1  44
#define NOTE_FS1 46
#define NOTE_G1  49
#define NOTE_GS1 52
#define NOTE_A1  55
#define NOTE_AS1 58
#define NOTE_B1  62
#define NOTE_C2  65
#define NOTE_CS2 69
#define NOTE_D2  73
#define NOTE_DS2 78
#define NOTE_E2  82
#define NOTE_F2  87
#define NOTE_FS2 93
#define NOTE_G2  98
#define NOTE_GS2 104
#define NOTE_A2  110
#define NOTE_AS2 117
#define NOTE_B2  123
#define NOTE_C3  131
#define NOTE_CS3 139
#define NOTE_D3  147
#define NOTE_DS3 156
#define NOTE_E3  165
#define NOTE_F3  175
#define NOTE_FS3 185
#define NOTE_G3  196
#define NOTE_GS3 208
#define NOTE_A3  220
#define NOTE_AS3 233
#define NOTE_B3  247
#define NOTE_C4  262
#define NOTE_CS4 277
#define NOTE_D4  294
#define NOTE_DS4 311
#define NOTE_E4  330
#define NOTE_F4  349
#define NOTE_FS4 370
#define NOTE_G4  392
#define NOTE_GS4 415
#define NOTE_A4  440
#define NOTE_AS4 466
#define NOTE_B4  494
#define NOTE_C5  523
#define NOTE_CS5 554
#define NOTE_D5  587
#define NOTE_DS5 622
#define NOTE_E5  659
#define NOTE_F5  698
#define NOTE_FS5 740
#define NOTE_G5  784
#define NOTE_GS5 831
#define NOTE_A5  880
#define NOTE_AS5 932
#define NOTE_B5  988
#define NOTE_C6  1047
#define NOTE_CS6 1109
#define NOTE_D6  1175
#define NOTE_DS6 1245
#define NOTE_E6  1319
#define NOTE_F6  1397
#define NOTE_FS6 1480
#define NOTE_G6  1568
#define NOTE_GS6 1661
#define NOTE_A6  1760
#define NOTE_AS6 1865
#define NOTE_B6  1976
#define NOTE_C7  2093
#define NOTE_CS7 2217
#define NOTE_D7  2349
#define NOTE_DS7 2489
#define NOTE_E7  2637
#define NOTE_F7  2794
#define NOTE_FS7 2960
#define NOTE_G7  3136
#define NOTE_GS7 3322
#define NOTE_A7  3520
#define NOTE_AS7 3729
#define NOTE_B7  3951
#define NOTE_C8  4186
#define NOTE_CS8 4435
#define NOTE_D8  4699
#define NOTE_DS8 4978
1 Like

Hi i will look , i already have "pitches.h" i found that , will look for the midi file , the song im looking to replicate is just the main chorus of " Rule Britannia " i will change a few notes to avoid copyright obviously but retain the theme.

Thanks

This is unnecessary since the song is from 1740 and thus in the public domain.

2 Likes

Oh Ok thanks

Every day is a school day

"Friends don't let friends skip school day."
"Every day is school day."

Words to live by.

1 Like

I gave it a quick try

The melody is probably far from being correct but may be it does the job?

click to see the code
/* ============================================
  code is placed under the MIT license
  Copyright (c) 2024 J-M-L
  For the Arduino Forum : https://forum.arduino.cc/u/j-m-l

  Pitches from https://github.com/arduino/arduino-examples/blob/main/examples/02.Digital/toneMelody/pitches.h$0

  Permission is hereby granted, free of charge, to any person obtaining a copy
  of this software and associated documentation files (the "Software"), to deal
  in the Software without restriction, including without limitation the rights
  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  copies of the Software, and to permit persons to whom the Software is
  furnished to do so, subject to the following conditions:

  The above copyright notice and this permission notice shall be included in
  all copies or substantial portions of the Software.

  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  THE SOFTWARE.
  ===============================================
*/


#include "pitches.h"
const byte buzzerPin = 8;

struct Note {
  uint16_t pitch;
  uint8_t duration;
};

Note melody[] = {
  // Main theme
  {NOTE_D4, 4}, {NOTE_D4, 4}, {NOTE_G4, 4}, {NOTE_FS4, 8}, {NOTE_G4, 8},
  {NOTE_A4, 4}, {NOTE_G4, 4}, {NOTE_FS4, 4}, {NOTE_E4, 8}, {NOTE_D4, 8},
  {NOTE_E4, 4}, {NOTE_FS4, 4}, {NOTE_G4, 4}, {NOTE_E4, 4}, {NOTE_D4, 2},

  // Second phrase
  {NOTE_A4, 4}, {NOTE_G4, 8}, {NOTE_FS4, 8}, {NOTE_G4, 8}, {NOTE_FS4, 8},
  {NOTE_E4, 4}, {NOTE_FS4, 4}, {NOTE_G4, 4}, {NOTE_FS4, 8}, {NOTE_E4, 8},
  {NOTE_D4, 4}, {NOTE_E4, 4}, {NOTE_FS4, 4}, {NOTE_D4, 2},

  // Main theme reprise
  {NOTE_D4, 4}, {NOTE_D4, 4}, {NOTE_G4, 4}, {NOTE_FS4, 8}, {NOTE_G4, 8},
  {NOTE_A4, 4}, {NOTE_G4, 4}, {NOTE_FS4, 4}, {NOTE_E4, 8}, {NOTE_D4, 8},
  {NOTE_E4, 4}, {NOTE_FS4, 4}, {NOTE_G4, 4}, {NOTE_E4, 4}, {NOTE_D4, 2},

  // Final phrase
  {NOTE_A4, 4}, {NOTE_G4, 8}, {NOTE_FS4, 8}, {NOTE_G4, 8}, {NOTE_FS4, 8},
  {NOTE_E4, 4}, {NOTE_FS4, 4}, {NOTE_G4, 4}, {NOTE_FS4, 8}, {NOTE_E4, 8},
  {NOTE_D4, 4}, {NOTE_E4, 4}, {NOTE_FS4, 4}, {NOTE_D4, 2}
};



void setup() {
  pinMode(buzzerPin, OUTPUT);
}

void loop() {
  for (auto &aNote : melody) {
    uint16_t noteDuration = 1000u / aNote.duration;
    tone(buzzerPin, aNote.pitch, noteDuration);
    delay(noteDuration * 1.30);
  }
  noTone(buzzerPin);
  delay(2000);
}

pitches.h

/*************************************************
   Public Constants
 *************************************************/

#define NOTE_B0 31
#define NOTE_C1 33
#define NOTE_CS1 35
#define NOTE_D1 37
#define NOTE_DS1 39
#define NOTE_E1 41
#define NOTE_F1 44
#define NOTE_FS1 46
#define NOTE_G1 49
#define NOTE_GS1 52
#define NOTE_A1 55
#define NOTE_AS1 58
#define NOTE_B1 62
#define NOTE_C2 65
#define NOTE_CS2 69
#define NOTE_D2 73
#define NOTE_DS2 78
#define NOTE_E2 82
#define NOTE_F2 87
#define NOTE_FS2 93
#define NOTE_G2 98
#define NOTE_GS2 104
#define NOTE_A2 110
#define NOTE_AS2 117
#define NOTE_B2 123
#define NOTE_C3 131
#define NOTE_CS3 139
#define NOTE_D3 147
#define NOTE_DS3 156
#define NOTE_E3 165
#define NOTE_F3 175
#define NOTE_FS3 185
#define NOTE_G3 196
#define NOTE_GS3 208
#define NOTE_A3 220
#define NOTE_AS3 233
#define NOTE_B3 247
#define NOTE_C4 262
#define NOTE_CS4 277
#define NOTE_D4 294
#define NOTE_DS4 311
#define NOTE_E4 330
#define NOTE_F4 349
#define NOTE_FS4 370
#define NOTE_G4 392
#define NOTE_GS4 415
#define NOTE_A4 440
#define NOTE_AS4 466
#define NOTE_B4 494
#define NOTE_C5 523
#define NOTE_CS5 554
#define NOTE_D5 587
#define NOTE_DS5 622
#define NOTE_E5 659
#define NOTE_F5 698
#define NOTE_FS5 740
#define NOTE_G5 784
#define NOTE_GS5 831
#define NOTE_A5 880
#define NOTE_AS5 932
#define NOTE_B5 988
#define NOTE_C6 1047
#define NOTE_CS6 1109
#define NOTE_D6 1175
#define NOTE_DS6 1245
#define NOTE_E6 1319
#define NOTE_F6 1397
#define NOTE_FS6 1480
#define NOTE_G6 1568
#define NOTE_GS6 1661
#define NOTE_A6 1760
#define NOTE_AS6 1865
#define NOTE_B6 1976
#define NOTE_C7 2093
#define NOTE_CS7 2217
#define NOTE_D7 2349
#define NOTE_DS7 2489
#define NOTE_E7 2637
#define NOTE_F7 2794
#define NOTE_FS7 2960
#define NOTE_G7 3136
#define NOTE_GS7 3322
#define NOTE_A7 3520
#define NOTE_AS7 3729
#define NOTE_B7 3951
#define NOTE_C8 4186
#define NOTE_CS8 4435
#define NOTE_D8 4699
#define NOTE_DS8 4978

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.