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


main.cpp

Committer:
MasaoHamanaka
Date:
2015-01-30
Revision:
0:0ff243f7fb2d

File content as of revision 0:0ff243f7fb2d:

/*******************************************************************************
* DISCLAIMER
* This software is supplied by Renesas Electronics Corporation and is only
* intended for use with Renesas products. No other uses are authorized. This
* software is owned by Renesas Electronics Corporation and is protected under
* all applicable laws, including copyright laws.
* THIS SOFTWARE IS PROVIDED "AS IS" AND RENESAS MAKES NO WARRANTIES REGARDING
* THIS SOFTWARE, WHETHER EXPRESS, IMPLIED OR STATUTORY, INCLUDING BUT NOT
* LIMITED TO WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE
* AND NON-INFRINGEMENT. ALL SUCH WARRANTIES ARE EXPRESSLY DISCLAIMED.
* TO THE MAXIMUM EXTENT PERMITTED NOT PROHIBITED BY LAW, NEITHER RENESAS
* ELECTRONICS CORPORATION NOR ANY OF ITS AFFILIATED COMPANIES SHALL BE LIABLE
* FOR ANY DIRECT, INDIRECT, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES FOR
* ANY REASON RELATED TO THIS SOFTWARE, EVEN IF RENESAS OR ITS AFFILIATES HAVE
* BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
* Renesas reserves the right, without notice, to make changes to this software
* and to discontinue the availability of this software. By using this software,
* you agree to the additional terms and conditions found by accessing the
* following link:
* http://www.renesas.com/disclaimer
* Copyright (C) 2012 - 2015 Renesas Electronics Corporation. All rights reserved.
*******************************************************************************/

/******************************************************************************
Includes   <System Includes> , "Project Includes"
******************************************************************************/
#include <stdio.h>
#include <stdint.h>
#include "mbed.h"
extern "C"{
#include    "iodefine.h"
#include    "devdrv_sdg.h"
}
#include    "score.h"
#include    "piano4int.h"


/******************************************************************************
Typedef definitions
******************************************************************************/


/******************************************************************************
Macro definitions
******************************************************************************/


/******************************************************************************
Imported global variables and functions (from other files)
******************************************************************************/


/******************************************************************************
Exported global variables and functions (to be accessed by other files)
******************************************************************************/
extern "C" {
    void Sample_OSTM0_Interrupt(void);
};

static void sound_LED (uint32_t channel, uint32_t value);


/******************************************************************************
Private global variables and functions
******************************************************************************/
Ticker      LEDTicker;
DigitalOut  LED(LED1);
DigitalOut  SoundLED(LED2);

static const int8_t score1[] = {
    SCORE1
};

static const int8_t score3[] = {
    SCORE3
};


/******************************************************************************
* Function Name: main
* Description  : Displays the sample program information on the terminal
*              : connected with the CPU board by the UART, and executes initial
*              : setting for the PORT connected with the LEDs on the board.
*              : Executes initial setting for the OSTM channel 0, and sets the
*              : timer counter so that the OSTM0 interrupt occurs every 500ms.
* Arguments    : none
* Return Value : 0
******************************************************************************/
int main(void)
{
    static const int8_t *scores[SDG_CH_TOTAL] = {
        /*DEVDRV_CH_0*/ NULL,
        /*DEVDRV_CH_1*/ score1,
        /*DEVDRV_CH_2*/ NULL,
        /*DEVDRV_CH_3*/ score3
    };
    /* ==== Setup the I/O without buffering ==== */
    printf("\nRZ/A1H CPU Board Sample Program. Ver.1.00\n");
    printf("RZ/A1H Sound Generator Sample Program. Ver.0.01\n");
    printf("Copyright (C) 2015 Renesas Electronics Corporation. All rights reserved.\n");
    printf("\n");

    LED = 0;
    LEDTicker.attach(&Sample_OSTM0_Interrupt, 0.2);

    /* ==== Receive command, activate sample software ==== */
    while (true) {
        /* start playing scores */
        piano4int_start (scores, sound_LED);

        while (piano4int_status()) {
            /* wait 10ms */
            wait_us(10.0 * 1000.0);
        }
        /* stop playing scores */
        piano4int_stop(false);
        /* wait 5 seconds */
        wait(5.0);
    }
    return 0;
}

/******************************************************************************
* Function Name: Sample_OSTM0_Interrupt
* Description  : This function is executed when the OSTM0 interrupt is received.
*              : In this sample code, the processing to blink the LEDs on the
*              : CPU board every 500ms is executed.
* Arguments    : none
* Return Value : none
******************************************************************************/
void Sample_OSTM0_Interrupt(void)
{
    /* ==== LED blink ==== */
    LED = LED ^ 1;
}

/******************************************************************************
* End of Function Sample_OSTM0_Interrupt
******************************************************************************/

/******************************************************************************
* Function Name: sound_LED
* Description  : This function is used for callback function.
* Arguments    : uint32_t channel : SDG channel (0, 1, 2 or 3)
*              : uint32_t value   : Syncronization Marker ID
* Return Value : none
******************************************************************************/
static void sound_LED (uint32_t channel, uint32_t value)
{
    /* ==== LED blink ==== */
    SoundLED = SoundLED ^ 1;
}

/* End of File */