xref: /OK3568_Linux_fs/debian/overlay-debug/rockchip-test/audio/test_loopback.sh (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1#!/bin/bash
2
3device_1=$1
4device_2=$2
5
6prepare_mic_capture()
7{
8	amixer set "Capture Digital" 192
9	amixer set "Capture Mute" 0
10	amixer set "Right PGA Mux" "DifferentialR"
11	amixer set "Left PGA Mux" "DifferentialL"
12	amixer set "Differential Mux" "Line 2"
13}
14
15prepare_adc_gains()
16{
17	adc_mic_gain=$1
18
19	echo "Prepare ADC MIC GAINs with $adc_mic_gain dB"
20	# PGA gain
21	amixer set "Left Channel" $adc_mic_gain
22	amixer set "Right Channel" $adc_mic_gain
23}
24
25test_loopback()
26{
27	# PATH_CAPTURE=/mnt/sdcard/cap_files
28	# PATH_CAPTURE=/media/usb0/cap_files
29	PATH_CAPTURE=/tmp/cap_files
30	play_device="default"
31	capt_device="default"
32	fs=16000
33	capt_bits="S16_LE"
34	capt_ch=2
35	capt_seconds=60			# capture once per 1min
36	capt_minutes=60			# capture minutes
37	switch_gain_count=8
38	play_seconds=2
39	play_start_doze=0.3
40	play_stop_doze=1
41	play_bits=16
42	play_ch=2
43
44	if [ -n "$1" ]; then
45		play_device=$1
46	fi
47
48	if [ -n "$2" ]; then
49		capt_device=$2
50	fi
51
52	# play_gain_tbl="-30 -25 -20 -15 -10 -5 0 5 10 15 20 25 30"
53	play_gain_tbl="-30"
54	set -- $play_gain_tbl
55	play_gain_num=$#
56	test_hours=5
57	let "capt_count=$capt_minutes*$test_hours"		# capture 120 hours
58	let "play_count=$capt_seconds/($play_seconds+$play_stop_doze)"
59
60	mkdir -p $PATH_CAPTURE
61
62	echo "play_device: $play_device, capt_device: $capt_device, capt_count: $capt_count, play_count: $play_count, test $test_hours hours on PATH_CAPTURE: $PATH_CAPTURE"
63
64	echo "******** Test loopback start ********"
65
66	prepare_mic_capture
67
68	for capt_cnt in `seq 1 $capt_count`; do
69		capt_gain=0
70		# playback -> capture -> playback
71		sox -b $play_bits -r $fs -c $play_ch -n -t alsa $play_device synth $play_seconds sine 1000 gain -30 &  # do playback
72		# start doze
73		sleep $play_start_doze
74
75		let "temp=($capt_cnt-1)/($capt_count/$switch_gain_count)"
76		let "capt_gain=$temp%(switch_gain_count+1)*3" # step 3dB
77
78		DUMP_FILE=$(printf 'loopback_fs%d_format_%s_ch%d_mic%ddb_%04d.wav' $fs $capt_bits $capt_ch $capt_gain $capt_cnt)
79		echo "temp: $temp, capt_gain: $capt_gain DUMP_FILE: $DUMP_FILE"
80
81		prepare_adc_gains $capt_gain
82
83		# echo "capt_cnt: $capt_cnt"
84		# echo "play_count: $play_count, play_seconds: $play_seconds"
85		echo "arecord -D $capt_device -r $fs -f $capt_bits -c $capt_ch -d $capt_seconds $PATH_CAPTURE/$DUMP_FILE"
86		arecord -D $capt_device -r $fs -f $capt_bits -c $capt_ch -d $capt_seconds $PATH_CAPTURE/$DUMP_FILE &  # do capture
87
88		# wait the first playback stop
89		sleep $play_seconds
90
91		set -- $play_gain_tbl
92		for play_cnt in `seq 1 $play_count`; do
93			play_gain=$1
94			shift
95			let play_gain_index+=1
96
97			# echo "play_gain_index: $play_gain_index, play_gain_num: $play_gain_num"
98			echo "sox -b $play_bits -r $fs -c $play_ch -n -t alsa $play_device synth $play_seconds sine 1000 gain $play_gain"
99			if [ $play_gain_index -ge $play_gain_num ]; then
100				set -- $play_gain_tbl
101				play_gain_index=0
102			fi
103			# echo "play_cnt: $play_cnt"
104			sox -b $play_bits -r $fs -c $play_ch -n -t alsa $play_device synth $play_seconds sine 1000 gain $play_gain # do playback
105			# stop doze
106			sleep $play_stop_doze
107		done;
108	done;
109
110	echo "******** Test loopback end ********"
111}
112
113echo "******** Test loopback v0.1.0 ********"
114
115test_loopback $device_1 $device_2
116