-
Notifications
You must be signed in to change notification settings - Fork 26
Enable speaker sounds #326
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,81 @@ | ||
| #include "speaker.h" | ||
|
|
||
| /** | ||
| * Number of semitones above C, for each note in an octave. | ||
| * | ||
| * Notes are in order: {A, B, C, D, E, F, G}. | ||
| */ | ||
| uint8_t SEMITONES[8] = {9, 11, 0, 2, 4, 5, 7}; | ||
|
|
||
| /** | ||
| * Base or reference note, used for the pitch. | ||
| */ | ||
| uint8_t BASE_NOTE = 9; | ||
| uint8_t BASE_OCTAVE = 4; | ||
| float BASE_FREQUENCY = 440.; | ||
|
|
||
| /** | ||
| * @brief Set the frequency for the speaker. | ||
| * | ||
| * Frequency is set modulating the PWM signal sent to the speaker. | ||
| * | ||
| * @param[in] hz Frequency, in Hertz. | ||
| */ | ||
| static void speaker_set_frequency(float hz) | ||
| { | ||
| uint16_t period; | ||
|
|
||
| period = (uint16_t)(SPEAKER_BASE_FREQUENCY_HZ / hz); | ||
| timer_set_period(TIM1, period); | ||
| timer_set_oc_value(TIM1, TIM_OC3, period / 2); | ||
| } | ||
|
|
||
| /** | ||
| * @brief Turn on the speaker to play the set frequency. | ||
| */ | ||
| static void speaker_on(void) | ||
| { | ||
| timer_enable_counter(TIM1); | ||
| timer_enable_oc_output(TIM1, TIM_OC3); | ||
| } | ||
|
|
||
| /** | ||
| * @brief Turn off the speaker. | ||
| */ | ||
| static void speaker_off(void) | ||
| { | ||
| timer_disable_counter(TIM1); | ||
| timer_disable_oc_output(TIM1, TIM_OC3); | ||
| } | ||
|
|
||
| /** | ||
| * @brief Play a note through the speaker. | ||
| * | ||
| * Even if this function uses the scientific notation, notes are played with | ||
| * the concert pitch (standard pitch). That means A above middle C is the | ||
| * reference note, played at 440 Hz. | ||
| * | ||
| * @param[in] note Which note to play, in scientific notation. | ||
| * @param[in] octave Which octave to play, in scientific notation. | ||
| * @param[in] accidental Number of semitones to sum to the note. | ||
| * @param[in] duration Duration of the note, in seconds. | ||
| * | ||
| * @note The speaker and emitters both share TIM1. While playing a note, the | ||
| * emitters will be completely disabled and enabled back only after finishing | ||
| * playing the note. | ||
| */ | ||
|
cua-cua marked this conversation as resolved.
|
||
| void speaker_play(char note, uint8_t octave, int8_t accidental, float duration) | ||
| { | ||
| int16_t sound; | ||
| float frequency; | ||
|
|
||
| sound = SEMITONES[note - 'A'] - BASE_NOTE + (octave - BASE_OCTAVE) * 12; | ||
| sound += accidental; | ||
| frequency = pow(2, sound / 12.) * BASE_FREQUENCY; | ||
| setup_speaker(); | ||
| speaker_set_frequency(frequency); | ||
| speaker_on(); | ||
| sleep_seconds(duration); | ||
| speaker_off(); | ||
| setup_emitters(); | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| #ifndef __SPEAKER_H | ||
| #define __SPEAKER_H | ||
|
|
||
| #include <math.h> | ||
|
|
||
| #include <libopencm3/stm32/timer.h> | ||
|
|
||
| #include "setup.h" | ||
|
|
||
| void speaker_play(char note, uint8_t octave, int8_t accidental, float duration); | ||
|
|
||
| #endif /* __SPEAKER_H */ |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.