technical difficulties intro

This commit is contained in:
2024-08-11 20:39:01 -04:00
parent f544777047
commit 17cd15d5c7
10 changed files with 220 additions and 3 deletions
+82
View File
@@ -0,0 +1,82 @@
import sys
import numpy as np
from scipy.io import wavfile
from scipy import signal
def resample_audio(audio, original_sr, target_sr):
number_of_samples = round(len(audio) * float(target_sr) / original_sr)
return signal.resample(audio, number_of_samples)
def noise_shape_and_quantize(signal, bits):
steps = 2 ** bits
step_size = (signal.max() - signal.min()) / steps
shaped = np.zeros_like(signal)
error = np.zeros_like(signal)
for i in range(len(signal)):
shaped[i] = signal[i] - error[i]
quantized = np.round(shaped[i] / step_size) * step_size
error[i] = quantized - signal[i]
if i < len(signal) - 1:
error[i + 1] = error[i] * 0.5 # Simple first-order noise shaping
shaped[i] = quantized
return np.clip(shaped, signal.min(), signal.max())
# Read the WAV file
original_sr, data = wavfile.read(sys.argv[1])
data = data.astype(float)
data = data / np.max(np.abs(data))
# Define target sample rate
target_sr = 5000 # 6 kHz
# Resample the audio
resampled_data = resample_audio(data, original_sr, target_sr)
# Apply noise shaping and quantization
quantized = noise_shape_and_quantize(resampled_data, 4)
# Scale to 0-15 range and round to integers
scaled = np.round((quantized - quantized.min()) / (quantized.max() - quantized.min()) * 15).astype(int)
scaled = np.clip(scaled, 0, 15)
print("Min value:", scaled.min())
print("Max value:", scaled.max())
print("Unique values:", np.unique(scaled))
# Pack 4-bit values into bytes
packed = []
for i in range(0, len(scaled), 2):
if i + 1 < len(scaled):
byte = (scaled[i] << 4) | scaled[i + 1]
else:
byte = scaled[i] << 4
packed.append(byte)
# Write packed data to binary file
with open(sys.argv[1]+'.bin', 'wb') as f:
f.write(bytes(packed))
print(f"Packed 4-bit data written to output.bin")
print(f"Original sample rate: {original_sr} Hz")
print(f"New sample rate: {target_sr} Hz")
print(f"Number of samples: {len(scaled)}")
print(f"Duration: {len(scaled) / target_sr:.2f} seconds")
# Print first few bytes in hex
print("First 10 bytes in hex:")
print(" ".join(f"{b:02X}" for b in packed[:10]))
# Save the resampled audio as a WAV file for verification
# Correctly scale back to 16-bit audio range
wav_output = (scaled.astype(float) - 7.5) / 7.5 # Center around 0
wav_output = (wav_output * 32767).astype(np.int16) # Scale to 16-bit range
#wavfile.write('resampled_output.wav', target_sr, wav_output)
+131 -2
View File
@@ -39,7 +39,136 @@ start
mva #0 COLOR2 mva #0 COLOR2
sta COLBAK sta COLBAK
mva #15 COLOR1 mva #15 COLOR1
halt POKEY_INIT
lda #0
;sta dmactls
;sta dmactl
lda #0
sta $d40e ; wylaczamy NMI
sei ; oraz IRQ
;-----playa-da-sampla-----
ldx #0
please_wait_loop
lda samples_l,x
sta sample_load
lda samples_h,x
sta sample_load+1
lda samples_end_l,x
sta temp_w
lda samples_end_h,x
sta temp_w+1
@
lda sample_load: $ffff
tay
sec
:4 ror
sta AUDC1
sta AUDC2
sta AUDC3
;sta AUDC4
:3 sta wsync
tya
and #$0F
ora #$10
sta AUDC1
sta AUDC2
sta AUDC3
;sta AUDC4
inw sample_load
sta wsync
cpw sample_load temp_w
sta wsync
beq @+
sta wsync
jmp @-
@
inx
cpx #13
sne:ldx #0
jmp please_wait_loop
sample1
ins 'wait1.wav.bin'
sample_end1
sample2
ins 'wait2.wav.bin'
sample_end2
sample3
ins 'wait3.wav.bin'
sample_end3
sample4
ins 'wait4.wav.bin'
sample_end4
sample5
ins 'wait5.wav.bin'
sample_end5
sample6
ins 'wait6.wav.bin'
sample_end6
samples_l
.by <sample1
.by <sample2
.by <sample3
.by <sample2
.by <sample4
.by <sample2
.by <sample5
.by <sample2
.by <sample3
.by <sample2
.by <sample4
.by <sample2
.by <sample6
samples_h
.by >sample1
.by >sample2
.by >sample3
.by >sample2
.by >sample4
.by >sample2
.by >sample5
.by >sample2
.by >sample3
.by >sample2
.by >sample4
.by >sample2
.by >sample6
samples_end_l
.by <sample_end1
.by <sample_end2
.by <sample_end3
.by <sample_end2
.by <sample_end4
.by <sample_end2
.by <sample_end5
.by <sample_end2
.by <sample_end3
.by <sample_end2
.by <sample_end4
.by <sample_end2
.by <sample_end6
samples_end_h
.by >sample_end1
.by >sample_end2
.by >sample_end3
.by >sample_end2
.by >sample_end4
.by >sample_end2
.by >sample_end5
.by >sample_end2
.by >sample_end3
.by >sample_end2
.by >sample_end4
.by >sample_end2
.by >sample_end6
ini start ini start
Binary file not shown.
Binary file not shown.
File diff suppressed because one or more lines are too long
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
+6 -1
View File
@@ -179,4 +179,9 @@
dex dex
bne ?PAUSELOOP bne ?PAUSELOOP
.ENDM .ENDM
;-------------------------------------
.MACRO POKEY_INIT
; Initialises Pokey chip (required before playing sounds)
mva #0 AUDCTL
mva #3 SKSTAT
.ENDM