Sound Generator Sample

Dependencies:   Sound_Generator

Fork of Sound_Generator_Sample by GR-PEACH_producer_meeting

Information

Japanese version is available in lower part of this page.
このページの後半に日本語版が用意されています.

Connection

The Connection is shown below.
Connect GR-PEACH and Amplifier with speaker as shown in figure.

/media/uploads/1050186/template_platform_images.png

Music playback function

What is this ?

This is a program to play music by using sound generator in RZ/A1H.

Music Data

Music data is described in score.h.
Music data is described by string of C language.
Range of tone is 4 octaves.
The format of music data is shown below:

  1. Tempo
  2. Tone data
  3. ‘\0’

"Tempo" specifies number of quarter notes per minute.
"Tone data" is composed of the following characters.

  • Zero or more space characters.
  • “-”,“+”, or “*” specifies octave. This is optional.
  • "A" to "G" specifies key notation, or "R" specifies rest.
  • "#" specifies sharp. This is optional.
  • 1-digit or two-digit number that indicates the note value.
  • "." specifies doted-note. This is optional.
  • "." specifies tie. This is optional.
  • "@" specifies synchronized marker. This is optional.
  • 1-digit or two-digit number following synchronized marker, indicates the marker IDs.

For example, the score is expressed as follows.
/media/uploads/RyoheiHagimoto/sample_score1.png

120c4d4e4f4g4+a4b4r4


The default setting of serial communication (baud rate etc.) in mbed is shown the following link.
Please refer to the link and change the settings of your PC terminal software.
The default value of baud rate in mbed is 9600, and this application uses baud rate 9600.
https://developer.mbed.org/teams/Renesas/wiki/GR-PEACH-Getting-Started#install-the-usb-serial-communication


接続

接続方法を以下に示します。
GR-PEACHとアンプ付きスピーカーを図のように接続してください。 /media/uploads/1050186/template_platform_images.png

楽曲再生機能

概要

RZ/A1Hに搭載されているサウンドジェネレータを使用して、簡単な音楽を再生するためのプログラムです。

楽曲データ

score.hには楽曲データが記載されています。
楽曲データは、C言語の文字列で記述します。
現在は、大よそ4オクターブが指定できます。
楽曲データのフォーマットは次のとおりです。

  1. テンポ
  2. 音符の羅列
  3. ‘\0’ (C言語の文字列の終端文字; 自動で入るものでOKです。)

テンポは、1分あたりの四分音符の数です。

音符は、次の文字で構成されます。

  • 0個以上のスペース
  • オクターブを表す“-”、“+”、“*”、又は省略
  • 音階を表す“A”~“G”、又は休符を表す“R”
  • シャープを表す“#”、又は省略
  • 長さを表す1~2桁の数字
  • 付点音符を表す“.”、又は省略
  • タイを表す“_”、又は省略
  • 同期マーカ“@”、又は省略
  • 同期マーカがある場合、同期マーカIDとなる1~2桁の数字

例えば、以下のような楽譜の場合、
/media/uploads/RyoheiHagimoto/sample_score1.png
次のように記述します。

120c4d4e4f4g4+a4b4r4


mbedのシリアル通信(ボーレート等)のデフォルト設定は以下のリンクに示しています。
リンクを参考に、お使いのPCターミナルソフトの設定を変更して下さい。
mbedでのボーレートのデフォルト値は9600で、このサンプルではボーレート9600を使います。
https://developer.mbed.org/teams/Renesas/wiki/GR-PEACH-Getting-Started#install-the-usb-serial-communication


Committer:
Osamu Nakamura
Date:
Mon Apr 24 19:05:48 2017 +0900
Revision:
3:9e8c5b0d9fba
Parent:
0:0ff243f7fb2d
Introduce mbed OS 5 instead of mbed OS 2 (Classic)

Who changed what in which revision?

UserRevisionLine numberNew contents of line
MasaoHamanaka 0:0ff243f7fb2d 1 /*******************************************************************************
MasaoHamanaka 0:0ff243f7fb2d 2 * DISCLAIMER
MasaoHamanaka 0:0ff243f7fb2d 3 * This software is supplied by Renesas Electronics Corporation and is only
MasaoHamanaka 0:0ff243f7fb2d 4 * intended for use with Renesas products. No other uses are authorized. This
MasaoHamanaka 0:0ff243f7fb2d 5 * software is owned by Renesas Electronics Corporation and is protected under
MasaoHamanaka 0:0ff243f7fb2d 6 * all applicable laws, including copyright laws.
MasaoHamanaka 0:0ff243f7fb2d 7 * THIS SOFTWARE IS PROVIDED "AS IS" AND RENESAS MAKES NO WARRANTIES REGARDING
MasaoHamanaka 0:0ff243f7fb2d 8 * THIS SOFTWARE, WHETHER EXPRESS, IMPLIED OR STATUTORY, INCLUDING BUT NOT
MasaoHamanaka 0:0ff243f7fb2d 9 * LIMITED TO WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE
MasaoHamanaka 0:0ff243f7fb2d 10 * AND NON-INFRINGEMENT. ALL SUCH WARRANTIES ARE EXPRESSLY DISCLAIMED.
MasaoHamanaka 0:0ff243f7fb2d 11 * TO THE MAXIMUM EXTENT PERMITTED NOT PROHIBITED BY LAW, NEITHER RENESAS
MasaoHamanaka 0:0ff243f7fb2d 12 * ELECTRONICS CORPORATION NOR ANY OF ITS AFFILIATED COMPANIES SHALL BE LIABLE
MasaoHamanaka 0:0ff243f7fb2d 13 * FOR ANY DIRECT, INDIRECT, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES FOR
MasaoHamanaka 0:0ff243f7fb2d 14 * ANY REASON RELATED TO THIS SOFTWARE, EVEN IF RENESAS OR ITS AFFILIATES HAVE
MasaoHamanaka 0:0ff243f7fb2d 15 * BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
MasaoHamanaka 0:0ff243f7fb2d 16 * Renesas reserves the right, without notice, to make changes to this software
MasaoHamanaka 0:0ff243f7fb2d 17 * and to discontinue the availability of this software. By using this software,
MasaoHamanaka 0:0ff243f7fb2d 18 * you agree to the additional terms and conditions found by accessing the
MasaoHamanaka 0:0ff243f7fb2d 19 * following link:
MasaoHamanaka 0:0ff243f7fb2d 20 * http://www.renesas.com/disclaimer
MasaoHamanaka 0:0ff243f7fb2d 21 * Copyright (C) 2012 - 2015 Renesas Electronics Corporation. All rights reserved.
MasaoHamanaka 0:0ff243f7fb2d 22 *******************************************************************************/
MasaoHamanaka 0:0ff243f7fb2d 23
MasaoHamanaka 0:0ff243f7fb2d 24 /******************************************************************************
MasaoHamanaka 0:0ff243f7fb2d 25 Includes <System Includes> , "Project Includes"
MasaoHamanaka 0:0ff243f7fb2d 26 ******************************************************************************/
MasaoHamanaka 0:0ff243f7fb2d 27 #include <stdio.h>
MasaoHamanaka 0:0ff243f7fb2d 28 #include <stdint.h>
MasaoHamanaka 0:0ff243f7fb2d 29 #include "mbed.h"
MasaoHamanaka 0:0ff243f7fb2d 30 extern "C"{
MasaoHamanaka 0:0ff243f7fb2d 31 #include "iodefine.h"
MasaoHamanaka 0:0ff243f7fb2d 32 #include "devdrv_sdg.h"
MasaoHamanaka 0:0ff243f7fb2d 33 }
MasaoHamanaka 0:0ff243f7fb2d 34 #include "score.h"
MasaoHamanaka 0:0ff243f7fb2d 35 #include "piano4int.h"
MasaoHamanaka 0:0ff243f7fb2d 36
MasaoHamanaka 0:0ff243f7fb2d 37
MasaoHamanaka 0:0ff243f7fb2d 38 /******************************************************************************
MasaoHamanaka 0:0ff243f7fb2d 39 Typedef definitions
MasaoHamanaka 0:0ff243f7fb2d 40 ******************************************************************************/
MasaoHamanaka 0:0ff243f7fb2d 41
MasaoHamanaka 0:0ff243f7fb2d 42
MasaoHamanaka 0:0ff243f7fb2d 43 /******************************************************************************
MasaoHamanaka 0:0ff243f7fb2d 44 Macro definitions
MasaoHamanaka 0:0ff243f7fb2d 45 ******************************************************************************/
MasaoHamanaka 0:0ff243f7fb2d 46
MasaoHamanaka 0:0ff243f7fb2d 47
MasaoHamanaka 0:0ff243f7fb2d 48 /******************************************************************************
MasaoHamanaka 0:0ff243f7fb2d 49 Imported global variables and functions (from other files)
MasaoHamanaka 0:0ff243f7fb2d 50 ******************************************************************************/
MasaoHamanaka 0:0ff243f7fb2d 51
MasaoHamanaka 0:0ff243f7fb2d 52
MasaoHamanaka 0:0ff243f7fb2d 53 /******************************************************************************
MasaoHamanaka 0:0ff243f7fb2d 54 Exported global variables and functions (to be accessed by other files)
MasaoHamanaka 0:0ff243f7fb2d 55 ******************************************************************************/
MasaoHamanaka 0:0ff243f7fb2d 56 extern "C" {
MasaoHamanaka 0:0ff243f7fb2d 57 void Sample_OSTM0_Interrupt(void);
MasaoHamanaka 0:0ff243f7fb2d 58 };
MasaoHamanaka 0:0ff243f7fb2d 59
MasaoHamanaka 0:0ff243f7fb2d 60 static void sound_LED (uint32_t channel, uint32_t value);
MasaoHamanaka 0:0ff243f7fb2d 61
MasaoHamanaka 0:0ff243f7fb2d 62
MasaoHamanaka 0:0ff243f7fb2d 63 /******************************************************************************
MasaoHamanaka 0:0ff243f7fb2d 64 Private global variables and functions
MasaoHamanaka 0:0ff243f7fb2d 65 ******************************************************************************/
MasaoHamanaka 0:0ff243f7fb2d 66 Ticker LEDTicker;
MasaoHamanaka 0:0ff243f7fb2d 67 DigitalOut LED(LED1);
MasaoHamanaka 0:0ff243f7fb2d 68 DigitalOut SoundLED(LED2);
MasaoHamanaka 0:0ff243f7fb2d 69
MasaoHamanaka 0:0ff243f7fb2d 70 static const int8_t score1[] = {
MasaoHamanaka 0:0ff243f7fb2d 71 SCORE1
MasaoHamanaka 0:0ff243f7fb2d 72 };
MasaoHamanaka 0:0ff243f7fb2d 73
MasaoHamanaka 0:0ff243f7fb2d 74 static const int8_t score3[] = {
MasaoHamanaka 0:0ff243f7fb2d 75 SCORE3
MasaoHamanaka 0:0ff243f7fb2d 76 };
MasaoHamanaka 0:0ff243f7fb2d 77
MasaoHamanaka 0:0ff243f7fb2d 78
MasaoHamanaka 0:0ff243f7fb2d 79 /******************************************************************************
MasaoHamanaka 0:0ff243f7fb2d 80 * Function Name: main
MasaoHamanaka 0:0ff243f7fb2d 81 * Description : Displays the sample program information on the terminal
MasaoHamanaka 0:0ff243f7fb2d 82 * : connected with the CPU board by the UART, and executes initial
MasaoHamanaka 0:0ff243f7fb2d 83 * : setting for the PORT connected with the LEDs on the board.
MasaoHamanaka 0:0ff243f7fb2d 84 * : Executes initial setting for the OSTM channel 0, and sets the
MasaoHamanaka 0:0ff243f7fb2d 85 * : timer counter so that the OSTM0 interrupt occurs every 500ms.
MasaoHamanaka 0:0ff243f7fb2d 86 * Arguments : none
MasaoHamanaka 0:0ff243f7fb2d 87 * Return Value : 0
MasaoHamanaka 0:0ff243f7fb2d 88 ******************************************************************************/
MasaoHamanaka 0:0ff243f7fb2d 89 int main(void)
MasaoHamanaka 0:0ff243f7fb2d 90 {
MasaoHamanaka 0:0ff243f7fb2d 91 static const int8_t *scores[SDG_CH_TOTAL] = {
MasaoHamanaka 0:0ff243f7fb2d 92 /*DEVDRV_CH_0*/ NULL,
MasaoHamanaka 0:0ff243f7fb2d 93 /*DEVDRV_CH_1*/ score1,
MasaoHamanaka 0:0ff243f7fb2d 94 /*DEVDRV_CH_2*/ NULL,
MasaoHamanaka 0:0ff243f7fb2d 95 /*DEVDRV_CH_3*/ score3
MasaoHamanaka 0:0ff243f7fb2d 96 };
MasaoHamanaka 0:0ff243f7fb2d 97 /* ==== Setup the I/O without buffering ==== */
MasaoHamanaka 0:0ff243f7fb2d 98 printf("\nRZ/A1H CPU Board Sample Program. Ver.1.00\n");
MasaoHamanaka 0:0ff243f7fb2d 99 printf("RZ/A1H Sound Generator Sample Program. Ver.0.01\n");
MasaoHamanaka 0:0ff243f7fb2d 100 printf("Copyright (C) 2015 Renesas Electronics Corporation. All rights reserved.\n");
MasaoHamanaka 0:0ff243f7fb2d 101 printf("\n");
MasaoHamanaka 0:0ff243f7fb2d 102
MasaoHamanaka 0:0ff243f7fb2d 103 LED = 0;
MasaoHamanaka 0:0ff243f7fb2d 104 LEDTicker.attach(&Sample_OSTM0_Interrupt, 0.2);
MasaoHamanaka 0:0ff243f7fb2d 105
MasaoHamanaka 0:0ff243f7fb2d 106 /* ==== Receive command, activate sample software ==== */
MasaoHamanaka 0:0ff243f7fb2d 107 while (true) {
MasaoHamanaka 0:0ff243f7fb2d 108 /* start playing scores */
MasaoHamanaka 0:0ff243f7fb2d 109 piano4int_start (scores, sound_LED);
MasaoHamanaka 0:0ff243f7fb2d 110
MasaoHamanaka 0:0ff243f7fb2d 111 while (piano4int_status()) {
MasaoHamanaka 0:0ff243f7fb2d 112 /* wait 10ms */
MasaoHamanaka 0:0ff243f7fb2d 113 wait_us(10.0 * 1000.0);
MasaoHamanaka 0:0ff243f7fb2d 114 }
MasaoHamanaka 0:0ff243f7fb2d 115 /* stop playing scores */
MasaoHamanaka 0:0ff243f7fb2d 116 piano4int_stop(false);
MasaoHamanaka 0:0ff243f7fb2d 117 /* wait 5 seconds */
MasaoHamanaka 0:0ff243f7fb2d 118 wait(5.0);
MasaoHamanaka 0:0ff243f7fb2d 119 }
MasaoHamanaka 0:0ff243f7fb2d 120 return 0;
MasaoHamanaka 0:0ff243f7fb2d 121 }
MasaoHamanaka 0:0ff243f7fb2d 122
MasaoHamanaka 0:0ff243f7fb2d 123 /******************************************************************************
MasaoHamanaka 0:0ff243f7fb2d 124 * Function Name: Sample_OSTM0_Interrupt
MasaoHamanaka 0:0ff243f7fb2d 125 * Description : This function is executed when the OSTM0 interrupt is received.
MasaoHamanaka 0:0ff243f7fb2d 126 * : In this sample code, the processing to blink the LEDs on the
MasaoHamanaka 0:0ff243f7fb2d 127 * : CPU board every 500ms is executed.
MasaoHamanaka 0:0ff243f7fb2d 128 * Arguments : none
MasaoHamanaka 0:0ff243f7fb2d 129 * Return Value : none
MasaoHamanaka 0:0ff243f7fb2d 130 ******************************************************************************/
MasaoHamanaka 0:0ff243f7fb2d 131 void Sample_OSTM0_Interrupt(void)
MasaoHamanaka 0:0ff243f7fb2d 132 {
MasaoHamanaka 0:0ff243f7fb2d 133 /* ==== LED blink ==== */
MasaoHamanaka 0:0ff243f7fb2d 134 LED = LED ^ 1;
MasaoHamanaka 0:0ff243f7fb2d 135 }
MasaoHamanaka 0:0ff243f7fb2d 136
MasaoHamanaka 0:0ff243f7fb2d 137 /******************************************************************************
MasaoHamanaka 0:0ff243f7fb2d 138 * End of Function Sample_OSTM0_Interrupt
MasaoHamanaka 0:0ff243f7fb2d 139 ******************************************************************************/
MasaoHamanaka 0:0ff243f7fb2d 140
MasaoHamanaka 0:0ff243f7fb2d 141 /******************************************************************************
MasaoHamanaka 0:0ff243f7fb2d 142 * Function Name: sound_LED
MasaoHamanaka 0:0ff243f7fb2d 143 * Description : This function is used for callback function.
MasaoHamanaka 0:0ff243f7fb2d 144 * Arguments : uint32_t channel : SDG channel (0, 1, 2 or 3)
MasaoHamanaka 0:0ff243f7fb2d 145 * : uint32_t value : Syncronization Marker ID
MasaoHamanaka 0:0ff243f7fb2d 146 * Return Value : none
MasaoHamanaka 0:0ff243f7fb2d 147 ******************************************************************************/
MasaoHamanaka 0:0ff243f7fb2d 148 static void sound_LED (uint32_t channel, uint32_t value)
MasaoHamanaka 0:0ff243f7fb2d 149 {
MasaoHamanaka 0:0ff243f7fb2d 150 /* ==== LED blink ==== */
MasaoHamanaka 0:0ff243f7fb2d 151 SoundLED = SoundLED ^ 1;
MasaoHamanaka 0:0ff243f7fb2d 152 }
MasaoHamanaka 0:0ff243f7fb2d 153
MasaoHamanaka 0:0ff243f7fb2d 154 /* End of File */