convert_audio.sh 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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. OUTPUT_DIR="assets/audio"
  15. echo -e "${BLUE}ZenTap Audio Conversion Script${NC}"
  16. echo "=================================="
  17. # Check if ffmpeg is installed
  18. if ! command -v ffmpeg &> /dev/null; then
  19. echo -e "${RED}Error: ffmpeg is not installed${NC}"
  20. echo "Please install ffmpeg:"
  21. echo " Ubuntu/Debian: sudo apt install ffmpeg"
  22. echo " macOS: brew install ffmpeg"
  23. echo " Windows: Download from https://ffmpeg.org/download.html"
  24. exit 1
  25. fi
  26. # Create directories if they don't exist
  27. mkdir -p "$SOURCE_DIR"
  28. mkdir -p "$OUTPUT_DIR"
  29. echo -e "${YELLOW}Audio Conversion Specifications:${NC}"
  30. echo "- Sound Effects: WAV format, 44.1 kHz, 16-bit, 0.1-0.5 seconds"
  31. echo "- Background Music: MP3 format, 44.1 kHz, 128-320 kbps, 2-5 minutes"
  32. echo ""
  33. # Function to convert sound effects to WAV
  34. convert_sound_effect() {
  35. local input_file="$1"
  36. local output_file="$2"
  37. echo -e "${BLUE}Converting sound effect: $(basename "$input_file")${NC}"
  38. ffmpeg -i "$input_file" \
  39. -ar 44100 \
  40. -sample_fmt s16 \
  41. -ac 1 \
  42. -t 0.5 \
  43. -y \
  44. "$output_file"
  45. if [ $? -eq 0 ]; then
  46. echo -e "${GREEN}✓ Successfully converted $(basename "$output_file")${NC}"
  47. else
  48. echo -e "${RED}✗ Failed to convert $(basename "$input_file")${NC}"
  49. fi
  50. }
  51. # Function to convert background music to MP3
  52. convert_background_music() {
  53. local input_file="$1"
  54. local output_file="$2"
  55. echo -e "${BLUE}Converting background music: $(basename "$input_file")${NC}"
  56. ffmpeg -i "$input_file" \
  57. -ar 44100 \
  58. -b:a 192k \
  59. -ac 2 \
  60. -y \
  61. "$output_file"
  62. if [ $? -eq 0 ]; then
  63. echo -e "${GREEN}✓ Successfully converted $(basename "$output_file")${NC}"
  64. else
  65. echo -e "${RED}✗ Failed to convert $(basename "$input_file")${NC}"
  66. fi
  67. }
  68. # Function to validate audio file duration
  69. validate_duration() {
  70. local file="$1"
  71. local expected_min="$2"
  72. local expected_max="$3"
  73. duration=$(ffprobe -v quiet -show_entries format=duration -of default=noprint_wrappers=1:nokey=1 "$file" 2>/dev/null)
  74. if [ $? -eq 0 ] && [ -n "$duration" ]; then
  75. # Convert to integer using awk for better compatibility
  76. duration_int=$(echo "$duration" | awk '{print int($1)}')
  77. if [ "$duration_int" -ge "$expected_min" ] && [ "$duration_int" -le "$expected_max" ]; then
  78. echo -e "${GREEN}✓ Duration validation passed: ${duration}s${NC}"
  79. else
  80. echo -e "${YELLOW}⚠ Duration warning: ${duration}s (expected ${expected_min}-${expected_max}s)${NC}"
  81. fi
  82. else
  83. echo -e "${YELLOW}⚠ Could not validate duration${NC}"
  84. fi
  85. }
  86. echo -e "${YELLOW}Looking for source files in $SOURCE_DIR${NC}"
  87. echo ""
  88. # Convert sound effects
  89. for sound_effect in "bubble_pop" "bubble_pop_alt"; do
  90. # Look for various input formats
  91. for ext in wav mp3 ogg flac m4a; do
  92. source_file="$SOURCE_DIR/${sound_effect}.$ext"
  93. if [ -f "$source_file" ]; then
  94. output_file="$OUTPUT_DIR/${sound_effect}.wav"
  95. convert_sound_effect "$source_file" "$output_file"
  96. if [ -f "$output_file" ]; then
  97. validate_duration "$output_file" 0 1
  98. fi
  99. echo ""
  100. break
  101. fi
  102. done
  103. done
  104. # Convert background music
  105. for music_track in "ambient_background" "zen_mode_background"; do
  106. # Look for various input formats
  107. for ext in wav mp3 ogg flac m4a; do
  108. source_file="$SOURCE_DIR/${music_track}.$ext"
  109. if [ -f "$source_file" ]; then
  110. output_file="$OUTPUT_DIR/${music_track}.mp3"
  111. convert_background_music "$source_file" "$output_file"
  112. if [ -f "$output_file" ]; then
  113. validate_duration "$output_file" 120 300 # 2-5 minutes
  114. fi
  115. echo ""
  116. break
  117. fi
  118. done
  119. done
  120. # Check if we have any missing files
  121. echo -e "${YELLOW}Checking for required audio files:${NC}"
  122. required_files=(
  123. "bubble_pop.wav"
  124. "bubble_pop_alt.wav"
  125. "ambient_background.mp3"
  126. "zen_mode_background.mp3"
  127. )
  128. missing_files=()
  129. for file in "${required_files[@]}"; do
  130. if [ -f "$OUTPUT_DIR/$file" ]; then
  131. echo -e "${GREEN}✓ $file${NC}"
  132. else
  133. echo -e "${RED}✗ $file (missing)${NC}"
  134. missing_files+=("$file")
  135. fi
  136. done
  137. echo ""
  138. if [ ${#missing_files[@]} -eq 0 ]; then
  139. echo -e "${GREEN}All required audio files are present!${NC}"
  140. else
  141. echo -e "${YELLOW}Missing files: ${missing_files[*]}${NC}"
  142. echo ""
  143. echo -e "${BLUE}To add missing files:${NC}"
  144. echo "1. Place source audio files in $SOURCE_DIR/"
  145. echo "2. Run this script again"
  146. echo ""
  147. echo -e "${BLUE}Supported source formats:${NC} WAV, MP3, OGG, FLAC, M4A"
  148. fi
  149. echo ""
  150. echo -e "${BLUE}Audio conversion complete!${NC}"