Skip to content

Commit

Permalink
Merge pull request #2 from eeyrw/code-refactoring
Browse files Browse the repository at this point in the history
Code refactoring
  • Loading branch information
eeyrw authored Dec 14, 2019
2 parents fdbb348 + b3b43c4 commit ec99717
Show file tree
Hide file tree
Showing 19 changed files with 480 additions and 35 deletions.
22 changes: 22 additions & 0 deletions BinarySearch.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# 查找最后一个等于或者大于key的元素
def findLastEqualSmaller(arr, key):
left = 0
right = len(arr) - 1
length = len(arr)

# 这里必须是 <=
while left <= right:
mid = (left + right) // 2
if arr[mid] >= key:
right = mid - 1
else:
left = mid + 1
if left>=length:
left=length-1
return left


arr=[6,3,4,1,9]
idx = findLastEqualSmaller(arr,11)
print('Idx:',idx)
print('Value:',arr[idx])
6 changes: 3 additions & 3 deletions KeyScan.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
#include <stdint.h>
#include <stddef.h>

uint32_t keyStatusList[KEY_NUM];
uint32_t keyValueList[KEY_NUM];
uint8_t keyStatusList[KEY_NUM];
uint8_t keyValueList[KEY_NUM];
void (*keyCallBackList[KEY_NUM])(uint32_t);

void KeyRawInput(uint32_t keyIndex, uint32_t keyValue)
Expand Down Expand Up @@ -61,4 +61,4 @@ void KeyScanInit(void)
keyStatusList[keyIndex] = KEY_STATUS_IDLE;
keyValueList[keyIndex] = !KEY_ACTIVE;
}
}
}
5 changes: 3 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ SRC += $(ROOT_DIR)/WaveTableSynthesizer/Player.c

SRC += $(ROOT_DIR)/Ring-Buffer/ringbuffer.c

ASM_SRC += $(ROOT_DIR)/WaveTableSynthesizer/Synth_m0.s
ASM_SRC += $(ROOT_DIR)/WaveTableSynthesizer/PeriodTimer.s
ASM_SRC += $(ROOT_DIR)/WaveTableSynthesizer/SynthCoreAsm.s
# ASM_SRC += $(ROOT_DIR)/ScoreList.s


Expand Down Expand Up @@ -88,7 +89,7 @@ all: $(OBJECTS) $(PROJECT_NAME).elf $(PROJECT_NAME).hex $(PROJECT_NAME).bin
@echo [CC] $(notdir $<)
@$(CC) -c $(CP_FLAGS) -I . $(INC_DIR) $< -o $@

%.o: %.s
%.o: %.s $(ROOT_DIR)/WaveTableSynthesizer/Synth.inc $(ROOT_DIR)/WaveTableSynthesizer/UpdateTick.inc
@echo [AS] $(notdir $<)
@$(AS) -c $(AS_FLAGS) $< -o $@

Expand Down
2 changes: 1 addition & 1 deletion Ring-Buffer/ringbuffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* can be contained in the buffer.
* The buffer size must be a power of two.
*/
#define RING_BUFFER_SIZE 1024
#define RING_BUFFER_SIZE 512

#if (RING_BUFFER_SIZE & (RING_BUFFER_SIZE - 1)) != 0
#error "RING_BUFFER_SIZE must be a power of two"
Expand Down
6 changes: 6 additions & 0 deletions WaveTableSynthesizer/AsmCommon.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#ifndef __ASM_COMMON_H__
#define __ASM_COMMON_H__



#endif
12 changes: 12 additions & 0 deletions WaveTableSynthesizer/PeriodTimer.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#ifndef __PERIOD_TIMER_H__
#define __PERIOD_TIMER_H__

#ifndef __ASSEMBLER__
extern void PIT_Task(void);

#else


#endif

#endif
28 changes: 28 additions & 0 deletions WaveTableSynthesizer/PeriodTimer.s
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#include "Player.h"
#include "RegDefNv32.h"

#define PeriodTimerHandler PIT_Ch0Isr_Override

.syntax unified
.section .text
.thumb_func
.global PeriodTimerHandler
.func PeriodTimerHandler
PeriodTimerHandler:
push {r4-r7,lr} // r0-r3,r12 saved by hardware when expection happening
ldr r0,=#PIT_TFLG0
ldr r1,[r0]
ldr r2,=#PIT_TFLG_TIF_BIT
orrs r1,r1,r2
str r1,[r0]

ldr r5,=#GlobalPlayerPtr
ldr r5,[r5]
ldr r6,=#pSynthesizer
adds r0,r5,r6
ldr r6,=#pScoreDecoder
adds r1,r5,r6
#include "Synth.inc"
#include "UpdateTick.inc"
pop {r4-r7,pc}
.endfunc
5 changes: 4 additions & 1 deletion WaveTableSynthesizer/Player.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
#include "SynthCore.h"
#include "Player.h"

Player *GlobalPlayerPtr;

void Player32kProc(Player *player)
{
SynthAsm(&(player->synthesizer));
Expand Down Expand Up @@ -178,9 +180,10 @@ void StopDecode(Player *player)

void PlayerInit(Player *player)
{
GlobalPlayerPtr = player;
player->decoder.status = STATUS_STOP;
player->decoder.currentTick = 0;
player->decoder.lastScoreTick = 0;
player->decoder.scorePointer = NULL;
SynthInit(&(player->synthesizer));
}
}
33 changes: 29 additions & 4 deletions WaveTableSynthesizer/Player.h
Original file line number Diff line number Diff line change
@@ -1,14 +1,29 @@
#ifndef __ScoreDecoder_H__
#define __ScoreDecoder_H__

#include <stdint.h>
#include "SynthCore.h"

#define pSynthesizer 0
#define pScoreDecoder (pSynthesizer+SynthesizerSize)
#define pCurrentTick 0
#define pLastScoreTick (pCurrentTick+4)
#define pStatus (pLastScoreTick+4)
#define constSTATUS_DECODING 2

#ifndef __ASSEMBLER__

#include <stdint.h>


#ifdef __cplusplus
extern "C" {
#endif

enum DECODER_STATUS
{
STATUS_STOP = 0,
STATUS_REDAY_TO_DECODE = 1,
STATUS_DECODING = 2
STATUS_DECODING = constSTATUS_DECODING
};

enum SCHEDULER_MODE
Expand Down Expand Up @@ -55,11 +70,12 @@ typedef struct _Player
Synthesizer synthesizer;
ScoreDecoder decoder;
PlayScheduler scheduler;
}Player;
} Player;



extern ScoreListHeader ScoreDataList;
extern Player *GlobalPlayerPtr;

extern void PlayerInit(Player *player);
extern void Player32kProc(Player *player);
Expand All @@ -74,4 +90,13 @@ extern void PlaySchedulerPreviousScore(Player *player);
extern void SchedulerSetIntialRandomSeed(Player *player,uint8_t randomSeed);
extern void UpdateTick(ScoreDecoder *decoder);
extern void UpdateNextScoreTick(ScoreDecoder *decoder);
#endif

#ifdef __cplusplus
} //end extern "C"
#endif

#else
.extern GlobalPlayerPtr;
#endif

#endif
11 changes: 11 additions & 0 deletions WaveTableSynthesizer/RegDefNv32.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#ifndef __REG_DEF_NV32_H__
#define __REG_DEF_NV32_H__

#define PWM_OUT1 0x4003A030
#define PWM_OUT2 0x4003A038
#define PIT_TFLG0 0x4003710C
#define PIT_TFLG1 0x4003711C
#define PIT_TFLG_TIF_BIT 0x01

#endif

81 changes: 81 additions & 0 deletions WaveTableSynthesizer/Synth.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
#include "SynthCore.h"
#include "AsmCommon.h"
#include "RegDefNv32.h"

.syntax unified
.section .text
SynthAsm:
pSoundUnit .req r3
loopIndex .req r4
mixOut .req r2

movs loopIndex,#POLY_NUM
movs mixOut,#0
movs pSoundUnit,r0

loopSynth:
ldrh r7,[pSoundUnit,#pEnvelopeLevel]
cmp r7,#0
beq loopSynthEnd
ldr r5,[pSoundUnit,#pWaveTableAddress]
ldr r6,[pSoundUnit,#pWavetablePos]
lsrs r6,r6,#8 @wavetablePos /= 256
lsls r6,r6,#1 @wavetablePos *= 2
ldrsh r6,[r5,r6] @ Load signed 16bit sample to r6
#ifdef RUN_TEST
strsh r6,[pSoundUnit,#pSampleVal]
#endif
muls r7,r6,r7 @sample*envelope/256
#ifdef RUN_TEST
asrs r7,r7,#8
strsh r7,[pSoundUnit,#pVal]
#endif
adds mixOut,r7,mixOut @mixOut+=sample*envelope/256

ldr r6,[pSoundUnit,#pWavetablePos]
ldr r5,[pSoundUnit,#pIncrement]
adds r6,r5,r6
ldr r5,[pSoundUnit,#pWaveTableLen]
lsls r5,r5,#8 @pWaveTableLen*=256
cmp r5,r6
bhi wavePosUpdateEnd @bhi : HI C = 1 ands Z = 0 Higher, unsigned
ldr r5,[pSoundUnit,#pWaveTableLoopLen]
lsls r5,r5,#8 @waveTableLoopLen*=256
subs r6,r6,r5
wavePosUpdateEnd:
str r6,[pSoundUnit,#pWavetablePos]
loopSynthEnd:

adds pSoundUnit,pSoundUnit,#SoundUnitSize
subs loopIndex,loopIndex,#1 @ set n = n-1
bne loopSynth
movs pSoundUnit,r0

ldr r5,=#pMixOut
adds r5,r5,pSoundUnit
str mixOut,[r5]

@
ldrh r5,[r5,#(pMainVolume-pMixOut)]
asrs mixOut,mixOut,#8
muls mixOut,r5,mixOut
asrs mixOut,mixOut,#(MAX_VOLUME_SHIFT_BIT+8)

ldr r5,=#-128
cmp mixOut,r5
bge lowerBoundSatisfied
movs mixOut,r5
lowerBoundSatisfied:
ldr r5,=#127
cmp mixOut,r5
ble saturateEnd
movs mixOut,r5
saturateEnd:

@ mixOut: [-512,511] -> [0,1023]
ldr r5,=#128
adds mixOut,mixOut,r5
ldr r5,=#PWM_OUT1
strh mixOut,[r5]
ldr r5,=#PWM_OUT2
strh mixOut,[r5]
12 changes: 9 additions & 3 deletions WaveTableSynthesizer/SynthCore.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@
#include "WaveTable_Celesta_C6.h"
#include "EnvelopeTable.h"

#ifdef RUN_TEST
Synthesizer synthForC;
#endif

Synthesizer *GlobalSynthPtr;

void SynthInit(Synthesizer *synth)
{
SoundUnit *soundUnits = synth->SoundUnitList;
Expand All @@ -14,7 +20,6 @@ void SynthInit(Synthesizer *synth)
soundUnits[i].wavetablePos = 0;
soundUnits[i].envelopeLevel = 0;
soundUnits[i].envelopePos = 0;
soundUnits[i].val = 0;
soundUnits[i].waveTableAddress = (uint32_t)WaveTable_Celesta_C5;
soundUnits[i].waveTableLen = WAVETABLE_CELESTA_C5_LEN;
soundUnits[i].waveTableLoopLen = WAVETABLE_CELESTA_C5_LOOP_LEN;
Expand All @@ -24,6 +29,7 @@ void SynthInit(Synthesizer *synth)
synth->lastSoundUnit = 0;
synth->mainVolume = 1 << (MAX_VOLUME_SHIFT_BIT - 1);
synth->decayGenTick = 0;
GlobalSynthPtr = synth;
}

void SynthRegisterHwChangeFunc(Synthesizer *synth, void (*hwSet)(SYNTH_HW_STATUS))
Expand All @@ -45,7 +51,7 @@ void SynthOff(Synthesizer *synth)

void SynthGenEnvelopeProcess(Synthesizer *synth)
{
if (synth->decayGenTick >= 150)
if (synth->decayGenTick >= DECAY_TIME_FACTOR)
{
GenDecayEnvlopeAsm(synth);
synth->decayGenTick = 0;
Expand Down Expand Up @@ -113,4 +119,4 @@ void GenDecayEnvlopeC(Synthesizer *synth)
}
}
}
#endif
#endif
Loading

0 comments on commit ec99717

Please sign in to comment.