Bonjour,

je veux créer un générateur de sons pour mon jeu vidéo. Je veux que ce générateur produisent divers cris d'oiseaux et d'autres sons d'ambiance. Les générer me permettra d'économiser des ressources mémoire.

J'ai créé une classe qui génère un ensemble de sons de façon aléatoire, la voici :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
 
import java.nio.*;
import java.util.*;
import javax.sound.sampled.*;
 
public class Sounder implements Runnable {
 
  private static final int SAMPLE_SIZE_IN_BITS = 16;
  private static final int CHANNELS = 2;
  private static final boolean SIGNED = true;
  private static final boolean BIG_ENDIAN = true;
 
  private float SAMPLE_RATE;
  private AudioFormat AUDIO_FORMAT;
  private DataLine.Info LINE_INFO;
 
  private byte[] bytes;
  private ByteBuffer buffer;
  private short valueLeft;
  private short dValueLeft;
  private short valueRight;
  private short dValueRight;
 
  private SourceDataLine line;
 
  private Random random;
 
  public Sounder() {
    random = new Random(System.currentTimeMillis());
 
    SAMPLE_RATE = 44444;
    AUDIO_FORMAT = new AudioFormat(SAMPLE_RATE, SAMPLE_SIZE_IN_BITS, CHANNELS, SIGNED, BIG_ENDIAN);
    LINE_INFO = new DataLine.Info(SourceDataLine.class, AUDIO_FORMAT);
 
    bytes = new byte[10000];
    buffer = ByteBuffer.wrap(bytes, 0, bytes.length);
 
    valueLeft = (short)random.nextInt(1000);
    dValueLeft = (short)random.nextInt(100);
    valueRight = (short)random.nextInt(1000);
    dValueRight = (short)random.nextInt(100);
    try {
      line = (SourceDataLine)AudioSystem.getLine(LINE_INFO);
      line.open();
      line.start();
    }
    catch(Exception e) {
      e.printStackTrace();
    }
    new Thread(this).start();
  }
 
  public void next() {
    int bs = bytes.length / 2;
    int min = random.nextInt(bs / 2);
    int max = random.nextInt(bs / 2) + bs / 2;
    next(min, max);
  }
 
  public void next(int min, int max) {
    for (int i = min ; i < max; i++) {
      dValueLeft += random.nextInt(7) - 3;
      valueLeft += dValueLeft;
      if (Math.abs(valueLeft) >= 16380)
        dValueLeft = (short)-dValueLeft;
      buffer.putShort(i, valueLeft);
 
      dValueRight += random.nextInt(7) - 3;
      valueRight += dValueRight;
      if (Math.abs(valueRight) >= 16380)
        dValueRight = (short)-dValueRight;
      buffer.putShort(i + bytes.length / 2, valueRight);
    }
    line.write(bytes, 0, bytes.length);
  }
 
  public void run() {
    while (true) {
      next();
      try {
        Thread.sleep(50);
      }
      catch (Exception e) {
        e.printStackTrace();
      }
    }
  }
 
  public static void main(String[] args) {
    System.out.println("Brouillon de fond sonore.");
    System.out.println("Appuyez sur Ctrl-C pour terminer le programme.");
    for (int i = 0 ; i < 7 ; i++)
      new Sounder();
  }
 
}
J'utilise un codage de son linéaire PCM avec les canaux stéréo. Cependant je ne maitrise pas du tout l'aspect stéréo.
J'ai essayé deux méthodes :
- En supposant que le canal gauche et le canal droit sont entrelacés, j'ai codé ainsi la séquence : [gauche][droite][gauche][droite] etc...
- En supposant que le canal gauche est avant le canal droit ou inversement, j'ai codé la séquence ainsi : [gauche][gauche]...[gauche][droite][droite]...[droite]
Mais aucune n'a semblé fonctionner.

Je cherche à comprendre comment encoder les données pour contrôler l'aspect stéréo du son généré.

Comment dois-je m'y prendre ?