瀏覽代碼

Add status and position functions to AudioPlayMemory

dds
PaulStoffregen 10 年之前
父節點
當前提交
cb5ea4bf7f
共有 2 個文件被更改,包括 66 次插入0 次删除
  1. +62
    -0
      play_memory.cpp
  2. +4
    -0
      play_memory.h

+ 62
- 0
play_memory.cpp 查看文件

@@ -36,6 +36,7 @@ void AudioPlayMemory::play(const unsigned int *data)
prior = 0;
format = *data++;
next = data;
beginning = data;
length = format & 0xFFFFFF;
playing = format >> 24;
}
@@ -186,4 +187,65 @@ void AudioPlayMemory::update(void)
}


#define B2M_88200 (uint32_t)((double)4294967296000.0 / AUDIO_SAMPLE_RATE_EXACT / 2.0)
#define B2M_44100 (uint32_t)((double)4294967296000.0 / AUDIO_SAMPLE_RATE_EXACT) // 97352592
#define B2M_22050 (uint32_t)((double)4294967296000.0 / AUDIO_SAMPLE_RATE_EXACT * 2.0)
#define B2M_11025 (uint32_t)((double)4294967296000.0 / AUDIO_SAMPLE_RATE_EXACT * 4.0)


uint32_t AudioPlayMemory::positionMillis(void)
{
uint8_t p;
const uint8_t *n, *b;
uint32_t b2m;

__disable_irq();
p = playing;
n = (const uint8_t *)next;
b = (const uint8_t *)beginning;
__enable_irq();
switch (p) {
case 0x81: // 16 bit PCM, 44100 Hz
b2m = B2M_88200; break;
case 0x01: // u-law encoded, 44100 Hz
case 0x82: // 16 bits PCM, 22050 Hz
b2m = B2M_44100; break;
case 0x02: // u-law encoded, 22050 Hz
case 0x83: // 16 bit PCM, 11025 Hz
b2m = B2M_22050; break;
case 0x03: // u-law encoded, 11025 Hz
b2m = B2M_11025; break;
default:
return 0;
}
if (p == 0) return 0;
return ((uint64_t)(n - b) * b2m) >> 32;
}

uint32_t AudioPlayMemory::lengthMillis(void)
{
uint8_t p;
const uint32_t *b;
uint32_t b2m;

__disable_irq();
p = playing;
b = (const uint32_t *)beginning;
__enable_irq();
switch (p) {
case 0x81: // 16 bit PCM, 44100 Hz
case 0x01: // u-law encoded, 44100 Hz
b2m = B2M_44100; break;
case 0x82: // 16 bits PCM, 22050 Hz
case 0x02: // u-law encoded, 22050 Hz
b2m = B2M_22050; break;
case 0x83: // 16 bit PCM, 11025 Hz
case 0x03: // u-law encoded, 11025 Hz
b2m = B2M_11025; break;
default:
return 0;
}
return ((uint64_t)(*(b - 1) & 0xFFFFFF) * b2m) >> 32;
}



+ 4
- 0
play_memory.h 查看文件

@@ -35,9 +35,13 @@ public:
AudioPlayMemory(void) : AudioStream(0, NULL), playing(0) { }
void play(const unsigned int *data);
void stop(void);
bool isPlaying(void) { return playing; }
uint32_t positionMillis(void);
uint32_t lengthMillis(void);
virtual void update(void);
private:
const unsigned int *next;
const unsigned int *beginning;
uint32_t length;
int16_t prior;
volatile uint8_t playing;

Loading…
取消
儲存