convert_audio.sh 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. #!/bin/bash
  2. # Audio Conversion Script for ZenTap
  3. # Converts audio files according to specifications in assets/audio/README.md
  4. set -e # Exit on any error
  5. # Colors for output
  6. RED='\033[0;31m'
  7. GREEN='\033[0;32m'
  8. YELLOW='\033[1;33m'
  9. BLUE='\033[0;34m'
  10. NC='\033[0m' # No Color
  11. # Directories
  12. AUDIO_DIR="assets/audio"
  13. SOURCE_DIR="assets/audio/source"
  14. MENU_DIR="assets/audio/menu"
  15. INGAME_DIR="assets/audio/ingame"
  16. COMPRESSED_DIR="assets/audio/compressed"
  17. OUTPUT_DIR="assets/audio"
  18. echo -e "${BLUE}ZenTap Audio Conversion Script${NC}"
  19. echo "=================================="
  20. # Check if ffmpeg is installed
  21. if ! command -v ffmpeg &> /dev/null; then
  22. echo -e "${RED}Error: ffmpeg is not installed${NC}"
  23. echo "Please install ffmpeg:"
  24. echo " Ubuntu/Debian: sudo apt install ffmpeg"
  25. echo " macOS: brew install ffmpeg"
  26. echo " Windows: Download from https://ffmpeg.org/download.html"
  27. exit 1
  28. fi
  29. # Create directories if they don't exist
  30. mkdir -p "$SOURCE_DIR"
  31. mkdir -p "$MENU_DIR"
  32. mkdir -p "$INGAME_DIR"
  33. mkdir -p "$COMPRESSED_DIR"
  34. mkdir -p "$OUTPUT_DIR"
  35. echo -e "${YELLOW}Audio Conversion Specifications:${NC}"
  36. echo "- Sound Effects: MP3 format, 44.1 kHz, 128 kbps, mono, 0.1-0.5 seconds"
  37. echo "- Menu Music: MP3 format, 44.1 kHz, 192 kbps, stereo, 2-5 minutes"
  38. echo "- Ingame Music: MP3 format, 44.1 kHz, 192 kbps, stereo, 3-8 minutes"
  39. echo ""
  40. # Function to convert sound effects to MP3
  41. convert_sound_effect() {
  42. local input_file="$1"
  43. local output_file="$2"
  44. echo -e "${BLUE}Converting sound effect: $(basename "$input_file")${NC}"
  45. ffmpeg -i "$input_file" \
  46. -ar 44100 \
  47. -b:a 128k \
  48. -ac 1 \
  49. -t 0.5 \
  50. -y \
  51. "$output_file"
  52. if [ $? -eq 0 ]; then
  53. echo -e "${GREEN}✓ Successfully converted $(basename "$output_file")${NC}"
  54. else
  55. echo -e "${RED}✗ Failed to convert $(basename "$input_file")${NC}"
  56. fi
  57. }
  58. # Function to convert background music to MP3
  59. convert_background_music() {
  60. local input_file="$1"
  61. local output_file="$2"
  62. echo -e "${BLUE}Converting background music: $(basename "$input_file")${NC}"
  63. ffmpeg -i "$input_file" \
  64. -ar 44100 \
  65. -b:a 192k \
  66. -ac 2 \
  67. -y \
  68. "$output_file"
  69. if [ $? -eq 0 ]; then
  70. echo -e "${GREEN}✓ Successfully converted $(basename "$output_file")${NC}"
  71. else
  72. echo -e "${RED}✗ Failed to convert $(basename "$input_file")${NC}"
  73. fi
  74. }
  75. # Function to validate audio file duration
  76. validate_duration() {
  77. local file="$1"
  78. local expected_min="$2"
  79. local expected_max="$3"
  80. duration=$(ffprobe -v quiet -show_entries format=duration -of default=noprint_wrappers=1:nokey=1 "$file" 2>/dev/null)
  81. if [ $? -eq 0 ] && [ -n "$duration" ]; then
  82. # Convert to integer using awk for better compatibility
  83. duration_int=$(echo "$duration" | awk '{print int($1)}')
  84. if [ "$duration_int" -ge "$expected_min" ] && [ "$duration_int" -le "$expected_max" ]; then
  85. echo -e "${GREEN}✓ Duration validation passed: ${duration}s${NC}"
  86. else
  87. echo -e "${YELLOW}⚠ Duration warning: ${duration}s (expected ${expected_min}-${expected_max}s)${NC}"
  88. fi
  89. else
  90. echo -e "${YELLOW}⚠ Could not validate duration${NC}"
  91. fi
  92. }
  93. echo -e "${YELLOW}Looking for source files in $SOURCE_DIR${NC}"
  94. echo ""
  95. # Convert sound effects
  96. for sound_effect in "bubble_pop" "bubble_pop_alt" "ambient_background"; do
  97. # Look for various input formats
  98. for ext in wav mp3 ogg flac m4a; do
  99. source_file="$SOURCE_DIR/${sound_effect}.$ext"
  100. if [ -f "$source_file" ]; then
  101. output_file="$SOURCE_DIR/${sound_effect}.mp3"
  102. if [[ "$sound_effect" == "ambient_background" ]]; then
  103. convert_background_music "$source_file" "$output_file"
  104. if [ -f "$output_file" ]; then
  105. validate_duration "$output_file" 120 300 # 2-5 minutes
  106. fi
  107. else
  108. convert_sound_effect "$source_file" "$output_file"
  109. if [ -f "$output_file" ]; then
  110. validate_duration "$output_file" 0 1
  111. fi
  112. fi
  113. echo ""
  114. break
  115. fi
  116. done
  117. done
  118. # Convert menu music files
  119. echo -e "${YELLOW}Converting menu music files...${NC}"
  120. for theme in "default" "spring" "summer" "autumn" "winter"; do
  121. for ext in wav mp3 ogg flac m4a; do
  122. source_file="$SOURCE_DIR/menu_${theme}.$ext"
  123. if [ -f "$source_file" ]; then
  124. output_file="$MENU_DIR/menu_${theme}.mp3"
  125. convert_background_music "$source_file" "$output_file"
  126. if [ -f "$output_file" ]; then
  127. validate_duration "$output_file" 120 300 # 2-5 minutes
  128. fi
  129. echo ""
  130. break
  131. fi
  132. done
  133. done
  134. # Convert ingame music files
  135. echo -e "${YELLOW}Converting ingame music files...${NC}"
  136. for theme in "default" "spring" "summer" "autumn" "winter"; do
  137. for track_num in "001" "002" "003"; do
  138. for ext in wav mp3 ogg flac m4a; do
  139. source_file="$SOURCE_DIR/ingame_${theme}_${track_num}.$ext"
  140. if [ -f "$source_file" ]; then
  141. output_file="$INGAME_DIR/ingame_${theme}_${track_num}.mp3"
  142. convert_background_music "$source_file" "$output_file"
  143. if [ -f "$output_file" ]; then
  144. validate_duration "$output_file" 180 480 # 3-8 minutes
  145. fi
  146. echo ""
  147. break
  148. fi
  149. done
  150. done
  151. done
  152. # Check if we have any missing files
  153. echo -e "${YELLOW}Checking for required audio files:${NC}"
  154. echo -e "${BLUE}Sound Effects (source/):${NC}"
  155. sound_effect_files=(
  156. "source/bubble_pop.mp3"
  157. "source/bubble_pop_alt.mp3"
  158. "source/ambient_background.mp3"
  159. )
  160. echo -e "${BLUE}Menu Music (menu/):${NC}"
  161. menu_music_files=(
  162. "menu/menu_default.mp3"
  163. "menu/menu_spring.mp3"
  164. "menu/menu_summer.mp3"
  165. "menu/menu_autumn.mp3"
  166. "menu/menu_winter.mp3"
  167. )
  168. echo -e "${BLUE}Ingame Music (ingame/):${NC}"
  169. ingame_music_files=(
  170. "ingame/ingame_default_001.mp3"
  171. "ingame/ingame_spring_001.mp3"
  172. "ingame/ingame_summer_001.mp3"
  173. "ingame/ingame_autumn_001.mp3"
  174. "ingame/ingame_winter_001.mp3"
  175. "ingame/ingame_winter_002.mp3"
  176. )
  177. all_files=("${sound_effect_files[@]}" "${menu_music_files[@]}" "${ingame_music_files[@]}")
  178. missing_files=()
  179. for file in "${all_files[@]}"; do
  180. if [ -f "$AUDIO_DIR/$file" ]; then
  181. echo -e "${GREEN}✓ $file${NC}"
  182. else
  183. echo -e "${RED}✗ $file (missing)${NC}"
  184. missing_files+=("$file")
  185. fi
  186. done
  187. echo ""
  188. if [ ${#missing_files[@]} -eq 0 ]; then
  189. echo -e "${GREEN}All audio files are present!${NC}"
  190. else
  191. echo -e "${YELLOW}Missing files: ${#missing_files[@]} out of ${#all_files[@]}${NC}"
  192. echo ""
  193. echo -e "${BLUE}To add missing files:${NC}"
  194. echo "1. Place source audio files in $SOURCE_DIR/ with appropriate names"
  195. echo "2. Run this script again"
  196. echo ""
  197. echo -e "${BLUE}Expected source file naming:${NC}"
  198. echo "- Sound effects: bubble_pop.*, bubble_pop_alt.*, ambient_background.*"
  199. echo "- Menu music: menu_default.*, menu_spring.*, menu_summer.*, menu_autumn.*, menu_winter.*"
  200. echo "- Ingame music: ingame_<theme>_<track>.* (e.g., ingame_winter_001.mp3)"
  201. echo ""
  202. echo -e "${BLUE}Supported source formats:${NC} WAV, MP3, OGG, FLAC, M4A"
  203. fi
  204. echo ""
  205. echo -e "${BLUE}Audio conversion complete!${NC}"