|
| 1 | +# Binaural Beat Player - Development Guide |
| 2 | + |
| 3 | +## Project Overview |
| 4 | +A Next.js-based binaural beat player with Web Audio API integration, migrated from Node.js to Bun runtime for improved performance and reduced bundle size. |
| 5 | + |
| 6 | +## Technology Stack |
| 7 | +- **Runtime**: Bun (>=1.0.0) |
| 8 | + - **Framework**: Next.js 14.2.15 with React 18.3.1 |
| 9 | +- **Styling**: Tailwind CSS v4 |
| 10 | + - **Audio**: Web Audio API |
| 11 | + - **Icons**: Lucide React & React Icons |
| 12 | +- **Deployment**: GitHub Pages (static export) |
| 13 | + |
| 14 | +## Development Commands |
| 15 | + |
| 16 | +### Core Commands |
| 17 | +```bash |
| 18 | +# Install dependencies |
| 19 | +bun install |
| 20 | + |
| 21 | +# Start development server with Turbopack |
| 22 | +bun run dev |
| 23 | + |
| 24 | +# Build for production |
| 25 | +bun run build |
| 26 | + |
| 27 | +# Start production server |
| 28 | +bun run start |
| 29 | + |
| 30 | +# Run linting |
| 31 | +bun run lint |
| 32 | + |
| 33 | +# Deploy to GitHub Pages |
| 34 | +bun run deploy |
| 35 | +``` |
| 36 | + |
| 37 | +## Key Features |
| 38 | +- **Binaural Beat Generation**: Real-time audio synthesis using Web Audio API |
| 39 | +- **Pattern Mode**: JSON-based frequency sequences with timing control |
| 40 | +- **Manual Mode**: Direct frequency control with range sliders (1-600 Hz) |
| 41 | +- **Visualizer**: Real-time waveform visualization using Canvas API |
| 42 | +- **Volume Control**: Frequency-aware volume adjustment |
| 43 | +- **Responsive Design**: Mobile-first approach with Tailwind CSS |
| 44 | + |
| 45 | +## Project Structure |
| 46 | +``` |
| 47 | +├── app/ # Next.js App Router |
| 48 | +│ ├── page.tsx # Main binaural player component |
| 49 | +│ ├── layout.tsx # Root layout |
| 50 | +│ └── globals.css # Global styles |
| 51 | +├── components/ # Reusable components |
| 52 | +│ └── Footer.tsx # Footer with donate links |
| 53 | +├── bineural-songs/ # Predefined patterns |
| 54 | +│ ├── ai-gemini.json # AI-focused pattern |
| 55 | +│ ├── creative.json # Creativity enhancement |
| 56 | +│ ├── intro.json # Introduction pattern |
| 57 | +│ ├── relaxing.json # Relaxation pattern |
| 58 | +│ └── scala.json # Scala tuning pattern |
| 59 | +└── public/ # Static assets |
| 60 | +``` |
| 61 | + |
| 62 | +## Audio Implementation Details |
| 63 | + |
| 64 | +### Web Audio API Architecture |
| 65 | +- **AudioContext**: Main audio processing context |
| 66 | +- **OscillatorNodes**: Generate left/right channel frequencies |
| 67 | +- **StereoPannerNodes**: Channel separation (-1 left, +1 right) |
| 68 | +- **GainNode**: Volume control with frequency-based scaling |
| 69 | + |
| 70 | +### Frequency Scaling |
| 71 | +Volume automatically adjusts based on average frequency: |
| 72 | +- 1 Hz: 100% volume |
| 73 | +- 600 Hz: 30% volume |
| 74 | +- Linear interpolation between endpoints |
| 75 | + |
| 76 | +### Pattern Format |
| 77 | +```json |
| 78 | +{ |
| 79 | + "pattern": [ |
| 80 | + [left_frequency, right_frequency, duration_ms], |
| 81 | + // ... more steps |
| 82 | + ] |
| 83 | +} |
| 84 | +``` |
| 85 | + |
| 86 | +## Migration Notes (Node.js → Bun) |
| 87 | + |
| 88 | +### Removed Dependencies |
| 89 | +- `@types/node`: No longer needed with Bun's built-in Node.js compatibility |
| 90 | +- `tone`: Removed to prevent localStorage SSR issues |
| 91 | +- `recharts`: Removed to prevent localStorage SSR issues |
| 92 | +- `package-lock.json`: Replaced with `bun.lockb` |
| 93 | + |
| 94 | +### Configuration Changes |
| 95 | +- **next.config.js**: Renamed from .ts for compatibility, static export for production |
| 96 | +- **package.json**: Added Bun engine requirement |
| 97 | +- **.gitignore**: Added `bun.lockb` and `bun-debug.log*` |
| 98 | + |
| 99 | +### Version Compatibility |
| 100 | +- Downgraded Next.js from 15.3.1 to 14.2.15 for stability |
| 101 | +- Downgraded React from 19 to 18.3.1 for better SSR compatibility |
| 102 | +- Changed font from Geist to Inter for Next.js 14 compatibility |
| 103 | + |
| 104 | +### Performance Benefits |
| 105 | +- Faster installation times |
| 106 | +- Reduced bundle size |
| 107 | +- Improved runtime performance |
| 108 | +- Built-in TypeScript support |
| 109 | + |
| 110 | +## Development Guidelines |
| 111 | + |
| 112 | +### Code Style |
| 113 | +- Use TypeScript with strict mode |
| 114 | +- Follow React functional component patterns |
| 115 | +- Implement proper cleanup in useEffect hooks |
| 116 | +- Use semantic HTML with ARIA labels |
| 117 | + |
| 118 | +### Audio Best Practices |
| 119 | +- Always handle AudioContext state changes (suspended/resumed) |
| 120 | +- Implement proper oscillator cleanup |
| 121 | +- Use smooth transitions (linearRampToValueAtTime) |
| 122 | +- Handle browser compatibility gracefully |
| 123 | + |
| 124 | +### State Management |
| 125 | +- Keep audio state separate from UI state |
| 126 | +- Use refs for persistent audio objects |
| 127 | +- Implement proper error boundaries |
| 128 | + |
| 129 | +## Deployment |
| 130 | +The app builds as a static site for GitHub Pages: |
| 131 | +- Output directory: `out/` |
| 132 | +- Base path: `/binaural-beat-player` (production only) |
| 133 | +- Automatic .nojekyll file generation |
| 134 | + |
| 135 | +## Browser Compatibility |
| 136 | +- Modern browsers with Web Audio API support |
| 137 | +- Chrome/Edge: Full support |
| 138 | +- Firefox: Full support |
| 139 | +- Safari: Partial support (may require user interaction) |
| 140 | + |
| 141 | +## Deployment |
| 142 | +The app builds as a static site for GitHub Pages: |
| 143 | +- Output directory: `out/` |
| 144 | +- Base path: `/binaural-beat-player` (production only) |
| 145 | +- Automatic .nojekyll file generation |
| 146 | + |
| 147 | +## Browser Compatibility |
| 148 | +- Modern browsers with Web Audio API support |
| 149 | +- Chrome/Edge: Full support |
| 150 | +- Firefox: Full support |
| 151 | +- Safari: Partial support (may require user interaction) |
| 152 | + |
| 153 | +## Troubleshooting |
| 154 | +- **Audio not playing**: Check browser autoplay policies |
| 155 | +- **Context suspended**: Call `audioContext.resume()` after user interaction |
| 156 | +- **Performance issues**: Monitor oscillator cleanup and gain node connections |
| 157 | +- **SSR Issues**: Use `typeof window === 'undefined'` checks for browser APIs |
0 commit comments