()
| 441 | } |
| 442 | |
| 443 | func (c *wasapiContext) restart() error { |
| 444 | // Probably the driver is missing temporarily e.g. plugging out the headset. |
| 445 | // Recreate the device. |
| 446 | |
| 447 | retry: |
| 448 | c.suspendedCond.L.Lock() |
| 449 | for c.suspended { |
| 450 | c.suspendedCond.Wait() |
| 451 | } |
| 452 | c.suspendedCond.L.Unlock() |
| 453 | |
| 454 | if err := c.start(); err != nil { |
| 455 | // When a device is switched, the new device might not support the desired format, |
| 456 | // or all the audio devices might be disconnected. |
| 457 | // Instead of aborting this context, let's wait for the next device switch. |
| 458 | if !errors.Is(err, errFormatNotSupported) && !errors.Is(err, errDeviceNotFound) { |
| 459 | return err |
| 460 | } |
| 461 | |
| 462 | // Just read the buffer and discard it. Then, retry to search the device. |
| 463 | var buf32 [4096]float32 |
| 464 | sleep := time.Duration(float64(time.Second) * float64(len(buf32)) / float64(c.channelCount) / float64(c.sampleRate)) |
| 465 | c.mux.ReadFloat32s(buf32[:]) |
| 466 | time.Sleep(sleep) |
| 467 | goto retry |
| 468 | } |
| 469 | return nil |
| 470 | } |
no test coverage detected