Aller au contenu


Photo

Probleme avec le wave et la stereo


  • Please log in to reply
No replies to this topic

#1 SuperCed

SuperCed

    iCeinture jaune

  • Membres
  • 35 Messages :

Posté 02 May 2002 - 01:27 PM

J'ai fait un systeme pour importer et jouer les wav mais ca ne fonctionne pas vraiment car je n'ai pas de stereo. A la place d'avoir la stereo, j'ai une enceinte qui joue, puis l'autre, mais jamais les deux en meme temps.

Je file mon code :

#include "WAVEReader.h"
#include "logFILE.h"

int monVolume = 1.0;

int WAVEOpen(char *path, WAVEInfo *theWAVEInfo)
{
//WAVEInfo *wave;
FILE *file;
char c[256];
unsigned char temp[4];
//unsigned char *tempData;
unsigned int i;

file = fopen(path, "r");
if (!file) {
logFILE("Pas trouve le son");
return NULL;
}

for (i=0;i<4;i++) {
fread(&(theWAVEInfo->theRIFFChunk.chunkID[i]), sizeof(char), 1, file);
}

for (i=0;i<4;i++) {
fread(&(temp[3-i]), sizeof(char), 1, file);
}
theWAVEInfo->theRIFFChunk.chunkSize = (int)(temp[0])*256*256*256 + (int)(temp[1])*256*256 + (int)(temp[2])*256 + (int)(temp[3]);

for (i=0;i<4;i++) {
fread(&(theWAVEInfo->theRIFFChunk.fileType[i]), sizeof(char), 1, file);
}

/***********************************************************************************/
for (i=0;i<4;i++) {
fread(&(theWAVEInfo->theFormatChunk.chunkID[i]), sizeof(char), 1, file);
}

for (i=0;i<4;i++) {
fread(&(temp[3-i]), sizeof(char), 1, file);
}
theWAVEInfo->theFormatChunk.chunkSize = (int)(temp[0])*256*256*256 + (int)(temp[1])*256*256 + (int)(temp[2])*256 + (int)(temp[3]);

for (i=0;i<2;i++) {
fread(&(temp[1-i]), sizeof(char), 1, file);
}
theWAVEInfo->theFormatChunk.chunkChaiPas = (short)(temp[0])*256 + (short)(temp[1]);

for (i=0;i<2;i++) {
fread(&(temp[1-i]), sizeof(char), 1, file);
}
theWAVEInfo->theFormatChunk.chanelNumber = (short)(temp[0])*256 + (short)(temp[1]);

for (i=0;i<4;i++) {
fread(&(temp[3-i]), sizeof(char), 1, file);
}
theWAVEInfo->theFormatChunk.sampleRate = (int)(temp[0]*256*256*256) + (int)(temp[1]*256*256) + (int)(temp[2]) * 256 + (int)(temp[3]);

for (i=0;i<4;i++) {
fread(&(temp[3-i]), sizeof(char), 1, file);
}
theWAVEInfo->theFormatChunk.bytesPerSample = (int)(temp[0])*256*256*256 + (int)(temp[1])*256*256 + (int)(temp[2]) * 256 + (int)(temp[3]);

for (i=0;i<2;i++) {
fread(&(temp[1-i]), sizeof(char), 1, file);
}
theWAVEInfo->theFormatChunk.bytesPerSample = (short)(temp[0])*256 + (short)(temp[1]);

for (i=0;i<2;i++) {
fread(&(temp[1-i]), sizeof(char), 1, file);
}
theWAVEInfo->theFormatChunk.bitsPerSample = (short)(temp[0])*256 + (short)(temp[1]);

/***********************************************************************************/
for (i=0;i<4;i++) {
fread(&(theWAVEInfo->theDataChunk.nomData[i]), sizeof(char), 1, file);
}

for (i=0;i<4;i++) {
fread(&(temp[3-i]), sizeof(char), 1, file);
}
theWAVEInfo->theDataChunk.dataLength = (int)(temp[0])*256*256*256 + (int)(temp[1])*256*256 + (int)(temp[2]) * 256 + (int)(temp[3]);

theWAVEInfo->theDataChunk.data = (unsigned short*)malloc(theWAVEInfo->theDataChunk.dataLength*sizeof(unsigned short));
for (i=0;i<theWAVEInfo->theDataChunk.dataLength;i+=2) {
fread(&(temp[0]), sizeof(char), 1, file);
fread(&(temp[1]), sizeof(char), 1, file);
theWAVEInfo->theDataChunk.data[i] = (unsigned short)(temp[1])*256+(unsigned short)(temp[0]);
}
sprintf(c, "Taille du son : %d", theWAVEInfo->theDataChunk.dataLength);
logFILE©;

theWAVEInfo->theDataChunk.position = 0;
fclose(file);
return 0;
}

int WAVERead(WAVEInfo *wave, unsigned short *samples, unsigned int sampleCount)
{
unsigned int index;
monVolume += 0.1;
int pos = wave->theDataChunk.position;
int max = wave->theDataChunk.dataLength;
int i = pos;
for (index = 0; index < sampleCount; index++) {
if (i>max) {
i = 0;
//return 0;
}
samples[index] = wave->theDataChunk.data[i];
i++;

}
wave->theDataChunk.position = i;
return sampleCount;
}

/*********************************/

#import "CoreAudio.h"

//#import <Foundation/Foundation.h>
#include <CoreAudio/AudioHardware.h>

#include "logFILE.h"

#define SAMPLES_PER_BUFFER (8*1024)

static WAVEInfo *waveInfo;
static unsigned short *samples;
static unsigned int sampleBufferCounter;
static unsigned int zeroBufferCounter;
float monScale = 0.0f;

static AudioDeviceID outputDeviceID;
static AudioStreamBasicDescription outputStreamBasicDescription;

static OSStatus audioDeviceIOProc(AudioDeviceID inDevice,
const AudioTimeStamp *inNow,
const AudioBufferList *inInputData,
const AudioTimeStamp *inInputTime,
AudioBufferList *outOutputData,
const AudioTimeStamp *inOutputTime,
void *inClientData);

void StartPlayingViaCoreAudio(WAVEInfo *wave)
{
OSStatus status;
UInt32 propertySize, bufferByteCount;
char c[256];

logFILE("StartPlayingViaCoreAudio");

// Get the output device
propertySize = sizeof(outputDeviceID);
status = AudioHardwareGetProperty(kAudioHardwarePropertyDefaultOutputDevice, &propertySize, &outputDeviceID);
if (status) {
sprintf(c,"AudioHardwareGetProperty returned %d", status);
logFILE©;
return;
}

if (outputDeviceID == kAudioDeviceUnknown) {
logFILE("AudioHardwareGetProperty: outputDeviceID is kAudioDeviceUnknown");
return;
}

// Configure the output device
propertySize = sizeof(bufferByteCount);
bufferByteCount = SAMPLES_PER_BUFFER * sizeof(float);
status = AudioDeviceSetProperty(outputDeviceID, NULL, 0, 0, kAudioDevicePropertyBufferSize, propertySize, &bufferByteCount);
if (status) {
sprintf(c,"AudioDeviceSetProperty: returned %d when setting kAudioDevicePropertyBufferSize to %d", status, SAMPLES_PER_BUFFER);
logFILE©;
return;
}

// Print out the device status
propertySize = sizeof(outputStreamBasicDescription);
status = AudioDeviceGetProperty(outputDeviceID, 0, 0, kAudioDevicePropertyStreamFormat, &propertySize, &outputStreamBasicDescription);
if (status) {
sprintf(c,"AudioDeviceGetProperty: returned %d when getting kAudioDevicePropertyStreamFormat", status);
logFILE©;
return;
}

logFILE("Hardware format:");
sprintf(c," %f mSampleRate", outputStreamBasicDescription.mSampleRate);
logFILE©;
/*sprintf(" %c%c%c%c mFormatID",
(outputStreamBasicDescription.mFormatID & 0xff000000) >> 24,
(outputStreamBasicDescription.mFormatID & 0x00ff0000) >> 16,
(outputStreamBasicDescription.mFormatID & 0x0000ff00) >> 8,
(outputStreamBasicDescription.mFormatID & 0x000000ff) >> 0);
logFILE©;*/
sprintf(c," %5d mBytesPerPacket", outputStreamBasicDescription.mBytesPerPacket);
logFILE©;
sprintf(c," %5d mFramesPerPacket", outputStreamBasicDescription.mFramesPerPacket);
logFILE©;
sprintf(c," %5d mBytesPerFrame", outputStreamBasicDescription.mBytesPerFrame);
logFILE©;
sprintf(c," %5d mChannelsPerFrame", outputStreamBasicDescription.mChannelsPerFrame);
logFILE©;
sprintf(c," %5d mBitsPerChannel", outputStreamBasicDescription.mBitsPerChannel);
logFILE©;
if (outputStreamBasicDescription.mFormatID != kAudioFormatLinearPCM) {
sprintf(c,"Default Audio Device doesn't support Linear PCM!");
logFILE©;
return;
}


waveInfo = wave;
sampleBufferCounter = 0;
zeroBufferCounter = 4;
samples = (unsigned short *)malloc(SAMPLES_PER_BUFFER * sizeof(*samples));

// Start sound running
status = AudioDeviceAddIOProc(outputDeviceID, audioDeviceIOProc, NULL);
if (status) {
sprintf(c,"AudioDeviceAddIOProc: returned %d", status);
logFILE©;
return;
}

status = AudioDeviceStart(outputDeviceID, audioDeviceIOProc);
if (status) {
sprintf(c,"AudioDeviceStart: returned %d", status);
logFILE©;
return;
}
}



OSStatus audioDeviceIOProc(AudioDeviceID inDevice,
const AudioTimeStamp *inNow,
const AudioBufferList *inInputData,
const AudioTimeStamp *inInputTime,
AudioBufferList *outOutputData,
const AudioTimeStamp *inOutputTime,
void *inClientData)
{
unsigned int sampleIndex, sampleCount;
float *outBuffer;
float scale = (1.0f / SHRT_MAX);
char c[256];

assert(outOutputData->mNumberBuffers == 1);
assert(outOutputData->mBuffers[0].mNumberChannels == 2);
assert(outOutputData->mBuffers[0].mDataByteSize == (SAMPLES_PER_BUFFER * sizeof(float)));

// The buffer that we need to fill
outBuffer = (float *)outOutputData->mBuffers[0].mData;

// Read some samples from the file.
sampleCount = WAVERead(waveInfo, samples, SAMPLES_PER_BUFFER);

monScale += 0.01;
// Convert whatever samples we got into floats. Scale the floats to be [-1..1].
for (sampleIndex = 0; sampleIndex < sampleCount; sampleIndex++) {
// Convert the samples from shorts to floats. Scale the floats to be [-1..1].
outBuffer[sampleIndex] = samples[sampleIndex] * scale;
//sprintf(c, "%f", outBuffer[sampleIndex]);
//logFILE©;
}

// Fill in zeros in the rest of the buffer
for (; sampleIndex < SAMPLES_PER_BUFFER; sampleIndex++)
outBuffer[sampleIndex] = 0.0;

return 0;
}


/***************************/
// Et la version Carbon :

#include "CarbonSound.h"

//#import <Foundation/Foundation.h>
#import <Carbon/Carbon.h>
#include "logFILE.h"

#define SAMPLES_PER_BUFFER (8*1024)
#define BUFFER_COUNT (4)

static WAVEInfo *waveInfo;
static SndChannel *soundChannel;
static ExtSoundHeader soundHeader;
static unsigned short *samples[BUFFER_COUNT];
static unsigned int sampleBufferCounter;


static void _callback(SndChannel *sc, SndCommand *cmd);

void StartPlayingViaCarbon(WAVEInfo *wave)
{
OSErr err;
unsigned int bufferIndex;
char c[256];

soundChannel = NULL;
err = SndNewChannel(&soundChannel, sampledSynth, initStereo, _callback);
if (err != noErr) {
sprintf(c,"SndNewChannel -> %d", err);
logFILE©;
return;
}
//sprintf(c,"SndNewChannel -> %d", *soundChannel);
//logFILE©;

waveInfo = wave;

// Immediately queue up BUFFER_COUNT buffers of sound.
// By using multiple buffers, we hope to be playing a buffer while we are preparing another
sampleBufferCounter = 0;
for (bufferIndex = 0; bufferIndex < BUFFER_COUNT; bufferIndex++) {
samples[bufferIndex] = (unsigned short*)malloc(SAMPLES_PER_BUFFER * sizeof(*samples));
_callback(soundChannel, NULL);
}
}

static void _callback( SndChannel *sc, SndCommand *cmd )
{
SndCommand playBufferCommand;
SndCommand doCallbackCommand;
SndCommand syncCommand;
unsigned short *buffer;

buffer = samples[sampleBufferCounter % BUFFER_COUNT];

if (SAMPLES_PER_BUFFER != WAVERead(waveInfo, buffer, SAMPLES_PER_BUFFER)) {
// Out of data. We could play whatever we got, but this is example code, so the reader can implement that icons/icon7.gif
// We'll just return w/o queuing anything up, terminating the playback
return;
}
memset( &soundHeader, 0, sizeof( soundHeader ) );
soundHeader.samplePtr = (char*)buffer;
soundHeader.numChannels = 2;
soundHeader.sampleRate = rate44khz;
soundHeader.loopStart = 0;
soundHeader.loopEnd = 0;
soundHeader.encode = extSH;
soundHeader.baseFrequency = 1;
soundHeader.numFrames = SAMPLES_PER_BUFFER / 2;
soundHeader.markerChunk = NULL;
soundHeader.instrumentChunks = NULL;
soundHeader.AESRecording = NULL;
soundHeader.sampleSize = 16;

/*syncCommand.cmd = syncCmd;
syncCommand.param1 = 1;
syncCommand.param2 = 1;
SndDoImmediate(sc, &syncCommand);*/

playBufferCommand.cmd = bufferCmd;
playBufferCommand.param1 = 0;
playBufferCommand.param2 = (int)&soundHeader;
SndDoCommand(sc, &playBufferCommand, true);



// and another callback
doCallbackCommand.cmd = callBackCmd;
doCallbackCommand.param1 = 0;
doCallbackCommand.param2 = 0;
SndDoCommand(sc, &doCallbackCommand, true);

sampleBufferCounter++;
}





Aucun ne fonctionne, help me please.




0 utilisateur(s) en train de lire ce sujet

0 membre(s), 0 invité(s), 0 utilisateur(s) anonyme(s)