Mise en tableau de tableaux

Bonjour,

Je tente d’utiliser la librairie PCM.h qui permet de sampler des datas placées en mémoire.

Voici mon code:

#include <PCM.h>
#include "coincoin.h"
#include "module.h"

const unsigned char* const Sounds[] PROGMEM = {coincoin, module};

void setup()
{
  startPlayback(coincoin, sizeof(coincoin));
  delay(2000);
  startPlayback(module, sizeof(module));
}

void loop()
{
}

J’ai donc créé deux fichiers wav que j’ai converti avec wav2c en fichier .h

Ils sont du genre:

const int coincoin_length = 5648;
const unsigned char coincoin[] PROGMEM =
{
	188,21,0,0,117,117,117,118,117,119,118,117,118,118,119,119,121,120,120,117,117,116,118,118,117,119,117,117,116,116,116,115,117,118,119,119,118,119,118,118,119,119,121,121,121,121,121,120,120,120,119,120,120,119,119,118,118,118,117,118,117,118,118,118,118,119,119,119,121,120,121,120,121,121,120,121,121,122,122,121,121,119,120,119,118,119,119,120,120,119,120,120,119,120,119,119,120,120,120,121,
	122,122,121,122,122,122,120,122,121,122,122,122,122,120,121,120,120,118,119,118,119,119,121,120,120,121,119,120,118,120,120,120,122,122,123,123,122,121,121,121,121,122,122,123,123,121,121,119,119,118,119,118,120,120,121,121,122,120,120,119,120,120,119,121,122,122,123,122,122,120,120,120,119,121,121,122,122,123,122,121,118,119,118,119,119,121,121,122,122,123,121,121,119,120,120,121,123,123,122,123,
	123,122,120,121,120,121,121,123,123,122,123,121,122,120,122,122,122,123,123,124,124,122,123,121,122,121,123,122,123,122,123,123,123,122,124,123,124,123,124,124,123,123,122,123,122,123,122,123,122,123,123,122,124,123,124,124,125,124,124,122,122,123,122,124,125,127,126,128,127,129,129,136,136,148,210,250,250,255,233,162,109,76,81,113,160,196,211,198,165,125,98,95,110,137,164,174,166,141,114,91,
	89}

J’arrive trés bien à les lire ainsi:

  startPlayback(coincoin, sizeof(coincoin));
  delay(2000);
  startPlayback(module, sizeof(module));

Avec le code qui suit, je voudrais définir coincoin et module dans un tableau:

const unsigned char* const Sounds[] PROGMEM = {coincoin, module};

et appler les sons ainsi:

  startPlayback(Sounds[0], sizeof(Sounds[0]));
  delay(2000);
  startPlayback(Sounds[1], sizeof(Sounds[1]));

Mais ce code ne marche pas !

Quelle est mon erreurs svp !!!

Perci par avnce de votre aide !!!

TestPCMSampling.zip (41.2 KB)

Mais ce code ne marche pas !

Quelle est mon erreurs svp !!!

Je dirais que l'erreur, tel que c'est présenté ici, c'est d'utiliser "Souns" à la place de "Sounds"...

  startPlayback(Souns[0], sizeof(Souns[0]));
  delay(2000);
  startPlayback(Souns[1], sizeof(Souns[1]));

ps: et sinon, ça ne serait pas au niveau de l'allocation mémoire ? As-tu essayé d'enlever le PROGMEM ?

Poogahzi: ps: et sinon, ça ne serait pas au niveau de l'allocation mémoire ? As-tu essayé d'enlever le PROGMEM ?

Non

Comment ça

Comment ça

avec la touche suppr...:)

http://arduino.cc/en/Reference/PROGMEM http://arduino.stackexchange.com/questions/623/why-can-i-not-use-pointers-instead-of-array-with-progmem

etc

L’utilisation de progmem n’est pas aussi triviale. C’est un autre type de variable et elle ne s’utilise pas comme les autres.

Merci pour votre aide , mais les exemples joints ne m’aident pas beaucoup , car dans mon cas la bibliothèque PCM fait cela:

/*
 * speaker_pcm
 *
 * Plays 8-bit PCM audio on pin 11 using pulse-width modulation (PWM).
 * For Arduino with Atmega168 at 16 MHz.
 *
 * Uses two timers. The first changes the sample value 8000 times a second.
 * The second holds pin 11 high for 0-255 ticks out of a 256-tick cycle,
 * depending on sample value. The second timer repeats 62500 times per second
 * (16000000 / 256), much faster than the playback rate (8000 Hz), so
 * it almost sounds halfway decent, just really quiet on a PC speaker.
 *
 * Takes over Timer 1 (16-bit) for the 8000 Hz timer. This breaks PWM
 * (analogWrite()) for Arduino pins 9 and 10. Takes Timer 2 (8-bit)
 * for the pulse width modulation, breaking PWM for pins 11 & 3.
 *
 * References:
 *     http://www.uchobby.com/index.php/2007/11/11/arduino-sound-part-1/
 *     http://www.atmel.com/dyn/resources/prod_documents/doc2542.pdf
 *     http://www.evilmadscientist.com/article.php/avrdac
 *     http://gonium.net/md/2006/12/27/i-will-think-before-i-code/
 *     http://fly.cc.fer.hr/GDM/articles/sndmus/speaker2.html
 *     http://www.gamedev.net/reference/articles/article442.asp
 *
 * Michael Smith <michael@hurts.ca>
 */

#include <stdint.h>
#include <avr/interrupt.h>
#include <avr/io.h>
#include <avr/pgmspace.h>

#define SAMPLE_RATE 8000

#if defined(ARDUINO) && ARDUINO >= 100
#include "Arduino.h"
#else
#include "WProgram.h"
#endif

#include "PCM.h"

/*
 * The audio data needs to be unsigned, 8-bit, 8000 Hz, and small enough
 * to fit in flash. 10000-13000 samples is about the limit.
 *
 * sounddata.h should look like this:
 *     const int sounddata_length=10000;
 *     const unsigned char sounddata_data[] PROGMEM = { ..... };
 *
 * You can use wav2c from GBA CSS:
 *     http://thieumsweb.free.fr/english/gbacss.html
 * Then add "PROGMEM" in the right place. I hacked it up to dump the samples
 * as unsigned rather than signed, but it shouldn't matter.
 *
 * http://musicthing.blogspot.com/2005/05/tiny-music-makers-pt-4-mac-startup.html
 * mplayer -ao pcm macstartup.mp3
 * sox audiodump.wav -v 1.32 -c 1 -r 8000 -u -1 macstartup-8000.wav
 * sox macstartup-8000.wav macstartup-cut.wav trim 0 10000s
 * wav2c macstartup-cut.wav sounddata.h sounddata
 *
 * (starfox) nb. under sox 12.18 (distributed in CentOS 5), i needed to run
 * the following command to convert my wav file to the appropriate format:
 * sox audiodump.wav -c 1 -r 8000 -u -b macstartup-8000.wav
 */

int speakerPin = 11;
unsigned char const *sounddata_data=0;
int sounddata_length=0;
volatile uint16_t sample;
byte lastSample;
volatile int playing = 0;

// This is called at 8000 Hz to load the next sample.
ISR(TIMER1_COMPA_vect) 
{
  if (sample >= sounddata_length) {
    if (sample == sounddata_length + lastSample) 
	{
      stopPlayback();
    }
    else {
      // Ramp down to zero to reduce the click at the end of playback.
      OCR2A = sounddata_length + lastSample - sample;
    }
  }
  else 
  {
    OCR2A = pgm_read_byte(&sounddata_data[sample]);
  }
  
  ++sample;
}

void startPlayback(unsigned char const *data, int length)
{
  sounddata_data = data;
  sounddata_length = length;
  
  playing = 1;
  
  pinMode(speakerPin, OUTPUT);
  
  // Set up Timer 2 to do pulse width modulation on the speaker
  // pin.
  
  // Use internal clock (datasheet p.160)
  ASSR &= ~(_BV(EXCLK) | _BV(AS2));
  
  // Set fast PWM mode  (p.157)
  TCCR2A |= _BV(WGM21) | _BV(WGM20);
  TCCR2B &= ~_BV(WGM22);
  
  // Do non-inverting PWM on pin OC2A (p.155)
  // On the Arduino this is pin 11.
  TCCR2A = (TCCR2A | _BV(COM2A1)) & ~_BV(COM2A0);
  TCCR2A &= ~(_BV(COM2B1) | _BV(COM2B0));
  
  // No prescaler (p.158)
  TCCR2B = (TCCR2B & ~(_BV(CS12) | _BV(CS11))) | _BV(CS10);
  
  // Set initial pulse width to the first sample.
  OCR2A = pgm_read_byte(&sounddata_data[0]);
  
  
  // Set up Timer 1 to send a sample every interrupt.
  
  cli();
  
  // Set CTC mode (Clear Timer on Compare Match) (p.133)
  // Have to set OCR1A *after*, otherwise it gets reset to 0!
  TCCR1B = (TCCR1B & ~_BV(WGM13)) | _BV(WGM12);
  TCCR1A = TCCR1A & ~(_BV(WGM11) | _BV(WGM10));
  
  // No prescaler (p.134)
  TCCR1B = (TCCR1B & ~(_BV(CS12) | _BV(CS11))) | _BV(CS10);
  
  // Set the compare register (OCR1A).
  // OCR1A is a 16-bit register, so we have to do this with
  // interrupts disabled to be safe.
  OCR1A = F_CPU / SAMPLE_RATE;    // 16e6 / 8000 = 2000
  
  // Enable interrupt when TCNT1 == OCR1A (p.136)
  TIMSK1 |= _BV(OCIE1A);
  
  lastSample = pgm_read_byte(&sounddata_data[sounddata_length-1]);
  sample = 0;
  sei();
}

void stopPlayback()
{
  playing = 0;
  
  // Disable playback per-sample interrupt.
  TIMSK1 &= ~_BV(OCIE1A);
  
  // Disable the per-sample timer completely.
  TCCR1B &= ~_BV(CS10);
  
  // Disable the PWM timer.
  TCCR2B &= ~_BV(CS10);
  
  digitalWrite(speakerPin, LOW);
}

int isPlaying(){
	return playing;
}

Pour rappel, mes variables sont déjà des tableaux que je voudrais appeler dans une boucle for , les un après les autres.

pierrotm777: Avec le code qui suit, je voudrais définir coincoin et module dans un tableau:

const unsigned char* const Sounds[] PROGMEM = {coincoin, module};

et appler les sons ainsi:

  startPlayback(Sounds[0], sizeof(Sounds[0]));
  delay(2000);
  startPlayback(Sounds[1], sizeof(Sounds[1]));

Mais ce code ne marche pas !

Sounds[0] est un pointeur donc sa taille n'est pas la taille de coincoin. Si tu veux avoir la taille au moment de l'exécution il faudrait avoir un tableau dans lequel la taille serait stockée ou utiliser un tableau de structure qui contiendrait le pointeur et la taille.