1414#include "common-hal/audioi2sin/I2SIn.h"
1515#include "shared-bindings/audioi2sin/I2SIn.h"
1616#include "shared-bindings/microcontroller/Pin.h"
17+ #include "shared-module/audioi2sin/__init__.h"
1718#include "bindings/rp2pio/StateMachine.h"
1819#include "supervisor/port.h"
1920
@@ -397,17 +398,12 @@ static inline int32_t i2sin_normalize_signed(uint32_t raw, uint8_t in_depth,
397398 return (int16_t )(raw & 0xffffu );
398399}
399400
400- // Write `raw` (input-depth bits, just-read FIFO sample) to `buffer` at sample
401- // index `idx`, converting from `in_depth` to the explicitly-requested
402- // `out_depth` (sign-preserving for signed) and (if needed) flipping the sign
403- // bit for the unsigned-WAV convention. This path runs only when the caller set
404- // `output_bit_depth`, i.e. asked for a real bit-depth rescale: upscaling
405- // bit-replicates so full-scale input maps to full-scale output (e.g. 16-bit
406- // 0xFFFF -> 24-bit 0xFFFFFF) and downscaling arithmetic-shifts right. This is
407- // distinct from the implicit container width set by the array typecode (e.g. a
408- // 24-bit sample carried in a 32-bit 'i' slot), which only sign-extends and is
409- // handled by the default `movesign24` path. Output element size follows
410- // `out_depth`: 1 byte at 8, 2 bytes at 16, 4 bytes at 24 or 32.
401+ // Normalize `raw` (input-depth bits, just-read FIFO sample) for this port's wire
402+ // format, then hand off to the shared converter to rescale `in_depth` ->
403+ // `out_depth` and write it to `buffer` at sample index `idx`. The depth
404+ // conversion + unsigned-WAV flip + container store live in
405+ // `shared_audioi2sin_write_converted` (shared with other ports); see that helper
406+ // for the upscale/downscale semantics.
411407//
412408// For signed 24-bit output, the int32 slot holds the sign-extended value
413409// (range -2^23 .. 2^23-1) — unlike the default `output_bit_depth=bit_depth=24`
@@ -417,49 +413,7 @@ static inline void i2sin_write_converted(void *buffer, uint32_t idx,
417413 uint32_t raw , uint8_t in_depth , uint8_t out_depth ,
418414 bool samples_signed , bool left_justified ) {
419415 int32_t s = i2sin_normalize_signed (raw , in_depth , left_justified );
420- int32_t shifted ;
421- if (out_depth > in_depth ) {
422- // Explicit upscale: bit-replicate the input across the wider output so
423- // that full-scale input maps to full-scale output (e.g. 16-bit 0xFFFF
424- // -> 24-bit 0xFFFFFF), filling out_depth bits rather than leaving the
425- // new low bits zero.
426- uint32_t in_mask = (in_depth >= 32 ) ? 0xffffffffu : ((1u << in_depth ) - 1u );
427- uint32_t in_bits = (uint32_t )s & in_mask ;
428- uint32_t result = 0 ;
429- int remaining = out_depth ;
430- while (remaining > 0 ) {
431- int take = (remaining >= (int )in_depth ) ? (int )in_depth : remaining ;
432- result = (result << take ) | (in_bits >> (in_depth - take ));
433- remaining -= take ;
434- }
435- // Sign-extend the out_depth-bit result up to int32 so a 24-bit value in
436- // a 32-bit container decodes correctly; harmless when out_depth == 32.
437- shifted = (int32_t )(result << (32 - out_depth )) >> (32 - out_depth );
438- } else if (out_depth == in_depth ) {
439- shifted = s ;
440- } else {
441- shifted = s >> (in_depth - out_depth );
442- }
443- uint32_t u = (uint32_t )shifted ;
444- if (!samples_signed ) {
445- if (out_depth >= 32 ) {
446- u ^= 0x80000000u ;
447- } else {
448- uint32_t mask = (1u << out_depth ) - 1u ;
449- u = (u & mask ) ^ (1u << (out_depth - 1 ));
450- }
451- }
452- switch (out_depth ) {
453- case 8 :
454- ((uint8_t * )buffer )[idx ] = (uint8_t )(u & 0xffu );
455- break ;
456- case 16 :
457- ((uint16_t * )buffer )[idx ] = (uint16_t )(u & 0xffffu );
458- break ;
459- default : // 24 or 32
460- ((uint32_t * )buffer )[idx ] = u ;
461- break ;
462- }
416+ shared_audioi2sin_write_converted (buffer , idx , s , in_depth , out_depth , samples_signed );
463417}
464418
465419uint32_t common_hal_audioi2sin_i2sin_record_to_buffer (audioi2sin_i2sin_obj_t * self ,
0 commit comments