Ticket #5093: game_kit_sound.patch
File game_kit_sound.patch, 8.2 KB (added by , 14 years ago) |
---|
-
src/kits/game/GameSoundDevice.cpp
180 180 181 181 status_t 182 182 BGameSoundDevice::CreateBuffer(gs_id* sound, const void* object, 183 const gs_audio_format* format) 183 const gs_audio_format* format, size_t inBufferFrameCount, 184 size_t inBufferCount) 184 185 { 185 186 if (!object || !sound) 186 187 return B_BAD_VALUE; … … 189 190 int32 position = AllocateSound(); 190 191 191 192 if (position >= 0) { 192 fSounds[position] = new StreamingSoundBuffer(format, object); 193 fSounds[position] = new StreamingSoundBuffer(format, object, 194 inBufferFrameCount, inBufferCount); 193 195 err = fSounds[position]->Connect(&fConnection->producer); 194 196 } 195 197 -
src/kits/game/GameSoundDevice.h
51 51 int64 frames); 52 52 virtual status_t CreateBuffer(gs_id * sound, 53 53 const void * object, 54 const gs_audio_format * format); 54 const gs_audio_format * format, 55 size_t inBufferFrameCount = 0, 56 size_t inBufferCount = 0); 55 57 virtual void ReleaseBuffer(gs_id sound); 56 58 57 59 virtual status_t Buffer(gs_id sound, … … 73 75 void SetInitError(status_t error); 74 76 75 77 gs_audio_format fFormat; 78 76 79 private: 77 80 status_t Connect(); 78 81 int32 AllocateSound(); -
src/kits/game/GameSoundBuffer.cpp
32 32 #include <MediaAddOn.h> 33 33 #include <MediaTheme.h> 34 34 #include <TimeSource.h> 35 #include <BufferGroup.h> 35 36 36 37 #include "GameProducer.h" 37 38 #include "GameSoundBuffer.h" … … 270 271 pan[1] = fPanLeft; 271 272 272 273 char * buffer = new char[fFrameSize * frames]; 273 274 274 275 FillBuffer(buffer, frames); 275 276 276 277 switch (fFormat.format) { … … 531 532 532 533 // StreamingSoundBuffer ------------------------------------------------------ 533 534 StreamingSoundBuffer::StreamingSoundBuffer(const gs_audio_format * format, 534 const void * streamHook) 535 const void * streamHook, size_t inBufferFrameCount, 536 size_t inBufferCount) 535 537 : 536 538 GameSoundBuffer(format), 537 539 fStreamHook(const_cast<void *>(streamHook)) 538 540 { 541 if (inBufferFrameCount != 0 && inBufferCount != 0) { 542 BBufferGroup *bufferGroup = new BBufferGroup(inBufferFrameCount 543 * fFrameSize, inBufferCount); 544 fNode->SetBufferGroup(fConnection->source, bufferGroup); 545 } 539 546 } 540 547 541 548 … … 552 559 size_t bytes = fFrameSize * frames; 553 560 object->FillBuffer(buffer, bytes); 554 561 } 555 -
src/kits/game/GameProducer.cpp
234 234 media_node_id id; 235 235 FindLatencyFor(fOutput.destination, &fLatency, &id); 236 236 237 if (!fBufferGroup) 238 fBufferSize = fOutput.format.u.raw_audio.buffer_size; 239 // Have to set it before latency calculating 240 237 241 // Use a dry run to see how long it takes me to fill a buffer of data 238 242 239 243 // The first step to setup the buffer 240 244 bigtime_t start, produceLatency; 241 int32 frames = int32(f Output.format.u.raw_audio.buffer_size / fFrameSize);245 int32 frames = int32(fBufferSize / fFrameSize); 242 246 float* data = new float[frames * 2]; 243 247 244 248 // Second, fill the buffer … … 263 267 // Set up the buffer group for our connection, as long as nobody handed us a 264 268 // buffer group (via SetBufferGroup()) prior to this. 265 269 if (!fBufferGroup) { 266 size_t size = fOutput.format.u.raw_audio.buffer_size;267 270 int32 count = int32(fLatency / BufferDuration() + 2); 268 fBufferGroup = new BBufferGroup( size, count);271 fBufferGroup = new BBufferGroup(fBufferSize, count); 269 272 } 270 273 } 271 274 … … 312 315 if (newGroup != NULL) { 313 316 // we were given a valid group; just use that one from now on 314 317 fBufferGroup = newGroup; 318 319 // get buffer length from the first buffer 320 BBuffer *buffers[1]; 321 if (newGroup->GetBufferList(1, buffers) != B_OK) 322 return B_BAD_VALUE; 323 fBufferSize = buffers[0]->SizeAvailable(); 315 324 } else { 316 325 // we were passed a NULL group pointer; that means we construct 317 326 // our own buffer group to use from now on 318 size_t size = fOutput.format.u.raw_audio.buffer_size;327 fBufferSize = fOutput.format.u.raw_audio.buffer_size; 319 328 int32 count = int32(fLatency / BufferDuration() + 2); 320 fBufferGroup = new BBufferGroup( size, count);329 fBufferGroup = new BBufferGroup(fBufferSize, count); 321 330 } 322 331 323 332 return B_OK; … … 356 365 // The other run modes dictate various strategies for sacrificing data quality 357 366 // in the interests of timely data delivery. The way *we* do this is to skip 358 367 // a buffer, which catches us up in time by one buffer duration. 359 size_t nSamples = f Output.format.u.raw_audio.buffer_size / fFrameSize;368 size_t nSamples = fBufferSize / fFrameSize; 360 369 fFramesSent += nSamples; 361 370 } 362 371 } … … 478 487 } 479 488 480 489 // track how much media we've delivered so far 481 size_t nFrames = f Output.format.u.raw_audio.buffer_size / fFrameSize;490 size_t nFrames = fBufferSize / fFrameSize; 482 491 fFramesSent += nFrames; 483 492 484 493 // The buffer is on its way; now schedule the next one to go … … 497 506 498 507 BBuffer* 499 508 GameProducer::FillNextBuffer(bigtime_t event_time) 500 { 509 { 501 510 // get a buffer from our buffer group 502 BBuffer* buf = fBufferGroup->RequestBuffer(f Output.format.u.raw_audio.buffer_size, BufferDuration());511 BBuffer* buf = fBufferGroup->RequestBuffer(fBufferSize, BufferDuration()); 503 512 504 513 // if we fail to get a buffer (for example, if the request times out), we skip this 505 514 // buffer and go on to the next, to avoid locking up the control thread … … 507 516 return NULL; 508 517 509 518 // we need to discribe the buffer 510 int64 frames = int64(f Output.format.u.raw_audio.buffer_size / fFrameSize);511 memset(buf->Data(), 0, f Output.format.u.raw_audio.buffer_size);519 int64 frames = int64(fBufferSize / fFrameSize); 520 memset(buf->Data(), 0, fBufferSize); 512 521 513 522 // now fill the buffer with data, continuing where the last buffer left off 514 523 fObject->Play(buf->Data(), frames); … … 516 525 // fill in the buffer header 517 526 media_header* hdr = buf->Header(); 518 527 hdr->type = B_MEDIA_RAW_AUDIO; 519 hdr->size_used = f Output.format.u.raw_audio.buffer_size;528 hdr->size_used = fBufferSize; 520 529 hdr->time_source = TimeSource()->ID(); 521 530 522 531 bigtime_t stamp; -
src/kits/game/GameSoundBuffer.h
65 65 66 66 size_t fFrameSize; 67 67 68 Connection * fConnection; 69 GameProducer * fNode; 70 68 71 private: 69 72 70 Connection * fConnection;71 GameProducer * fNode;72 73 bool fIsConnected; 73 74 bool fIsPlaying; 74 75 … … 103 104 class StreamingSoundBuffer : public GameSoundBuffer { 104 105 public: 105 106 StreamingSoundBuffer(const gs_audio_format * format, 106 const void * streamHook); 107 const void * streamHook, 108 size_t inBufferFrameCount, 109 size_t inBufferCount); 107 110 virtual ~StreamingSoundBuffer(); 108 111 109 112 protected: -
src/kits/game/GameProducer.h
146 146 size_t fFrameSize; 147 147 int64 fFramesSent; 148 148 GameSoundBuffer * fObject; 149 size_t fBufferSize; 149 150 }; 150 151 151 152 #endif -
src/kits/game/StreamingGameSound.cpp
106 106 size_t inBufferCount) 107 107 { 108 108 gs_id sound; 109 status_t error = Device()->CreateBuffer(&sound, this, format); 109 status_t error = Device()->CreateBuffer(&sound, this, format, 110 inBufferFrameCount, inBufferCount); 110 111 if (error != B_OK) return error; 111 112 112 113 return BGameSound::Init(sound);