How to integrate WAV files for sound generation

GCC provides the objcopy tool which enables to link external binary files to a NXT binary image. nxtOSEK provides a makefile macro WAV_SOURCES to specify 8bit monoral WAV files to be linked during the build process. A linked WAV file needs to be declared as EXTERNAL_WAV_DATA(file name) and WAV file data can be accessed via

WAV_DATA_START(file name)
: Start address of a WAV data
WAV_DATA_END(file name): End address of a WAV data
WAV_DATA_SIZE(file name): Size of a WAV data

In samples\wavtest sample, an original WAV file (artificial voice of "LEGO MINDSTORMS NXT") is stored and the sample program makes the NXT speaks it!

samples\wavtest\Makefile

     # Target specific macros
     TARGET = wavtest
     TARGET_SOURCES := \
          wavtest.c
     TOPPERS_OSEK_OIL_SOURCE := ./wavtest.oil
     # WAV files to be linked
     WAV_SOURCES := \
          lego_mindstorms_nxt.wav
     BUILD_MODE = ROM_ONLY

     O_PATH ?= build
     include ../../ecrobot/lejos_osek.tmf

samples\wavtest\wavtest.c

/* wavtest.c */
#include <string.h>
#include "kernel.h"
#include "kernel_id.h"
#include "ecrobot_interface.h"

/* OSEK declarations */
DeclareTask(Task1);

/* nxtOSEK hook to be invoked from an ISR in category 2 */
void user_1ms_isr_type2(void){}

/*
* a wav file can be accessed by using following macros:
* E.g lego_mindstorms_nxt.wav
* EXTERNAL_WAV_DATA(file name without extension); <- This is external declarations
* WAV_DATA_START(file name without extension) <- start address of a wav file
* WAV_DATA_END(file name without extension) <- end address of a wav file
* WAV_DATA_SIZE(file name without extension) <- size of a wav file
*/
EXTERNAL_WAV_DATA(lego_mindstorms_nxt);

TASK(Task1)
{
  display_clear(0);
  display_goto_xy(0, 0);
  display_string("WAV TEST");
  display_goto_xy(0, 2);
  display_string("PRESS ENTR");
  display_update();

  while(1)
  {
    if (ecrobot_is_ENTER_button_pressed())
    {
      ecrobot_sound_wav(WAV_DATA_START(lego_mindstorms_nxt),
            (U32)WAV_DATA_SIZE(lego_mindstorms_nxt), -1, 70);
    }
  }

  TerminateTask();
}

samples\wavtest\wavtest.oil

#include "implementation.oil"

CPU ATMEL_AT91SAM7S256
{
  OS LEJOS_OSEK
  {
    STATUS = EXTENDED;
    STARTUPHOOK = FALSE;
    ERRORHOOK = FALSE;
    SHUTDOWNHOOK = FALSE;
    PRETASKHOOK = FALSE;
    POSTTASKHOOK = FALSE;
    USEGETSERVICEID = FALSE;
    USEPARAMETERACCESS = FALSE;
    USERESSCHEDULER = FALSE;
  };

  /* Definition of application mode */
  APPMODE appmode1{};

  /* Definition of Task1 */
  TASK Task1
  {
    AUTOSTART = TRUE
    {
      APPMODE = appmode1;
    };
    PRIORITY = 1; /* Smaller value means lower priority */
    ACTIVATION = 1;
    SCHEDULE = FULL;
    STACKSIZE = 512; /* Stack size */
  };
};

 

 

 

Back to Samples