Go-based FM stereo transmitter with RDS, Windows-first and cross-platform
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

77 行
1.9KB

  1. package factory
  2. import (
  3. "fmt"
  4. "io"
  5. "net/http"
  6. "os"
  7. "strings"
  8. "github.com/jan/fm-rds-tx/internal/config"
  9. "github.com/jan/fm-rds-tx/internal/ingest"
  10. "github.com/jan/fm-rds-tx/internal/ingest/adapters/httpraw"
  11. "github.com/jan/fm-rds-tx/internal/ingest/adapters/icecast"
  12. "github.com/jan/fm-rds-tx/internal/ingest/adapters/stdinpcm"
  13. )
  14. type Deps struct {
  15. Stdin io.Reader
  16. HTTP *http.Client
  17. }
  18. type AudioIngress interface {
  19. WritePCM16(data []byte) (int, error)
  20. }
  21. func BuildSource(cfg config.Config, deps Deps) (ingest.Source, AudioIngress, error) {
  22. switch normalizeIngestKind(cfg.Ingest.Kind) {
  23. case "", "none":
  24. return nil, nil, nil
  25. case "stdin", "stdin-pcm":
  26. reader := deps.Stdin
  27. if reader == nil {
  28. reader = os.Stdin
  29. }
  30. src := stdinpcm.New("stdin-main", reader, cfg.Ingest.Stdin.SampleRateHz, cfg.Ingest.Stdin.Channels, 1024)
  31. return src, nil, nil
  32. case "http-raw":
  33. src := httpraw.New("http-raw-main", cfg.Ingest.HTTPRaw.SampleRateHz, cfg.Ingest.HTTPRaw.Channels)
  34. return src, src, nil
  35. case "icecast":
  36. src := icecast.New(
  37. "icecast-main",
  38. cfg.Ingest.Icecast.URL,
  39. deps.HTTP,
  40. icecast.ReconnectConfig{
  41. Enabled: cfg.Ingest.Reconnect.Enabled,
  42. InitialBackoffMs: cfg.Ingest.Reconnect.InitialBackoffMs,
  43. MaxBackoffMs: cfg.Ingest.Reconnect.MaxBackoffMs,
  44. },
  45. icecast.WithDecoderPreference(cfg.Ingest.Icecast.Decoder),
  46. )
  47. return src, nil, nil
  48. default:
  49. return nil, nil, fmt.Errorf("unsupported ingest kind: %s", cfg.Ingest.Kind)
  50. }
  51. }
  52. func SampleRateForKind(cfg config.Config) int {
  53. switch normalizeIngestKind(cfg.Ingest.Kind) {
  54. case "stdin", "stdin-pcm":
  55. if cfg.Ingest.Stdin.SampleRateHz > 0 {
  56. return cfg.Ingest.Stdin.SampleRateHz
  57. }
  58. case "http-raw":
  59. if cfg.Ingest.HTTPRaw.SampleRateHz > 0 {
  60. return cfg.Ingest.HTTPRaw.SampleRateHz
  61. }
  62. case "icecast":
  63. return 44100
  64. }
  65. return 44100
  66. }
  67. func normalizeIngestKind(kind string) string {
  68. return strings.ToLower(strings.TrimSpace(kind))
  69. }