Skip to content

Commit

Permalink
First release (#4)
Browse files Browse the repository at this point in the history
  • Loading branch information
martinkooij authored Mar 31, 2021
1 parent 7fce25a commit c779f97
Show file tree
Hide file tree
Showing 7 changed files with 202 additions and 13 deletions.
9 changes: 4 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,8 @@ The following methods on the created object are provided:
</td>
</tr>
<tr>
<td><code>play_melody(uint t, uint n, uint * pitches, uint * values)</code> </td>
<td> plays a melody of length n and tempo t. t in beats per second. "pitches" refers to an array of notes. The length of the array should be n. "values" refers to an array of equal length that indicating the note values, as taken from the common musical notation. So 4 is a quarter note (most common), 8 an eighth note, 2 a half note, etc. If the pitch is 0 (zero) it denotes a silence, so a pitch of zero with value 4 denotes a silence that is as long as a quarter note.
<td><code>play_melody(uint t, uint n, uint * melody)</code> </td>
<td> plays a melody of with n notes and tempo t. t in beats per second. "melody" refers to an array of pitches of notes,each followed by an integer representing the not value according to common musical notation. So 4 is a quarter note, 8 an eighth. Negative values indicate a dotted note (so -4 is a dotted quarter note = quarter not + half duration). The length of the array should be n*2. If the pitch is 0 (zero) it denotes a silence, so a pitch of zero with value 4 denotes a silence that is as long as a quarter note.
</td>
</tr>
</tbody>
Expand Down Expand Up @@ -118,9 +118,8 @@ When this is defined the following holds (for the upper case defined constants,

Or to play a simple melody:
````
uint melody[] = {NOTE_C4, NOTE_G3, NOTE_G3, NOTE_A3, NOTE_G3, 0, NOTE_B3, NOTE_C4};
uint values[] = {4, 8, 8, 4, 4, 4, 4, 4 };
myPlayer1.play_melody(T_LARGHETTO,8,melody,values);
uint melody[] = {NOTE_C4,4,NOTE_G3,8,NOTE_G3,8,NOTE_A3,4,NOTE_G3,4,0,4,NOTE_B3,4,NOTE_C4,4};
myPlayer1.play_melody(T_PRESTO,8,melody);
````


Expand Down
7 changes: 3 additions & 4 deletions example/tone_example.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,16 @@

int main() {

uint melody[] = {NOTE_C4, NOTE_G3, NOTE_G3, NOTE_A3, NOTE_G3, 0, NOTE_B3, NOTE_C4};
uint values[] = {4, 8, 8, 4, 4, 4, 4, 4 };

int melody[] = {NOTE_C4,4, NOTE_G3,8, NOTE_G3,8, NOTE_A3,4, NOTE_G3,4, 0,4, NOTE_B3,4, NOTE_C4,4};

Tone myPlayer(15,50,0,20,0,20,0,10);
Tone myPlayer2(14) ;
myPlayer.init(TONE_NON_BLOCKING) ;
myPlayer2.init(TONE_NON_BLOCKING) ;

while (true) {

myPlayer.play_melody(T_PRESTO,8,melody,values);
myPlayer.play_melody(T_PRESTO,8,melody);
myPlayer2.tone(NOTE_FS4, 2.0);
sleep_ms(4000);

Expand Down
16 changes: 16 additions & 0 deletions longer-melody-example/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# Generated Cmake Pico project file

cmake_minimum_required(VERSION 3.13)

include(pico_sdk_import.cmake)

project(tone_example C CXX ASM)

pico_sdk_init()

# Add executable. Default name is the project name,
add_executable(tone_example tone_example.cpp)
add_subdirectory(../pico-tone build)

pico_add_extra_outputs(tone_example)
target_link_libraries(tone_example pico_stdlib pico_tone)
62 changes: 62 additions & 0 deletions longer-melody-example/pico_sdk_import.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
# This is a copy of <PICO_SDK_PATH>/external/pico_sdk_import.cmake

# This can be dropped into an external project to help locate this SDK
# It should be include()ed prior to project()

if (DEFINED ENV{PICO_SDK_PATH} AND (NOT PICO_SDK_PATH))
set(PICO_SDK_PATH $ENV{PICO_SDK_PATH})
message("Using PICO_SDK_PATH from environment ('${PICO_SDK_PATH}')")
endif ()

if (DEFINED ENV{PICO_SDK_FETCH_FROM_GIT} AND (NOT PICO_SDK_FETCH_FROM_GIT))
set(PICO_SDK_FETCH_FROM_GIT $ENV{PICO_SDK_FETCH_FROM_GIT})
message("Using PICO_SDK_FETCH_FROM_GIT from environment ('${PICO_SDK_FETCH_FROM_GIT}')")
endif ()

if (DEFINED ENV{PICO_SDK_FETCH_FROM_GIT_PATH} AND (NOT PICO_SDK_FETCH_FROM_GIT_PATH))
set(PICO_SDK_FETCH_FROM_GIT_PATH $ENV{PICO_SDK_FETCH_FROM_GIT_PATH})
message("Using PICO_SDK_FETCH_FROM_GIT_PATH from environment ('${PICO_SDK_FETCH_FROM_GIT_PATH}')")
endif ()

set(PICO_SDK_PATH "${PICO_SDK_PATH}" CACHE PATH "Path to the Raspberry Pi Pico SDK")
set(PICO_SDK_FETCH_FROM_GIT "${PICO_SDK_FETCH_FROM_GIT}" CACHE BOOL "Set to ON to fetch copy of SDK from git if not otherwise locatable")
set(PICO_SDK_FETCH_FROM_GIT_PATH "${PICO_SDK_FETCH_FROM_GIT_PATH}" CACHE FILEPATH "location to download SDK")

if (NOT PICO_SDK_PATH)
if (PICO_SDK_FETCH_FROM_GIT)
include(FetchContent)
set(FETCHCONTENT_BASE_DIR_SAVE ${FETCHCONTENT_BASE_DIR})
if (PICO_SDK_FETCH_FROM_GIT_PATH)
get_filename_component(FETCHCONTENT_BASE_DIR "${PICO_SDK_FETCH_FROM_GIT_PATH}" REALPATH BASE_DIR "${CMAKE_SOURCE_DIR}")
endif ()
FetchContent_Declare(
pico_sdk
GIT_REPOSITORY https://github.com/raspberrypi/pico-sdk
GIT_TAG master
)
if (NOT pico_sdk)
message("Downloading Raspberry Pi Pico SDK")
FetchContent_Populate(pico_sdk)
set(PICO_SDK_PATH ${pico_sdk_SOURCE_DIR})
endif ()
set(FETCHCONTENT_BASE_DIR ${FETCHCONTENT_BASE_DIR_SAVE})
else ()
message(FATAL_ERROR
"SDK location was not specified. Please set PICO_SDK_PATH or set PICO_SDK_FETCH_FROM_GIT to on to fetch from git."
)
endif ()
endif ()

get_filename_component(PICO_SDK_PATH "${PICO_SDK_PATH}" REALPATH BASE_DIR "${CMAKE_BINARY_DIR}")
if (NOT EXISTS ${PICO_SDK_PATH})
message(FATAL_ERROR "Directory '${PICO_SDK_PATH}' not found")
endif ()

set(PICO_SDK_INIT_CMAKE_FILE ${PICO_SDK_PATH}/pico_sdk_init.cmake)
if (NOT EXISTS ${PICO_SDK_INIT_CMAKE_FILE})
message(FATAL_ERROR "Directory '${PICO_SDK_PATH}' does not appear to contain the Raspberry Pi Pico SDK")
endif ()

set(PICO_SDK_PATH ${PICO_SDK_PATH} CACHE PATH "Path to the Raspberry Pi Pico SDK" FORCE)

include(${PICO_SDK_INIT_CMAKE_FILE})
95 changes: 95 additions & 0 deletions longer-melody-example/tone_example.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@

#include "pico/stdlib.h"
#include "pico/time.h"
#include "pico_tone.hpp"


int main() {

// Thanks to Michal Choma, Slovakya

int melody[] = {
// notes of the melody followed by the duration.
// a 4 means a quarter note, 8 an eighteenth , 16 sixteenth, so on
// !!negative numbers are used to represent dotted notes,
// so -4 means a dotted quarter note, that is, a quarter plus an eighteenth!!

// Mafia theme
// Score available at https://musescore.com/user/2139361/scores/783461

NOTE_D4, 4, NOTE_F4, 4, NOTE_E4, 4, NOTE_D4, 4, //1
NOTE_D4, 4, NOTE_A3, 4, NOTE_G3, 4, NOTE_C4, 4, //2
NOTE_D4, 4, NOTE_F4, 4, NOTE_E4, 4, NOTE_G4, 4, //3
NOTE_A4, 1, //4
NOTE_A4, 4, NOTE_C5, 4, NOTE_G4, 2, //5
NOTE_G4, -4, NOTE_E4, 8, NOTE_D4, 4, NOTE_C4, 8, NOTE_E4, 16, NOTE_F4, 16, //6
NOTE_E4, 4, NOTE_C4, 4, NOTE_D4, 2, //7

NOTE_D4, -4, NOTE_C4, 8, NOTE_A3, 4, NOTE_G3, 4, //8
NOTE_A5, 4, NOTE_C6, 4, NOTE_G5, 2, //9
NOTE_G5, -4, NOTE_E5, 8, NOTE_D5, 4, NOTE_C5, 8, NOTE_E5, 16, NOTE_F5, 16, //10
NOTE_E5, 4, NOTE_C5, 4, NOTE_D5, 2, //11
NOTE_D5, -4, NOTE_C5, 8, NOTE_A4, 4, NOTE_G4, 4, //12
NOTE_D6, 2, NOTE_D6, 8, NOTE_F5, 8, NOTE_A5, 8, NOTE_D6, 8, //13

NOTE_G5, 2, NOTE_C6, 8, NOTE_AS5, 4, NOTE_A5, 16, NOTE_AS5, 16, //14
NOTE_A5, 2, NOTE_E5, 8, NOTE_F5, 8, NOTE_E5, 8, NOTE_C5, 8, //15
NOTE_D5, 2, NOTE_E5, 8, NOTE_F5, 8, NOTE_E5, 8, NOTE_C5, 8, //16
NOTE_D6, 2, NOTE_D5, 8, NOTE_F4, 8, NOTE_A4, 8, NOTE_D5, 8, //17
NOTE_E5, 8, NOTE_F5, 8, NOTE_C5, -2, //18

NOTE_D5, 8, NOTE_DS5, 8, NOTE_AS4, -4, NOTE_G4, 8, NOTE_F4, 8, NOTE_DS4, 8, //19
NOTE_AS4, 8, NOTE_B4, 8, NOTE_DS4, -2, //20
NOTE_G4, -8, NOTE_GS4, 16, NOTE_GS4, -4, NOTE_G4, -8, NOTE_GS4, 8, //21 -1/16
NOTE_AS4, -8, NOTE_B4, 16, NOTE_B4, 2, NOTE_C5, 4, //22
NOTE_G4, -8, NOTE_F4, 16, NOTE_F4, -4, NOTE_G4, -8, NOTE_GS4, 8, //23 -1/16

NOTE_E4, -2, NOTE_AS4, 8, //24 -1/8
NOTE_B4, 8, NOTE_AS4, 16, NOTE_B4, 8, NOTE_AS4, 16, NOTE_B4, 2, //25 -1/8
NOTE_AS4, -8, NOTE_B4, 2, NOTE_AS4, -8, NOTE_B4, 8, //26
NOTE_CS5, -8, NOTE_D5, 2, NOTE_DS5, 4, //27 -1/16
NOTE_AS4, -8, NOTE_GS4, 2, NOTE_AS4, -8, NOTE_B4, 8, //28
NOTE_G4, -2, NOTE_CS5, 8, //29 -1/8

NOTE_D5, 8, NOTE_CS5, 16, NOTE_D5, 8, NOTE_CS5, 16, NOTE_D5, 2, //30 -1/8
NOTE_CS5, -16, NOTE_E5, -16, NOTE_DS5, -8, NOTE_C6, 32, NOTE_A5, 32, NOTE_B5, 16, NOTE_B5, -16, NOTE_D6, -16, NOTE_CS6, 8, NOTE_A5, -8, //31
NOTE_CS5, -16, NOTE_E5, -16, NOTE_DS5, -8, NOTE_C6, 32, NOTE_A5, 32, NOTE_B5, 16, NOTE_B5, -16, NOTE_D6, -16, NOTE_CS6, 8, NOTE_A5, -8, //32

NOTE_CS5, -16, NOTE_E5, -16, NOTE_DS5, -8, NOTE_C6, 32, NOTE_A5, 32, NOTE_B5, 16, NOTE_B5, -16, NOTE_D6, -16, NOTE_CS6, 8, NOTE_A5, -8, //33
NOTE_CS5, -16, NOTE_E5, -16, NOTE_DS5, -8, NOTE_C6, 32, NOTE_A5, 32, NOTE_B5, 16, NOTE_B5, -16, NOTE_D6, -16, NOTE_CS6, 8, NOTE_A5, -8, //34

NOTE_D5, -4, NOTE_C5, 8, NOTE_D5, 8, NOTE_F4, 8, NOTE_A4, 8, NOTE_D5, 8, //35
NOTE_G4, 2, NOTE_C5, 8, NOTE_AS4, 4, NOTE_A4, 16, NOTE_AS4, 16, //36

NOTE_A4, 4, NOTE_AS4, 4, NOTE_E4, 8, NOTE_F4, 8, NOTE_E4, 8, NOTE_C4, 8, //37
NOTE_D4, 2, NOTE_E5, 8, NOTE_F5, 8, NOTE_E5, 8, NOTE_C5, 8, //38

NOTE_D5, -4, NOTE_C5, 8, NOTE_D5, 8, NOTE_F4, 8, NOTE_A4, 8, NOTE_D5, 8, //39
NOTE_E5, 8, NOTE_F5, 8, NOTE_C5, 2, //40

NOTE_D5, 8, NOTE_DS5, 8, NOTE_AS4, -4, NOTE_G4, 8, NOTE_F4, 8, NOTE_DS4, 8, //41
NOTE_AS4, 4, NOTE_B4, 4, NOTE_DS4, 2, //42
NOTE_C5, -4, NOTE_AS4, 8, NOTE_GS4, 4, NOTE_G4, 4, //43

NOTE_C5, -4, NOTE_AS4, 8, NOTE_F4, -4, NOTE_DS4, 8, //44
NOTE_AS4, -4, NOTE_GS4, 8, NOTE_G4, 4, NOTE_GS4, 4, //45
NOTE_F4, 1, //46
NOTE_C5, -4, NOTE_AS4, 8, NOTE_GS4, 4, NOTE_G4, 4, //47
NOTE_C5, -4, NOTE_AS4, 8, NOTE_F4, -4, NOTE_DS4, 8, //48
NOTE_AS4, -4, NOTE_GS4, 8, NOTE_G4, 4, NOTE_GS4, 4, //49
NOTE_F4, 1, //50
};

Tone myPlayer(15,50,0,20,0,20,0,10);
myPlayer.init(TONE_NON_BLOCKING) ;


while (true) {

myPlayer.play_melody(T_ADAGIO,sizeof(melody)/(sizeof(melody[0])*2),melody);

sleep_ms(4000);

};
return 0;
}
1 change: 1 addition & 0 deletions pico-tone/include/pico_tone.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,5 @@ class Tone {
void tone(uint frequency);
void reconfigure_harmonics(uint base,uint h2,uint h3,uint h4,uint h5,uint h6,uint h7);
void play_melody(uint tempo, uint length, uint * pitches, uint * values);
void play_melody(uint tempo, uint length, int * melody);
};
25 changes: 21 additions & 4 deletions pico-tone/pico_tone.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -239,16 +239,33 @@
reconstruct_tone(base,h2,h3,h4,h5,h6,h7);
};

void Tone::play_melody(uint tempo, uint no_notes, int * melody) {
float duration_unit = 4 * 60.0 / (float)tempo ; // tempo is beats per minute for quarter notes
float tone_duration ;
for (int i = 0; i < (no_notes*2) ; i=i+2) {
if (melody[i+1] < 0) {
tone_duration = melody[i+1]* -1.5 ;
} else {
tone_duration = melody[i+1];
};
tone(melody[i], 0.90 * duration_unit / tone_duration);
if (this->dma_chan == NOT_ASSIGNED_DMA) {
sleep_ms(1000* 0.10 * duration_unit / tone_duration);
} else {
sleep_ms(1000 * duration_unit / tone_duration);
};
};
};

//for backwards compatibility with pre-release version (not advertised).
void Tone::play_melody(uint tempo, uint length, uint * pitches, uint * values) {
float duration_unit = 4 * 60.0 / (float)tempo ; // tempo is beats per minute for quarter notes
for (int i = 0; i < length ; i++) {
tone(pitches[i], 0.85 * duration_unit / values[i]);
tone(pitches[i], 0.90 * duration_unit / values[i]);
if (this->dma_chan == NOT_ASSIGNED_DMA) {
sleep_ms(1000* 0.15 * duration_unit / values[i]);
sleep_ms(1000* 0.10 * duration_unit / values[i]);
} else {
sleep_ms(1000 * duration_unit / values[i]);
};
};
};


0 comments on commit c779f97

Please sign in to comment.