convert_audio.sh 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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 ]; then
  75. duration_int=$(printf "%.0f" "$duration")
  76. if [ "$duration_int" -ge "$expected_min" ] && [ "$duration_int" -le "$expected_max" ]; then
  77. echo -e "${GREEN}✓ Duration validation passed: ${duration}s${NC}"
  78. else
  79. echo -e "${YELLOW}⚠ Duration warning: ${duration}s (expected ${expected_min}-${expected_max}s)${NC}"
  80. fi
  81. else
  82. echo -e "${YELLOW}⚠ Could not validate duration${NC}"
  83. fi
  84. }
  85. echo -e "${YELLOW}Looking for source files in $SOURCE_DIR${NC}"
  86. echo ""
  87. # Convert sound effects
  88. for sound_effect in "bubble_pop" "bubble_pop_alt"; do
  89. # Look for various input formats
  90. for ext in wav mp3 ogg flac m4a; do
  91. source_file="$SOURCE_DIR/${sound_effect}.$ext"
  92. if [ -f "$source_file" ]; then
  93. output_file="$OUTPUT_DIR/${sound_effect}.wav"
  94. convert_sound_effect "$source_file" "$output_file"
  95. if [ -f "$output_file" ]; then
  96. validate_duration "$output_file" 0 1
  97. fi
  98. echo ""
  99. break
  100. fi
  101. done
  102. done
  103. # Convert background music
  104. for music_track in "ambient_background" "zen_mode_background"; do
  105. # Look for various input formats
  106. for ext in wav mp3 ogg flac m4a; do
  107. source_file="$SOURCE_DIR/${music_track}.$ext"
  108. if [ -f "$source_file" ]; then
  109. output_file="$OUTPUT_DIR/${music_track}.mp3"
  110. convert_background_music "$source_file" "$output_file"
  111. if [ -f "$output_file" ]; then
  112. validate_duration "$output_file" 120 300 # 2-5 minutes
  113. fi
  114. echo ""
  115. break
  116. fi
  117. done
  118. done
  119. # Check if we have any missing files
  120. echo -e "${YELLOW}Checking for required audio files:${NC}"
  121. required_files=(
  122. "bubble_pop.wav"
  123. "bubble_pop_alt.wav"
  124. "ambient_background.mp3"
  125. "zen_mode_background.mp3"
  126. )
  127. missing_files=()
  128. for file in "${required_files[@]}"; do
  129. if [ -f "$OUTPUT_DIR/$file" ]; then
  130. echo -e "${GREEN}✓ $file${NC}"
  131. else
  132. echo -e "${RED}✗ $file (missing)${NC}"
  133. missing_files+=("$file")
  134. fi
  135. done
  136. echo ""
  137. if [ ${#missing_files[@]} -eq 0 ]; then
  138. echo -e "${GREEN}All required audio files are present!${NC}"
  139. else
  140. echo -e "${YELLOW}Missing files: ${missing_files[*]}${NC}"
  141. echo ""
  142. echo -e "${BLUE}To add missing files:${NC}"
  143. echo "1. Place source audio files in $SOURCE_DIR/"
  144. echo "2. Run this script again"
  145. echo ""
  146. echo -e "${BLUE}Supported source formats:${NC} WAV, MP3, OGG, FLAC, M4A"
  147. fi
  148. echo ""
  149. echo -e "${BLUE}Audio conversion complete!${NC}"