![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
Simple starter skeleton for asteroids video game.
Sounds/Speaker.h@3:98aa3db6a48f, 2021-04-01 (annotated)
- Committer:
- jhurley31
- Date:
- Thu Apr 01 19:10:58 2021 +0000
- Revision:
- 3:98aa3db6a48f
- Parent:
- 2:30020ddfccf6
Initial commit.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
jhurley31 | 0:0c450cb95a1e | 1 | #include "mbed.h" |
jhurley31 | 3:98aa3db6a48f | 2 | #include "Fire.h" |
jhurley31 | 3:98aa3db6a48f | 3 | #include "beats1.h" |
jhurley31 | 3:98aa3db6a48f | 4 | #include "beats2.h" |
jhurley31 | 3:98aa3db6a48f | 5 | #include "bangsmall.h" |
jhurley31 | 3:98aa3db6a48f | 6 | #include "bangmedium.h" |
jhurley31 | 3:98aa3db6a48f | 7 | #include "banglarge.h" |
jhurley31 | 1:a6872783beca | 8 | |
jhurley31 | 3:98aa3db6a48f | 9 | #pragma once |
jhurley31 | 1:a6872783beca | 10 | |
jhurley31 | 1:a6872783beca | 11 | class Speaker : public PwmOut |
jhurley31 | 0:0c450cb95a1e | 12 | { |
jhurley31 | 0:0c450cb95a1e | 13 | public: |
jhurley31 | 3:98aa3db6a48f | 14 | enum ASTERIODS_SOUNDS {NO_SOUND, BEATS1, BEATS2, FIRE, BANG_SMALL, BANG_MEDIUM, BANG_LARGE}; |
jhurley31 | 1:a6872783beca | 15 | |
jhurley31 | 1:a6872783beca | 16 | Speaker(PinName nPin):PwmOut(nPin) |
jhurley31 | 1:a6872783beca | 17 | { |
jhurley31 | 1:a6872783beca | 18 | m_ulAudioArrayIndex = 0; |
jhurley31 | 2:30020ddfccf6 | 19 | SwitchSound(NO_SOUND); |
jhurley31 | 1:a6872783beca | 20 | } |
jhurley31 | 1:a6872783beca | 21 | |
jhurley31 | 1:a6872783beca | 22 | void PlayNextValue() |
jhurley31 | 1:a6872783beca | 23 | { |
jhurley31 | 3:98aa3db6a48f | 24 | if (m_pAudioArray != NULL) |
jhurley31 | 1:a6872783beca | 25 | { |
jhurley31 | 3:98aa3db6a48f | 26 | write((float)m_pAudioArray[ GetNextValue() ]/255.0f); |
jhurley31 | 1:a6872783beca | 27 | } |
jhurley31 | 0:0c450cb95a1e | 28 | } |
jhurley31 | 1:a6872783beca | 29 | |
jhurley31 | 3:98aa3db6a48f | 30 | void SwitchSound ( const ASTERIODS_SOUNDS &newSound) |
jhurley31 | 1:a6872783beca | 31 | { |
jhurley31 | 2:30020ddfccf6 | 32 | if (newSound == m_enActiveAudioArray) |
jhurley31 | 2:30020ddfccf6 | 33 | { |
jhurley31 | 2:30020ddfccf6 | 34 | return; |
jhurley31 | 2:30020ddfccf6 | 35 | } |
jhurley31 | 2:30020ddfccf6 | 36 | |
jhurley31 | 2:30020ddfccf6 | 37 | m_ulAudioArrayIndex = 0; |
jhurley31 | 2:30020ddfccf6 | 38 | m_enActiveAudioArray = newSound; |
jhurley31 | 1:a6872783beca | 39 | switch (newSound) |
jhurley31 | 1:a6872783beca | 40 | { |
jhurley31 | 2:30020ddfccf6 | 41 | case NO_SOUND: |
jhurley31 | 3:98aa3db6a48f | 42 | m_pAudioArray = NULL; |
jhurley31 | 3:98aa3db6a48f | 43 | break; |
jhurley31 | 3:98aa3db6a48f | 44 | case BEATS1: |
jhurley31 | 3:98aa3db6a48f | 45 | m_pAudioArray = &gBeats1Data[0]; |
jhurley31 | 2:30020ddfccf6 | 46 | break; |
jhurley31 | 3:98aa3db6a48f | 47 | case BEATS2: |
jhurley31 | 3:98aa3db6a48f | 48 | m_pAudioArray = &gBeats2Data[0]; |
jhurley31 | 1:a6872783beca | 49 | break; |
jhurley31 | 3:98aa3db6a48f | 50 | case FIRE: |
jhurley31 | 3:98aa3db6a48f | 51 | m_pAudioArray = &gFireData[0]; |
jhurley31 | 1:a6872783beca | 52 | break; |
jhurley31 | 3:98aa3db6a48f | 53 | case BANG_SMALL: |
jhurley31 | 3:98aa3db6a48f | 54 | m_pAudioArray = &gBangSmallData[0]; |
jhurley31 | 1:a6872783beca | 55 | break; |
jhurley31 | 3:98aa3db6a48f | 56 | case BANG_MEDIUM: |
jhurley31 | 3:98aa3db6a48f | 57 | m_pAudioArray = &gBangMediumData[0]; |
jhurley31 | 1:a6872783beca | 58 | break; |
jhurley31 | 3:98aa3db6a48f | 59 | case BANG_LARGE: |
jhurley31 | 3:98aa3db6a48f | 60 | m_pAudioArray = &gBangLargeData[0]; |
jhurley31 | 1:a6872783beca | 61 | break; |
jhurley31 | 1:a6872783beca | 62 | default: |
jhurley31 | 3:98aa3db6a48f | 63 | m_pAudioArray = NULL; |
jhurley31 | 1:a6872783beca | 64 | break; |
jhurley31 | 1:a6872783beca | 65 | } |
jhurley31 | 1:a6872783beca | 66 | |
jhurley31 | 0:0c450cb95a1e | 67 | } |
jhurley31 | 1:a6872783beca | 68 | |
jhurley31 | 1:a6872783beca | 69 | unsigned int GetNextValue() |
jhurley31 | 1:a6872783beca | 70 | { |
jhurley31 | 1:a6872783beca | 71 | m_ulAudioArrayIndex++; |
jhurley31 | 1:a6872783beca | 72 | switch (m_enActiveAudioArray) |
jhurley31 | 1:a6872783beca | 73 | { |
jhurley31 | 3:98aa3db6a48f | 74 | case BEATS1: |
jhurley31 | 3:98aa3db6a48f | 75 | m_ulAudioArrayIndex %= NUM_BEATS1_ELEMENTS; |
jhurley31 | 3:98aa3db6a48f | 76 | if (m_ulAudioArrayIndex == 0) |
jhurley31 | 3:98aa3db6a48f | 77 | { |
jhurley31 | 3:98aa3db6a48f | 78 | SwitchSound(BEATS2); |
jhurley31 | 3:98aa3db6a48f | 79 | } |
jhurley31 | 3:98aa3db6a48f | 80 | break; |
jhurley31 | 3:98aa3db6a48f | 81 | case BEATS2: |
jhurley31 | 3:98aa3db6a48f | 82 | m_ulAudioArrayIndex %= NUM_BEATS2_ELEMENTS; |
jhurley31 | 3:98aa3db6a48f | 83 | if (m_ulAudioArrayIndex == 0) |
jhurley31 | 3:98aa3db6a48f | 84 | { |
jhurley31 | 3:98aa3db6a48f | 85 | SwitchSound(BEATS1); |
jhurley31 | 3:98aa3db6a48f | 86 | } |
jhurley31 | 3:98aa3db6a48f | 87 | break; |
jhurley31 | 3:98aa3db6a48f | 88 | case FIRE: |
jhurley31 | 3:98aa3db6a48f | 89 | m_ulAudioArrayIndex %= NUM_FIRE_ELEMENTS; |
jhurley31 | 2:30020ddfccf6 | 90 | if (m_ulAudioArrayIndex == 0) |
jhurley31 | 2:30020ddfccf6 | 91 | { |
jhurley31 | 3:98aa3db6a48f | 92 | SwitchSound(BEATS1); |
jhurley31 | 3:98aa3db6a48f | 93 | } |
jhurley31 | 3:98aa3db6a48f | 94 | break; |
jhurley31 | 3:98aa3db6a48f | 95 | case BANG_SMALL: |
jhurley31 | 3:98aa3db6a48f | 96 | m_ulAudioArrayIndex %= NUM_BANG_SMALL_ELEMENTS; |
jhurley31 | 3:98aa3db6a48f | 97 | if (m_ulAudioArrayIndex == 0) |
jhurley31 | 3:98aa3db6a48f | 98 | { |
jhurley31 | 3:98aa3db6a48f | 99 | SwitchSound(BEATS1); |
jhurley31 | 3:98aa3db6a48f | 100 | } |
jhurley31 | 3:98aa3db6a48f | 101 | break; |
jhurley31 | 3:98aa3db6a48f | 102 | case BANG_MEDIUM: |
jhurley31 | 3:98aa3db6a48f | 103 | m_ulAudioArrayIndex %= NUM_BANG_MEDIUM_ELEMENTS; |
jhurley31 | 3:98aa3db6a48f | 104 | if (m_ulAudioArrayIndex == 0) |
jhurley31 | 3:98aa3db6a48f | 105 | { |
jhurley31 | 3:98aa3db6a48f | 106 | SwitchSound(BEATS1); |
jhurley31 | 2:30020ddfccf6 | 107 | } |
jhurley31 | 1:a6872783beca | 108 | break; |
jhurley31 | 3:98aa3db6a48f | 109 | case BANG_LARGE: |
jhurley31 | 3:98aa3db6a48f | 110 | m_ulAudioArrayIndex %= NUM_BANG_LARGE_ELEMENTS; |
jhurley31 | 3:98aa3db6a48f | 111 | if (m_ulAudioArrayIndex == 0) |
jhurley31 | 3:98aa3db6a48f | 112 | { |
jhurley31 | 3:98aa3db6a48f | 113 | SwitchSound(BEATS1); |
jhurley31 | 3:98aa3db6a48f | 114 | } |
jhurley31 | 3:98aa3db6a48f | 115 | break; |
jhurley31 | 1:a6872783beca | 116 | default: |
jhurley31 | 3:98aa3db6a48f | 117 | m_enActiveAudioArray = BEATS1; |
jhurley31 | 3:98aa3db6a48f | 118 | m_ulAudioArrayIndex %= NUM_BEATS1_ELEMENTS; |
jhurley31 | 1:a6872783beca | 119 | break; |
jhurley31 | 1:a6872783beca | 120 | } |
jhurley31 | 1:a6872783beca | 121 | return m_ulAudioArrayIndex; |
jhurley31 | 1:a6872783beca | 122 | } |
jhurley31 | 0:0c450cb95a1e | 123 | private: |
jhurley31 | 1:a6872783beca | 124 | unsigned long m_ulAudioArrayIndex; |
jhurley31 | 3:98aa3db6a48f | 125 | ASTERIODS_SOUNDS m_enActiveAudioArray; |
jhurley31 | 3:98aa3db6a48f | 126 | const unsigned char *m_pAudioArray; |
jhurley31 | 1:a6872783beca | 127 | |
jhurley31 | 0:0c450cb95a1e | 128 | }; |
jhurley31 | 1:a6872783beca | 129 | |
jhurley31 | 3:98aa3db6a48f | 130 |