Skip to content

Commit

Permalink
Merge pull request #2 from yuma-m/feature/fix-player
Browse files Browse the repository at this point in the history
Fix Player class
  • Loading branch information
yuma-m committed Dec 2, 2017
2 parents 5b344b9 + b50649f commit 64dcef6
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 8 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 0.1.4

- Fix channels of Player class from 2 to 1 to play proper sound.

## 0.1.3

- Specify audio output device.
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from setuptools import setup, find_packages
version = '0.1.3'
version = '0.1.4'

try:
import pypandoc
Expand Down
12 changes: 5 additions & 7 deletions synthesizer/player.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,20 @@
# -*- coding: utf-8 -*-

import struct

import numpy as np


class Player(object):

def __init__(self, channels=2, rate=44100):
def __init__(self, rate=44100):
try:
import pyaudio
self._pyaudio = pyaudio.PyAudio()
self._format = pyaudio.paInt16
except ImportError:
self._pyaudio = None
self._stream = None
self._channels = channels
# TODO: support stereo channels
self._channels = 1
self._rate = rate

def enumerate_device(self):
Expand Down Expand Up @@ -65,6 +64,5 @@ def play_wave(self, wave):
"""
if not self._stream:
raise RuntimeError("audio stream is not opened, please call open_stream() first.")
wave = (wave * float(2 ** 15 - 1)).astype(np.int16).tolist()
data = struct.pack("h" * len(wave), *wave)
self._stream.write(data)
wave = (wave * float(2 ** 15 - 1)).astype(np.int16).tobytes()
self._stream.write(wave)

0 comments on commit 64dcef6

Please sign in to comment.